简体   繁体   English

如何在后端 js 代码中使用 emscripten 生成的 cpp class?

[英]How can I use emscripten-generated cpp class in backend js code?

I am learning emscripten from official document, and I need to implement a js-called cpp function inner a class, that is referenced from this page: https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html I created a cpp class like this: I am learning emscripten from official document, and I need to implement a js-called cpp function inner a class, that is referenced from this page: https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html I created a cpp class 像这样:

#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)
        ;
}

And compile by: emcc --bind -o class_cpp.js class.cpp I can create the class instance and run its function by following way:并编译: emcc --bind -o class_cpp.js class.cpp我可以创建 class 实例并运行它的 function 通过以下方式:

<!doctype html>
<html>
  <script>
    var Module = {
      onRuntimeInitialized: function() {
        var instance = new Module.MyClass(10, "hello");
        instance.incrementX();
        console.log(instance.x); // 12
        instance.x = 20;
        console.log(instance.x); // 20
        console.log(Module.MyClass.getStringFromInstance(instance)); // "hello"
        instance.delete();
      }
    };
  </script>
  <script src="class_cpp.js"></script>
</html>

But how to call it in a nodejs backend program?但是如何在 nodejs 后端程序中调用它呢? I tried to create the instance but it reported error that MyClass is not a constructor.我试图创建实例,但它报告了MyClass不是构造函数的错误。 Anyone has any idea?有人有什么想法吗? thanks.谢谢。

As in the web version you should import your Module:在 web 版本中,您应该导入您的模块:

var Module = require('./path/to/class_cpp.js');

And then make sure that the runtime is initialized:然后确保运行时已初始化:

Module.onRuntimeInitialized = async function {
     // My code...
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将JS函数传递给Emscripten生成的代码 - Passing JS function to Emscripten-generated code 用于 Emscripten 生成的代码的 chrome 分析器中人类可读的 function 名称 - Human-readable function names in chrome profiler for Emscripten-generated code 将命令行参数传递给emscripten生成的应用程序 - Passing command-line arguments to an emscripten-generated application 如何在Emscripten生成的代码中使用C ++分配的数组? - How to use C++ allocated array in Emscripten generated code? 在浏览器中使用脚本生成的JavaScript文件使用C ++ API - Using C++ API in the browser from an emscripten-generated JavaScript file 如何在 Vue.js 中加载 emscripten 生成的模块以使用其功能? - How load an emscripten generated module in Vue.js in order to use its functions? 如何在emscripten中使用preamble.js文件? - How do I use the preamble.js file in emscripten? 如何检测 Emscripten 的 generated.js 何时完成加载 wasm,以便我可以运行调用它的 JS 函数? - How do I detect when Emscripten's generated .js finishes loading the wasm so I can run my JS functions which call it? 我可以获取简单的asm.js代码吗? 使用emscripten编译简单的c api代码时 - Can I get simple asm.js code? when simple c api code compile using emscripten 如何将用户生成的 png 的副本发送到我的后端? - How can I send a copy of a user generated png to my backend?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM