在Emscripten中与C ++类的交互

20

2
这个问题很快就会被 embind 解决。我认为你可以看一下 https://github.com/kripken/emscripten/tree/master/tests/embind,但不确定它的更新程度如何。 - abergmeier
在回答问题后,我看到了上面的评论。看起来现在有一些相关的文档在这里。我有机会时会研究使用embind - lakenen
这个很难,可以搜索一下C++的名称重载。 - meawoppl
2个回答

22

请查看:http://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html

示例:

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();

7
我采用的方法是创建“代理”函数来执行必要的操作。例如:
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']"

编辑:我创建了一个代码片段供大家尝试。


谢谢 - 做得很好..但我希望有一个更优雅的解决方案;-) - Mortennobel
是的,我也一样哈哈。如果你找到了,请告诉我! :) - lakenen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接