大佬教程收集整理的这篇文章主要介绍了Emscripten WebAssembly:导出类“导入 #13 模块 =“GOT.func”错误:模块不是对象或函数”,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在尝试在项目中使用 WebAssembly 作为可导入的功能模块(希望最终在 React/Electron 应用程序中使用),所以我已经使用 Emscripten 设置了自己并开始使用 C++ .
实现函数没有问题,但我在类上没有成功。我希望能够在 Js 中公开和实例化一个类并调用它的方法,一直持续到“任务”完成。
下面是一个 example from Emscripten's own website,它构建但在尝试运行时抛出错误(可能是由于我的构建命令和 Js 导入过程)
错误信息是 [TypeError: WebAssembly.instantiate(): import #13 module="GOT.func" error: module is not an object or function]
。
我正在努力实现的目标是否合理可行?
#include <stdio.h>
#include <String>
#include <iostream>
#include <emscripten/emscripten.h>
#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;
};
// Binding code
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);
};
const util = require('util');
const fs = require('fs');
var @R_450_9016@e = fs.readfileSync('./dist/module.wasm');
const env = {
memoryBase: 0,tableBase: 0,memory: new WebAssembly.Memory({
initial: 256
}),table: new WebAssembly.table({
initial: 0,element: 'anyfunc'
}),}
var typedArray = new Uint8Array(@R_450_9016@E);
WebAssembly.instantiate(typedArray,{
env: env
}).then(result => {
console.log('MODulE: ',result.instance.exports)
}).catch(e => {
// error caught
console.log(E);
});
{
"name": "wasm-Js-test","version": "1.0.0","description": "","main": "main.Js","scripts": {
"build-test": "rm -rf ./dist && mkdir ./dist && em++ ./src/module.cpp -O2 -s WASM=1 -s SIDE_MODulE=1 --no-entry --bind -o ./dist/module.wasm && node test.Js"
},"author": "","license": "ISC"
}
您可能不想使用 -s SIDE_MODULE
。使用 SIDE_MODULE
可启用您可能不想要的 emscripten 动态链接 (PIC) ABI。
使用 --no-entry
和以 .wasm
结尾的输出文件应该足以构建尽可能独立的东西。但是请记住,emscripten 的输出实际上是设计为由 emscripten 生成的 JS 运行的,因此您可能会在尝试像这样自己加载它时遇到复杂情况。
以上是大佬教程为你收集整理的Emscripten WebAssembly:导出类“导入 #13 模块 =“GOT.func”错误:模块不是对象或函数”全部内容,希望文章能够帮你解决Emscripten WebAssembly:导出类“导入 #13 模块 =“GOT.func”错误:模块不是对象或函数”所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。