1

Is there currently any way to get duktape to properly unwind the C++ stack (calling all appropriate destructors, etc.) when an error is encountered in duktape?

I know that Lua (for example) has the capability to do this by switching from the use of longjmp to C++ exceptions when Lua is compiled by a C++ compiler, but at first glance I'm not seeing anything similar in duktape, and I'm definitely winding up with C++ destructors not getting called. If that capability doesn't exist yet in duktape, are there any plans for it? (If the longjmp destinations are set and used in a strictly LIFO fashion, I'm guessing it'd be doable. If not, then maybe not so doable...)

I've been playing with duktape a bit and so far I've been impressed...I'm mostly done with a C++ template-based metaprogramming library that lets you automatically create duktape bindings to C++ functions. This stack-unwinding thing may be a complete deal-breaker, though...don't think I can use duktape if I can't trust my C++ destructors to get called.

EDIT: actually, on further reflection, I can probably make things work without the stack unwinding, although of course everything will be significantly more complex and error-prone. It'd still be a great feature to have.

4

3 回答 3

3

As a quick update: Duktape 1.4.0 has support for using C++ exceptions for long control transfers (https://github.com/svaarala/duktape/tree/master/examples/cpp-exceptions):

  • Define DUK_OPT_CPP_EXCEPTIONS (DUK_USE_CPP_EXCEPTIONS if you're editing duk_config.h directly) and compile using a C++ compiler.
  • Automatic destructors will then work when Duktape/C functions unwind.
  • You can't throw a C++ exception "through" Duktape however. If you do that Duktape will convert the C++ exception into an Ecmascript exception instead. So, while Duktape/C functions can use C++ exceptions internally those exceptions should be caught before they propagate to Duktape.
于 2016-01-30T12:20:12.153 回答
1

As of Duktape 1.2 there isn't yet support for replacing setjmp/longjmp (which are used for error handling inside Duktape) with a C++ exception friendly try-catch mechanism. Support for that is planned for future versions though.

于 2015-07-22T02:57:55.460 回答
0

Unfortunately you didn't provide any code samples so my guess is that you haven't set an fatal error handler in duk_create_heap and you are currently using the un-protected code execution variants like duk_eval. You should try using the protected code execution function variants like duk_peval instead.

于 2015-07-21T13:08:28.730 回答