加载 wasm 时出错 "Cannot set property 'widthNative'"

Error "Cannot set property 'widthNative'" while loading wasm

我尝试在我的网络中流式传输 wasm 的输出 canvas。 但是,在加载我的 Wasm 的主要功能时 - 引发了以下异常:

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\"]"

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

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

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