C++类互动的Emscripten教程给出了如何使用C函数互动一个很好的解释:https://github.com/kripken/emscripten/wiki/Interacting-with-code在Emscripten
但你如何用C++类交互:
- 调用构造函数来创建反对
- 删除OBJ
- 防止阶级死代码消除其方法
C++类互动的Emscripten教程给出了如何使用C函数互动一个很好的解释:https://github.com/kripken/emscripten/wiki/Interacting-with-code在Emscripten
但你如何用C++类交互:
检查:https://github.com/kripken/emscripten/wiki/embind
实施例:
C++代码:
#include <emscripten/bind.h>
using namespace emscripten;
class MyClass {
public:
MyClass(int x, std::string y)
: x(x)
, y(y)
{}
void incrementX() {
++x;
}
int getX() const { return x; }
void setX(int x_) { x = x_; }
static std::string getStringFromInstance(const MyClass& instance) {
return instance.y;
}
private:
int x;
std::string y;
};
EMSCRIPTEN_BINDINGS(my_class_example) {
class_<MyClass>("MyClass")
.constructor<int, std::string>()
.function("incrementX", &MyClass::incrementX)
.property("x", &MyClass::getX, &MyClass::setX)
.class_function("getStringFromInstance", &MyClass::getStringFromInstance)
;
}
JS代码:
var instance = new Module.MyClass(10, "hello");
instance.incrementX();
instance.x; // 12
instance.x = 20; // 20
Module.MyClass.getStringFromInstance(instance); // "hello"
instance.delete();
Awsome :)感谢百万:) – Mortennobel 2013-12-15 01:08:39
Embind不能在Emscripten 1.16.0上工作 – FacePalm 2014-07-08 09:08:36
我这样做的方式是创建执行必要操作的“代理”函数。例如:
class HelloWorld
{
int x;
public:
HelloWorld() { x = 0; }
~HelloWorld() {}
void setX(int v) { x = v; }
int getX() { return x; }
// ...
};
//compile using "C" linkage to avoid name obfuscation
extern "C" {
//constructor, returns a pointer to the HelloWorld object
void *HW_constructor() {
return new HelloWorld();
}
void HW_setX(HelloWorld *hw, int x) {
hw->setX(x);
}
int HW_getX(HelloWorld *hw) {
return hw->getX();
}
void HW_destructor(HelloWorld *hw) {
delete hw;
}
};
然后在JS,你必须建立自己的对象的克隆调用代理功能(烦人,我知道,但我不知道此刻一个更好的解决方案):
// get references to the exposed proxy functions
var HW_constructor = Module.cwrap('HW_constructor', 'number', []);
var HW_destructor = Module.cwrap('HW_destructor', null, ['number']);
var HW_setX = Module.cwrap('HW_setX', null, ['number', 'number']);
var HW_getX = Module.cwrap('HW_getX', 'number', ['number']);
function HelloWorld() {
this.ptr = HW_constructor();
}
HelloWorld.prototype.destroy = function() {
HW_destructor(this.ptr);
};
HelloWorld.prototype.setX = function (x) {
HW_setX(this.ptr, x);
};
HelloWorld.prototype.getX = function() {
return HW_getX(this.ptr);
};
重要请记住,为了这个工作,你需要将以下标志添加到您的EMCC命令来告诉它不去掉代理方法为死代码(注:此处下划线是有意和重要的!):
emcc helloworld.cpp -o helloworld.js \
-s EXPORTED_FUNCTIONS="['_HW_constructor','_HW_destructor','_HW_setX','_HW_getX']"
编辑:我为乡亲尝试代码中创建一个gist。
谢谢 - 不错的工作..但我希望有一个更优雅的解决方案;-) – Mortennobel 2013-05-24 12:02:18
是的,同样在这里哈哈。让我知道如果你找到一个! :) – lakenen 2013-05-24 15:40:04
这应该被很快被'embind'解决。我想你可能会看看https://github.com/kripken/emscripten/tree/master/tests/embind,但不知道它是多么现在。 – abergmeier 2013-04-08 14:39:42
我在写回答之后看到了上面的评论。看起来现在有些关于此事的文档[这里](https://github.com/imvu/emscripten/wiki/embind)。当我有机会时,我会考虑使用'embind'。 – lakenen 2013-05-23 22:57:19
简短的原因很难,谷歌的C++名称mangling。 – meawoppl 2014-03-03 06:18:16