c++ - Javascript 找不到用 emscripten 编译的导出 WASM 函数
问题描述
我有一些 c++ 文件,其中包含我试图编译为 wasm 的函数。出于某种原因,即使文件通过 emscripten 使用导出的函数编译为 wasm,但当我尝试使用这些函数时,我仍然会收到“TypeError:___ 不是函数”。
这是包含我要导出的所有功能的“bridge.cpp”文件:
/*
* Bridge from JS into the cellmap computation code in Wasm
*/
#include <gameoflife.h>
#include <constants.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
/**
* A struct that manages our instantiation of
* the game of life
*/
typedef struct {
cellmap *cm; /** holds the instance of the cellmap */
unsigned char *cells; /** holds the cell vals for current iteration */
} GOL_Instance;
/**
* Constructor for the game of life engine
*/
GOL_Instance *
GOL_Instance_new() {
GOL_Instance *gol;
gol = (GOL_Instance *)malloc(sizeof *gol);
if (gol) {
memset(gol, 0, sizeof *gol);
}
gol->cm = new_cellmap();
return gol;
}
/**
* Method to destroy the gol instance when the user is done
*/
void
GOL_Instance_destroy(GOL_Instance *gol) {
gol->cm->~cellmap();
delete[] gol->cells;
free(gol);
}
/**
* Method to initialize the gol instance
*/
void
GOL_Init(GOL_Instance *gol) {
gol->cm->init();
gol->cells = gol->cm->return_cells();
}
/**
* Method to run the gol instance by one step
*/
void
GOL_Step(GOL_Instance *gol) {
gol->cm->next_generation();
gol->cells = gol->cm->return_cells();
}
/**
* Method to get the cell values
*/
void
GOL_get_values(GOL_Instance *gol, int8_t *arr) {
unsigned char *c = gol->cells;
unsigned int total_cells = cellmap_width * cellmap_height;
for (unsigned int i = 0; i < total_cells; i++) {
unsigned int curr_ind = i * 8;
int8_t curr = (int8_t) *(c+i) & 0x01;
*(arr + curr_ind) = curr;
}
}
然后我使用 emscripten 编译代码:
em++ -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 \
-Igameoflife/include -Os -DNDEBUG \
-s EXPORTED_FUNCTIONS="['_GOL_Instance_new', '_GOL_Instance_destroy', '_GOL_Init', '_GOL_Step', '_GOL_get_values']" \
-o gol.js gameoflife/src/cellmap.cpp bridge.cpp
这个命令给了我文件'gol.js'和'gol.wasm',它们似乎编译没有任何错误。但是,当在我的 javascript 中,我尝试实际使用给定的功能(注意:我使用的是 SVG 文件中的函数,这就是为什么我的“href”标签位于不同的命名空间下):
<script xlink:href="gol.js"></script>
<script><![CDATA[
var wasm_loaded = false;
var gol_instance;
const cols = 96;
const rows = 96;
var cellArr = new Int8Array(cols * rows);
const box_side = 10; // boxes are 10 pixels accross
Module.onRuntimeInitialized = function() {
wasm_loaded = true;
gol_instance = Module._GOL_Instance_new();
}
.
.
.
然后,当我在本地托管页面时,在 chrome 的控制台输出中,我得到 wasm 流式编译失败:TypeError: Module._GOL_Instance_new is not a function
和 javascript 崩溃。
我尝试使用'-s EXPORT_ALL=1' 进行编译,但仍然找不到该函数。此外,“gol.wasm”文件似乎异常小(只有 12 个字符)。我不确定这是否表明存在问题,但我认为这可能是相对的。
解决方案
将以下内容添加到 Build-Command-s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]'
应该可以解决错误
推荐阅读
- javascript - 带有条件的 GAS 地图数组
- r - 如何用 NA 标记丢失的左手搭配
- python - 如何处理具有扩展用户(onetoone字段)关系django rest框架的模型的PUT请求
- c# - 如何使用 Windows 照片查看器查看二进制照片
- python-3.x - 如何使用不同形状的 DataFrame 解决“IndexError:单个位置索引器超出范围”
- python - TypeError:sort_index() 在 pandas Python 中出现意外的关键字参数“by”错误
- wordpress - 在 post_results() 过滤器之后,Wordpress 分页不考虑过滤后的帖子
- excel - 使用 mid 函数获取用户窗体的查找值?
- node.js - Puppeteer Devtools 程序化
- ios - 电容iOS同步失败,离子