Javascript找不到使用emscripten编译的导出的WASM函数

时间:2018-09-23 19:52:04

标签: c++ svg web emscripten jwasm

我有一些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不是函数

和javascript崩溃。

我尝试使用-s EXPORT_ALL = 1进行编译,但仍然找不到该函数。同样,'gol.wasm'文件似乎很小(只有12个字符)。我不确定这是否可以指示问题,但我认为可能是相对的。

1 个答案:

答案 0 :(得分:0)

在Build-Command -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]'中添加以下内容即可解决错误