如何使用Emscripten将对象从Javascript传递到C++
How do I pass objects from Javascript to C++ using Emscripten
我想把一个Javascript对象从JS传递回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
}
我使用以下代码在windows上使用emscripten进行编译:
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
错误是由'emscripten/val.h'中包含的'wire.h'引起的。
如何解决这些错误?或有没有其他方法可以将对象从JS传递到C++?
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-使用来自本机的代码调用javascript
如上所述,您需要将-std=c++11传递给编译器,因为它使用c++11
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 使用php或javascript从facebook相册URL中删除多余的部分
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何在JavaScript中将字符串转换为函数引用
- 模糊事件的Javascript测试
- Javascript更改图标
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 如何使用WCF服务和javascript表单post上传.doc文件
- javascript结合了数组和字典
- 这是什么 ==- javascript 运算符
- 从javascript创建一个列表
- 无法在通过jQuery的ajax加载的页面中执行javascript
- Javascript:selenium Web驱动程序isDisplayed()不工作
- 如何通过ajax刷新JSF填充的javascript变量
- 如何在Javascript中将JSon对象转换为数组
- Javascript生成的表单未提交
- 使用javascript将动态表从一个html页面打印到另一个html页
- 通过javascript重定向html传递php变量