简体   繁体   English

Wasm: Uncaught (in promise) TypeError: Import #0 module="env" error: module is not an object or function Promise.then (async) (anonymous) @ (index):9

[英]Wasm: Uncaught (in promise) TypeError: Import #0 module="env" error: module is not an object or function Promise.then (async) (anonymous) @ (index):9

I'm new to emscripten and find it very hard... I have obligation to work on windows because i have to test .exe versions of my apps.我是 emscripten 的新手,发现它很难......我有义务在 Windows 上工作,因为我必须测试我的应用程序的 .exe 版本。 I'm on windows 7.我在 Windows 7 上。

I can compile wasm but javascript cannot read it.我可以编译 wasm 但 javascript 无法读取它。 Here's my code.这是我的代码。

C code:代码:

char * HelloWorld ()
{
    return "Hello World !";
}

Emscripten command-line: Emscripten 命令行:

emcc hello.c -O2 -s ONLY_MY_CODE=1 -s WASM=1 -s SIDE_MODULE=1 -s EXPORTED_FUNCTIONS="['_HelloWorld']" -o hello.wasm

Wat result:结果:

(module
  (type $t0 (func (result i32)))
  (type $t1 (func))
  (import "env" "memory" (memory $env.memory 256))
  (import "env" "memoryBase" (global $env.memoryBase i32))
  (func $_HelloWorld (export "_HelloWorld") (type $t0) (result i32)
    (get_global $env.memoryBase))
  (func $__post_instantiate (export "__post_instantiate") (type $t1)
    (set_global $g1
      (i32.add
        (get_global $env.memoryBase)
        (i32.const 16)))
    (set_global $g2
      (i32.add
        (get_global $g1)
        (i32.const 5242880))))
  (global $g1 (mut i32) (i32.const 0))
  (global $g2 (mut i32) (i32.const 0))
  (data (get_global $env.memoryBase) "Hello World !"))

Javascript: Javascript:

importObject = {};

fetch('hello.wasm').then(response =>
  response.arrayBuffer()
).then(bytes =>
  WebAssembly.instantiate(bytes, importObject)
).then(results => {
  console.log("loaded");
});

Error message:错误信息:

Uncaught (in promise) TypeError: Import #0 module="env" error: module is not an object or function
Promise.then (async)
(anonymous) @ (index):9

Can you tell me what's wrong in my code ?你能告诉我我的代码有什么问题吗?

I think the challenge is this: WebAssembly isn't fully baked yet. 我认为挑战在于:WebAssembly尚未完全成熟。

The examples show their loader from what looks like an NPM package. 这些示例从看起来像NPM软件包的位置显示了它们的加载程序。 In that package, importObj is optional. 在该软件包中,importObj是可选的。

You and I are trying to use WebAssembly.instantiate() - I'm using a web worker. 您和我正在尝试使用WebAssembly.instantiate()-我正在使用网络工作者。

So we need to pass a more complete imports - specifically an env with an abort methoed. 因此,我们需要传递更完整的导入信息-特别是带有异常终止方法的环境。 Here's an example from https://webassembly.studio 's assemblyscript starter project: 这是来自https://webassembly.studio的assemblyscript入门项目的示例:

main.html main.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
</head>
<body style="background: #fff">
  <span id="container"></span>
  <script src="./main.js"></script>
</body>
</html>

main.js main.js

WebAssembly.instantiateStreaming(fetch("../out/main.wasm"), {
  main: {
    sayHello() {
      console.log("Hello from WebAssembly!");
    }
  },
  env: {
    abort(_msg, _file, line, column) {
      console.error("abort called at main.ts:" + line + ":" + column);
    }
  },
}).then(result => {
  const exports = result.instance.exports;
  document.getElementById("container").textContent = "Result: " + exports.add(19, 23);
}).catch(console.error);

main.ts 主要

declare function sayHello(): void;

sayHello();

export function add(x: i32, y: i32): i32 {
  return x + y;
}

Note the env.abort function in importObject 注意importObject中的env.abort函数

you should write importObject like this你应该像这样写 importObject

const importObject = {
  env: {
    __memory_base: 0,
    __table_base: 0,
    memory: new WebAssembly.Memory({initial: 1})
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:promise.then不是函数 - TypeError: promise.then is not a function 反应| Promise.then()上的Promise Uncaught类型错误 - React | Promise Uncaught Type Error on Promise.then() 错误: Uncaught (in promise) TypeError: doc is not a function at HTMLButtonElement。<anonymous> - Error: Uncaught (in promise) TypeError: doc is not a function at HTMLButtonElement.<anonymous> 异步模块返回 promise [object Promise] - Async module returning promise [object Promise] 动态导入组件错误-未捕获(承诺)错误:找不到模块 - Dynamic import components error - Uncaught (in promise) Error: Cannot find module React/Firebase 错误“未捕获(承诺中)TypeError:(0,_firebase__WEBPACK_IMPORTED_MODULE_1__.default)不是函数” - React/Firebase error "Uncaught (in promise) TypeError: (0 , _firebase__WEBPACK_IMPORTED_MODULE_1__.default) is not a function" v-on 处理程序中的错误(Promise/async):“TypeError:axios__WEBPACK_IMPORTED_MODULE_14___default.a.todo 不是函数” - Error in v-on handler (Promise/async): "TypeError: axios__WEBPACK_IMPORTED_MODULE_14___default.a.todo is not a function" Uncaught (in promise) TypeError: _this is not a function - Uncaught (in promise) TypeError: _this is not a function 未捕获(承诺)TypeError:$不是函数 - Uncaught (in promise) TypeError: $ is not a function 实例化 WASM 模块时出错:“模块不是 object 或函数” - Error instantiating WASM module: "module is not an object or function"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM