3

I just started using Duktape in my C++ framework today, and I've read the entire api without being able to understand how do i catch errors. I found some clues about an error object that is put on the stack However, every time there is an error (like an invalid javascript syntax for example), everything goes crazy and i get a SEGFAULT.

I'm currently evaluating some js lines using the duk_eval function

Here's my lines of code :

duk_push_string(ctx,"pouet");
duk_eval(ctx);

ctx is the base context that you provide when creating duktape heap

Using try-catch doesn't catch anything

Any idea?

Thanks in advance

Paul
  • 26,170
  • 12
  • 85
  • 119
Lordrem
  • 133
  • 7
  • Someone might have an idea if you provided some code and an indication of where it segfaults. – Jim Balter Oct 17 '14 at 01:42
  • The reason i asked this question is because i don't know how it works. Code in my question is totally irrelevant here as this is precisely what i'm asking for. But anyway i added some. – Lordrem Oct 17 '14 at 08:36
  • No, code is not irrelevant. Where's the code that defines and sets ctx? – Jim Balter Oct 17 '14 at 08:55
  • 2
    Lordrem, I'll vote to reopen this, but it helps to play along and provide more code instead of getting frustrated. Duktape does not seem to be well known on Stack Overflow, at least there was no tag for it until I created one. – Paul Oct 17 '14 at 09:18
  • Thanks a lot. Indeed the tag doesn't exist yet and i tried to create it but i don't have much reputation on this account so i didn't manage to. Thanks for the quick answer! – Lordrem Oct 17 '14 at 09:24
  • I don't know how relevant this is, as I've never used duktape. But in http://duktape.org/guide.html there is a section entitled "Error, Fatal, and Panic" that says "An ordinary error is caused by a throw statement, a duk_throw() API call (or similar), or by an internal, recoverable Duktape error. Ordinary errors can be caught with a try-catch in Ecmascript code or e.g. duk_pcall() in C code." – Paul Oct 17 '14 at 09:31
  • yep i saw that but when you look at the api, there is absolutely no explanation on how it really works or examples.. i'll spend some more hours on the topic i guess ;-) thanks for the answer anyway! (btw i think that you downvoted instead of upvoted ;-) ) – Lordrem Oct 17 '14 at 09:35
  • Well, based on the api : The default fatal error handler writes an error message to stderr and then escalates the fatal error to a panic (which, by default, abort()s the process). I'll try to look at overidding the panic function – Lordrem Oct 17 '14 at 09:42
  • FYI I have neither upvoted nor downvoted this. It has a few reopen votes that you can't see but not enough yet. Hope you are getting somewhere. – Paul Oct 17 '14 at 10:18
  • okay, after banging my head into the wall i found the solution. By default, the fata error is supposed to abort and just stop the execution and not segfault. However, on some compilators such as mine at the moment, the framework abort leads to an on purpose segfault. In order to avoid that, a new fatal function must be redefined that throws an exception which must be then catched in the C code to handle the error. At the moment, i've unknown error messages/code but i'll try to check if the stack contains the error object as it should contain the trace/file/line where the error occured. – Lordrem Oct 17 '14 at 10:33
  • I wish i could put that as the official answer to my question – Lordrem Oct 17 '14 at 10:35
  • "come on , if you've no idea about this framework, don't bother trying to answer" -- I've flagged your comment for being non-constructive. I know what ctx is, but whether you have a valid one is a legitimate question. – Jim Balter Oct 17 '14 at 18:45
  • "I wish i could put that as the official answer to my question" -- If this gets reopened, you can answer your own question and accept that answer. – Jim Balter Oct 17 '14 at 18:49
  • @JimBalter - The OP is asking for information regarding exception handling in duktape. Code would be relevant if he was asking for help solving the segfault itself, but his question was worded (which he then reiterated) to specifically regard exception handling. Either a) duktape provides such features, or b) it does not and he can reword his question to make it relevant to solving the segfault. – JSON Nov 20 '14 at 04:12
  • As regard to the segfault, I don't think a try/catch can help with a segfault. I'm not absolutely certain, but segfaults occur on the system level lower than your code, including the try/catch routine. This creates the perfect situation where you may think the try/catch isn't working right even though you may be using it perfectly as designed. – JSON Nov 20 '14 at 05:23

1 Answers1

6

You can "catch" errors during execution of JavaScript code by using the protected variant of duk_eval which is duk_peval:

duk_push_string(ctx, "syntax error=");
if (duk_peval(ctx) != 0) {
    printf("eval failed: %s\n", duk_safe_to_string(ctx, -1));
} else {
    printf("result is: %s\n", duk_safe_to_string(ctx, -1));
}
duk_pop(ctx);  /* pop result */

Do not confuse exceptions triggered by JavaScript code with C++ exceptions: Duktape is implemented in C and does not know about features provided by the C++ standard library. When using the non-protected duk_eval function variant the application is terminated by default. You can change that by assigning an own fatal handler, which in your case could throw a C++ exception if desired.

blerontin
  • 2,892
  • 5
  • 34
  • 60
  • Does duk_peval() prevent the use of C's setjump()/longjump()? More generally, given "In C++, the implementation may perform stack unwinding that destroys objects with automatic duration. If this invokes any non-trivial destructors, it causes undefined behavior.", and the prevalence of non-trivial destructors in C++ (for RAII), can Duktape really be used in C++ code? – ddevienne Mar 12 '15 at 15:14
  • I haven't digged into Duktape code but I guess it is possible that setjump/longjump is used _internally_ even by `duk_peval`. But I don't think it matters as `duk_peval` and the other protected execution interface functions will not jump out of your _outer calling scope_ and thus the C++ stack won't change. – blerontin Apr 15 '15 at 06:43