[英]How to compile C++ classes to .wasm files for wasmer?
I'm trying to compile a C++ library (pure code) to a .wasm file for Wasmer so I can run them universally in the server side regardless of what OS it's on.我正在尝试将 C++ 库(纯代码)编译为Wasmer的.wasm文件,这样我就可以在服务器端普遍运行它们,而不管它在什么操作系统上。 However I find rare documentation which can help me deal with C++ class.
但是,我发现很少有文档可以帮助我处理 C++ class。
For details, all helpful functions I wanted are in a C++ class named Timer , like Timer.reset()
.有关详细信息,我想要的所有有用功能都在名为Timer的 C++ class 中,例如
Timer.reset()
。 But it seems that Wasmer can only use exported functions
on it's documentation.但似乎 Wasmer 只能在其文档中使用
exported functions
。 So it is possible to use exported c++ classes like instance.exports.Timer.reset()
in Wasmer?所以可以在 Wasmer 中使用导出的 c++ 类,如
instance.exports.Timer.reset()
吗?
The main confusion is also about how to wrap this Timer
class in.wasm file.主要的困惑还在于如何将这个
Timer
class 包装在 .wasm 文件中。 I checked emscripten doc which shows this .我检查了显示这一点的 emscripten doc。 But the documentation compile them to
.js
files instead of .wasm
.但是文档将它们编译为
.js
文件而不是.wasm
。
Questions above combined, I find it hard to get clear procedures to use c++ classes in Wasmer for other programming languages.结合上述问题,我发现很难在 Wasmer 中为其他编程语言使用 c++ 类的明确程序。
I hope I've made a clear problem explanation for anyone willing to give some hints.我希望我已经为任何愿意提供一些提示的人做出了明确的问题解释。 Bests.
最好的。
or you can do "the old way" wrapping it in c calls:或者你可以用“旧方法”将它包装在 c 调用中:
#include <emscripten.h>
#include <emscripten/bind.h>
class Foo {
public:
int i;
};
class MyClass {
public:
int x;
Foo foo;
};
typedef void *MyClassHandle, *FooHandle;
MyClassHandle createMyClass() {
return static_cast<MyClassHandle>(new MyClass());
}
void destroyMyClass(MyClassHandle handle) {
delete static_cast<MyClass *>(handle);
}
FooHandle getFoo(MyClassHandle handle) {
MyClass *This = static_cast<MyClass *>(handle);
return static_cast<FooHandle>(&This->foo);
}
int getFooInt(FooHandle foo) {
return static_cast<Foo *>(foo)->i;
}
EMSCRIPTEN_BINDINGS(test) {
function("createMyClass", &createMyClass, emscripten::allow_raw_pointers());
function("destroyMyClass", &destroyMyClass, emscripten::allow_raw_pointers());
function("getFoo", &getFoo, emscripten::allow_raw_pointers());
function("getFooInt", &getFooInt, emscripten::allow_raw_pointers());
}
Check the following pull request: Enable accessing fields that are classes without copying .检查以下拉取请求: 启用无需复制即可访问属于类的字段。
Using it, you can expose C++ classes as properties using the following example (from the PR):使用它,您可以使用以下示例(来自 PR)将 C++ 类公开为属性:
class Foo {
public:
int i;
};
class MyClass {
public:
int x;
Foo foo;
int getY() const { return y; }
void setY(int y_) { y = y_; }
private:
int y;
};
EMSCRIPTEN_BINDINGS(test) {
class_<MyClass>("MyClass")
.constructor()
.property("x", &MyClass::x) // Expose a field directly.
.property("y", &MyClass::getY, &MyClass::setY) // Expose through a getter and setter.
.property("readOnlyY", &MyClass::getY) // Expose a read only property.
.property("foo", &MyClass::foo); // Expose a class field.
class_<Foo>("Foo")
.constructor()
.property("i", &Foo::i);
}
Then in Javascript, you can use the following code to use them:然后在 Javascript 中,可以使用下面的代码来使用它们:
var myClass = new Module.MyClass();
myClass.x = 10;
myClass.x; // 10
myClass.y = 20;
myClass.y; // 20
myClass.readOnlyY; // 20
// Assign directly to the inner class.
myClass.foo.i = 30;
myClass.foo.i; // 30
// Or use Foo object to assign to foo.
var foo = new Module.Foo();
foo.i = 40;
// Note: this will copy values from the foo but the objects will remain separate.
myClass.foo = foo;
myClass.foo.i; // 40
myClass.delete();
foo.delete();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.