2014-07-02 49 views
6

我想从JavaScript传递一个Javascript对象到C++类/方法。 我有以下简单的代码片段,其无法正常工作:如何使用Emscripten将对象从Javascript传递到C++

// 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 
} 

我用下面的使用emscripten在Windows上进行编译:

CMD> EMCC Access_Objects.cpp

我得到巨大的错误日志:

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 

Th e错误是由'emscripten/val.h'中包含的'wire.h'引起的。

如何解决这些错误?或 是否有任何其他方式将对象从JS传递到C++?

+0

另一种方式传递对象:http://leaningtech.com/cheerp/ – ArtemGr

+0

它使用C++ 11的功能,所以正确的命令是'em ++ -std = C++ 11 Access_Objects.cpp' –

回答

3

JS和C++之间还有其他的交互方式。我发现的最稳定的方法是通过包装JS函数来构建自己的库,以便可以从C++调用它们。

如果你想从C++调用Javascript的Math.abs(),你可以编辑“src/library.JS”。在那里你可以找到C++中可用的函数名称的巨大映射。如果你想打电话给你的新功能“JS_Math_abs”,你可以这样:

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

那么你会修改你的C++代码来指定输入和输出类型。地址:

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

不要误会我的意思,这种方法比val应为(被它正常工作)比较烦人,但它也应该在年底更有效。一旦你拥有了需要包装的主库,你会发现它非常稳定。然而,绝大多数情况下,使用像abs()这样的C++版本的函数很容易。

你可以找到更多关于这里创建自己的库:http://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#interacting-with-code-call-javascript-from-native

0

正如上面提到的,你需要通过-std = C++ 11的编译器作为其使用C++ 11

相关问题