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