ccxvii / mujs

An embeddable Javascript interpreter in C.
http://mujs.com/
ISC License
812 stars 98 forks source link

js_try(J) isn't C++-safe: fails in the C++ compiler: no matching function for call to 'setjmp' #98

Closed yurivict closed 3 years ago

yurivict commented 5 years ago

This testcase:

#include <mujs.h>

void x() {
  js_State *J = js_newstate(0, 0, JS_STRICT);
  if (js_try(J)) {
  }
  js_endtry(J);
}

fails to compile on FreeBSD:

test.c:6:7: error: no matching function for call to 'setjmp'
  if (js_try(J)) {
      ^~~~~~~~~
/usr/local/include/mujs.h:65:2: note: expanded from macro 'js_try'
        setjmp(js_savetry(J))
        ^~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/v1/setjmp.h:40:21: note: expanded from macro 'setjmp'
#define setjmp(env) setjmp(env)
                    ^~~~~~
/usr/include/setjmp.h:57:5: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'struct _jmp_buf *' for 1st argument
int     setjmp(jmp_buf) __returns_twice;
        ^
yurivict commented 5 years ago

The rest seems to be C++-safe, but js_try is not.

ccxvii commented 5 years ago

It works for me on Linux in both GCC and clang. Which compiler version are you using?

yurivict commented 5 years ago
$ c++ --version
FreeBSD clang version 8.0.0 (tags/RELEASE_800/final 356365) (based on LLVM 8.0.0)
Target: x86_64-unknown-freebsd12.0
Thread model: posix
InstalledDir: /usr/bin
ccxvii commented 3 years ago

MuJS is a C project. There are many other things that don't work if you try to compile it as C++.

ccxvii commented 3 years ago

You can use the js_try() macro on C++ if you pass the -fpermissive flag to the C++ compiler. Unfortunately the typedef and C language voodoo for setjmp make it very difficult to return a jmp_buf with portable code that also builds as C++. If you can think of a way, I'm all ears.