加载wasm时出现错误“无法设置属性'widthNative'”

时间:2018-10-05 07:59:08

标签: javascript emscripten webassembly

我尝试在Web画布中流式传输wasm的输出。 但是,在加载我的Wasm的Main函数时-引发以下异常:

hello.js:10522 exception thrown: TypeError: Cannot set property 'widthNative' of undefined,TypeError: Cannot set property 'widthNative' of undefined
at Object.updateCanvasDimensions (http://192.168.0.109:8080/hello.js:5876:30)
at Object.setCanvasSize (http://192.168.0.109:8080/hello.js:5854:17)
at _emscripten_set_canvas_size (http://192.168.0.109:8080/hello.js:9590:15)
at wasm-function[764]:168
at wasm-function[1034]:1597
at wasm-function[327]:57
at wasm-function[329]:9
at wasm-function[330]:9
at Object.Module._main (http://192.168.0.109:8080/hello.js:10327:75)
at Object.callMain (http://192.168.0.109:8080/hello.js:10502:30)

我已经在生成的js文件中确定了负责欺骗的部分代码:

updateCanvasDimensions:function (canvas, wNative, hNative) {
    if (wNative && hNative) {
      canvas.widthNative = wNative;
      canvas.heightNative = hNative;
    } else {
      wNative = canvas.widthNative;
      hNative = canvas.heightNative;
    }

我怀疑编译器emcc有问题。 这是我的编译命令行:

emcc -o hello.html hello.c -O3 -s WASM=1 --shell-file html_template/shell_minimal.html -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s NO_EXIT_RUNTIME=1 --embed-file pong.c8 -s EXPORTED_FUNCTIONS=[\"_main\",\"_test_function\"] -s EXTRA_EXPORTED_RUNTIME_METHODS=[\"ccall\"]"

有什么想法吗?预先非常感谢。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 实际上,有必要在主脚本之前分配属性canvas(Module.canvas):

var Module = {
        canvas: (function() {
            var canvas = document.getElementById('canvas');
            return canvas;
            })()
    };