尝试运行 wasm 函数时出现模块未定义错误

Module not defined error when trying to run a wasm function

本文关键字:模块 未定义 错误 试运行 wasm 函数      更新时间:2023-10-16

我正在阅读这篇关于Web汇编入门的文章。我尝试用js编写代码,但是当我运行它时,我在客户端控制台中出现此错误:

捕获的引用错误:未定义模块

我的test.cpp文件如下所示

#include <stdio.h>
#include <iostream>
using namespace std;
int test() {
return 0;
}

我的index.html文件看起来像这样

<!DOCTYPE html>
<html>
<!-- My Html Stuff -->
<script>
var testFunc = Module.cwrap(
'test',
null,
null
);
testFunc();
<script>
</html>

我的app.js文件看起来很石灰

const http = require('http')
, express = require('express')
, app = express()
, server = http.createServer(app);
server.listen(process.env.PORT || 80);
app.use(express.static(__dirname + '/views/'));

我从node app开始该过程,但是当我加载localhost时,它会在我的控制台中出现该错误。

您正在尝试调用不存在的函数(Module.cwrap()(

var testFunc = Module.cwrap(
'test',
null,
null
);
testFunc();

从代码中删除上述行将修复当前错误

我正在学习相同的教程,如果您查看上一个示例中生成的.js代码,则 module.cwrap 就是由此生成的。如果你想使用你自己的html,你需要导入生成的js或使用--shell-file标志作为模板。