2014-07-02 9 views
6

Ich möchte ein JavaScript-Objekt von JS nach C++ Klasse/Methode zurückgeben. Ich habe den folgenden einfachen Code-Schnipsel, die nicht funktionieren:Wie übergebe ich Objekte von Javascript zu C++ mit Emscripten

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

ich folgende verwenden mit Emscripten auf Windows zu kompilieren:

cmd> emcc Access_Objects.cpp

I erhalten Sie ein riesiges Protokoll der Fehler:

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 Der Fehler wird in 'wire.h' verursacht, der in 'emscripten/val.h' enthalten ist.

Wie behebe ich diese Fehler? Oder Gibt es eine andere Möglichkeit, Objekte von JS an C++ übergeben?

+0

Ein anderer Weg, Objekte übergeben: http://leaningtech.com/cheerp/ – ArtemGr

+0

Es verwendet C++ 11 Funktionen, so dass der richtige Befehl wäre 'em ++ -std = C++ 11 Access_Objects.cpp' –

Antwort

3

Es gibt andere Möglichkeiten, zwischen JS und C++ zu interagieren. Der stabilste Weg, den ich gefunden habe, besteht darin, eine eigene Bibliothek zu erstellen, indem ich JS-Funktionen einpacke, so dass Sie sie aus C++ aufrufen können.

Wenn Sie Javascript Math.abs() von C++ aufrufen möchten, können Sie "src/library.JS" bearbeiten. Dort finden Sie eine riesige Karte von Funktionsnamen, die in C++ verfügbar sind. Wenn Sie wollen Ihre neue Funktion „JS_Math_abs“ nennen, würden Sie hinzufügen:

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

Dann würden Sie Ihre C++ ändern Code zu Eingabe- und Ausgabetypen angeben. Hinzufügen:

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

Sie mich nicht falsch, ist diese Methode ärgerlicher als val sollte (es funktioniert einwandfrei waren), aber es sollte auch effizienter in das Ende sein. Sobald Sie die Hauptbibliotheken haben, die Sie verpackt benötigen, werden Sie feststellen, dass sie sehr stabil ist. In den meisten Fällen ist es jedoch einfach, die C++ - Versionen von Funktionen wie abs() zu verwenden.

Sie können Ihre eigenen Bibliotheken hier mehr zu erfahren über das Erstellen von: http://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#interacting-with-code-call-javascript-from-native

0

Wie oben erwähnt Sie übergeben müssen -std = C++ 11 an den Compiler als mit C++ 11

Verwandte Themen