If Pl_exception(0) is called when stack overflow occurs, the return value is a term that is a variable (as determined by PL_term_type()). Also, it seems to remove some exception information, so that return "false" results in failure rather than an exception. (That is, if I don't do the PL_exception(0) call, I get a resource error; if I do a PL_exception(0) and then return "false", I just get failure and no exception.)
When I don't call PL_exception(0), this is the error term I get (the foreign code is from test_cpp.cpp, but uses a "work-in-progress" version of SWI-cpp2.h):
catch(plunit_cpp:with_small_stacks(5000000,square_roots(1000000000,Z)), E, true), print_term(E, []).
error(resource_error(stack),
stack_overflow{ choicepoints:5,
depth:14,
environments:14,
globalused:3499,
localused:585,
stack:[ frame(14,user:square_roots(1000000000,[74655|_]),[]),
frame(13,system:setup_call_cleanup((:)/2,(:)/2,(:)/2),[]),
frame(12,plunit_cpp:with_small_stacks(5000000,'<garbage_collected>'),[]),
frame(11,system:catch((:)/2,_,(:)/2),[]),
frame(10,system:'<meta-call>'('<garbage_collected>'),[])
],
stack_limit:4885,
trailused:0
})
If you need a reproducible example, I can make one. But I was hoping that the reason can be determined by quickly looking at the code. Is it perhaps that PL_new_term_ref() doesn't work when there's a stack overflow, and the ensureLocalSpace() call confuses things? (I notice that there's no check for the PL_new_term_ref(), which presumably has returned 0; but before that it should have (recursively?) called raiseStackOverflow()?
I'm OK with getting a variable if the exception term can't be determined or allocated; the problem is that PL_exception(0) ends up removing some information from the exception, so a return false produces a failure in Prolog and not an exception.
If Pl_exception(0) is called when stack overflow occurs, the return value is a term that is a
variable(as determined byPL_term_type()). Also, it seems to remove some exception information, so that return "false" results in failure rather than an exception. (That is, if I don't do thePL_exception(0)call, I get a resource error; if I do aPL_exception(0)and then return "false", I just get failure and no exception.)When I don't call
PL_exception(0), this is the error term I get (the foreign code is fromtest_cpp.cpp, but uses a "work-in-progress" version ofSWI-cpp2.h):If you need a reproducible example, I can make one. But I was hoping that the reason can be determined by quickly looking at the code. Is it perhaps that
PL_new_term_ref()doesn't work when there's a stack overflow, and theensureLocalSpace()call confuses things? (I notice that there's no check for thePL_new_term_ref(), which presumably has returned0; but before that it should have (recursively?) calledraiseStackOverflow()?I'm OK with getting a variable if the exception term can't be determined or allocated; the problem is that
PL_exception(0)ends up removing some information from the exception, so areturn falseproduces a failure in Prolog and not an exception.