How to pass objects from Javascript to C ++ using Emscripten

I want to pass a Javascript object from JS to a C ++ class / method. I have the following simple code snippet that doesn't work:

// Access JS objects in C++ like C++ objects #include "emscripten/val.h" //Causing errors ! using namespace emscripten; int main() { val Math = val::global("Math"); return Math.call("abs", -10); // returns 10 } 

I use the following to compile using emscripten in windows:

cmd> emcc Access_Objects.cpp

I get a huge error log:

 In file included from Access_Objects.cpp:2: In file included from C:\Program Files\Emscripten\emscripten\1.16.0\system\inclu de\emscripten/val.h:4: C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:2 4:5: error: unknown type name 'constexpr' constexpr bool has_unbound_type_names = true; ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:2 4:15: error: expected unqualified-id constexpr bool has_unbound_type_names = true; ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:5 6:21: error: use of undeclared identifier 'has_unbound_type_names' if (has_unbound_type_names || std::is_polymorphic<C>::value) { ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:6 7:17: error: use of undeclared identifier 'has_unbound_type_names' if (has_unbound_type_names || std::is_polymorphic<C>::value) { ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:8 2:40: error: a space is required between consecutive right angle brackets (use '> >') struct TypeID<std::unique_ptr<T>> { ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:9 8:42: error: a space is required between consecutive right angle brackets (use '> >') struct TypeID<AllowedRawPointer<T>> { ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 82:9: error: unknown type name 'constexpr' EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(char); ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 74:13: note: expanded from macro 'EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE' constexpr static WireType toWireType(const type& v) { \ ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 82:9: error: expected member name or ';' after declaration specifiers EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(char); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 74:23: note: expanded from macro 'EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE' constexpr static WireType toWireType(const type& v) { \ ~~~~~~~~~ ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 83:9: error: unknown type name 'constexpr' EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(signed char); ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 74:13: note: expanded from macro 'EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE' constexpr static WireType toWireType(const type& v) { \ ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 83:9: error: expected member name or ';' after declaration specifiers EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(signed char); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 74:23: note: expanded from macro 'EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE' constexpr static WireType toWireType(const type& v) { \ ~~~~~~~~~ ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 84:9: error: unknown type name 'constexpr' EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(unsigned char); ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 74:13: note: expanded from macro 'EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE' constexpr static WireType toWireType(const type& v) { \ ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 84:9: error: expected member name or ';' after declaration specifiers EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(unsigned char); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 74:23: note: expanded from macro 'EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE' constexpr static WireType toWireType(const type& v) { \ ~~~~~~~~~ ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 85:9: error: unknown type name 'constexpr' EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(signed short); ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 74:13: note: expanded from macro 'EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE' constexpr static WireType toWireType(const type& v) { \ ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 85:9: error: expected member name or ';' after declaration specifiers EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(signed short); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 74:23: note: expanded from macro 'EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE' constexpr static WireType toWireType(const type& v) { \ ~~~~~~~~~ ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 86:9: error: unknown type name 'constexpr' EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(unsigned short); ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 74:13: note: expanded from macro 'EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE' constexpr static WireType toWireType(const type& v) { \ ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 86:9: error: expected member name or ';' after declaration specifiers EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(unsigned short); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 74:23: note: expanded from macro 'EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE' constexpr static WireType toWireType(const type& v) { \ ~~~~~~~~~ ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 87:9: error: unknown type name 'constexpr' EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(signed int); ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 74:13: note: expanded from macro 'EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE' constexpr static WireType toWireType(const type& v) { \ ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 87:9: error: expected member name or ';' after declaration specifiers EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(signed int); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 74:23: note: expanded from macro 'EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE' constexpr static WireType toWireType(const type& v) { \ ~~~~~~~~~ ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 88:9: error: unknown type name 'constexpr' EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(unsigned int); ^ C:\Program Files\Emscripten\emscripten\1.16.0\system\include\emscripten/wire.h:1 74:13: note: expanded from macro 'EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE' constexpr static WireType toWireType(const type& v) { \ ^ fatal error: too many errors emitted, stopping now [-ferror-limit=] 20 errors generated. ERROR root: compiler frontend failed to generate LLVM bitcode, halting 

The error occurs in 'wire.h', which is included in 'emscripten / val.h'.

How to fix these errors? Or is there any other way to pass objects from JS to C ++?

+6
source share
2 answers

There are other ways of interacting between JS and C ++. The most stable way I've found is to create your own library by wrapping JS functions so that you can call them from C ++.

If you want to call Javascript Math.abs () from C ++, you can edit "src / library.JS". There you will find a giant map of the function names available in C ++. If you want to call your new JS_Math_abs function, you must add:

 JS_Math_abs: function(value) { return Math.abs(value); }, 

Then you must change your code in C ++ to indicate the types of input and output. Add:

 extern "C" { extern int JS_Math_abs(int); } 

Don't get me wrong, this method is more annoying than val should be (if it works fine), but it should also be more efficient in the end. When you have the main libraries that you need to wrap, you will find them very stable. For the most part, however, it is easy to use versions of C ++ functions such as abs ().

You can learn more about creating your own libraries here: http://kripken.imtqy.com/emscripten-site/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#interacting-with-code-call-javascript-from- native

+3
source

As mentioned above, you need to pass -std = C ++ 11 to the compiler using its C ++ 11

0
source

Source: https://habr.com/ru/post/971641/


All Articles