【问题标题】:How to generate import section in wasm?如何在 wasm 中生成导入部分?
【发布时间】:2018-08-23 03:33:24
【问题描述】:

当我们将一个 c 源文件编译成 wasm 时,会有很多 import "env" xxxx 部分。比如这是我的c源文件,

char message[] = "hello wasm!";
char* getMessageRef() {
      return message;
}
int getMessageLength() {
    return sizeof(message);
}

const int SIZE = 10;
int data[SIZE];

void add(int value) {
    for (int i=0; i<SIZE; i++) {
        data[i] = data[i] + value;
    }
}

int* getData() {
    return &data[0];
}

编译后会生成相关的wat文件

(module
  (type (;0;) (func (param i32 i32 i32) (result i32)))
  (type (;1;) (func (param i32) (result i32)))
  (type (;2;) (func (result i32)))
  (type (;3;) (func (param i32)))
  (type (;4;) (func (param i32 i32) (result i32)))
  (type (;5;) (func (param i32 i32)))
  (type (;6;) (func))
  (type (;7;) (func (param i32 i32 i32 i32) (result i32)))
  (import "env" "memory" (memory (;0;) 256 256))
  (import "env" "table" (table (;0;) 10 10 anyfunc))
  (import "env" "memoryBase" (global (;0;) i32))
  (import "env" "tableBase" (global (;1;) i32))
  (import "env" "DYNAMICTOP_PTR" (global (;2;) i32))
  (import "env" "tempDoublePtr" (global (;3;) i32))
  (import "env" "ABORT" (global (;4;) i32))
  (import "env" "STACKTOP" (global (;5;) i32))
  (import "env" "STACK_MAX" (global (;6;) i32))
  (import "global" "NaN" (global (;7;) f64))
  (import "global" "Infinity" (global (;8;) f64))
  (import "env" "enlargeMemory" (func (;0;) (type 2)))
  (import "env" "getTotalMemory" (func (;1;) (type 2)))
  (import "env" "abortOnCannotGrowMemory" (func (;2;) (type 2)))
  (import "env" "abortStackOverflow" (func (;3;) (type 3)))
  (import "env" "nullFunc_ii" (func (;4;) (type 3)))
  (import "env" "nullFunc_iiii" (func (;5;) (type 3)))
  (import "env" "___lock" (func (;6;) (type 3)))
  (import "env" "___setErrNo" (func (;7;) (type 3)))
  (import "env" "___syscall140" (func (;8;) (type 4)))
  (import "env" "___syscall146" (func (;9;) (type 4)))
  (import "env" "___syscall54" (func (;10;) (type 4)))
  (import "env" "___syscall6" (func (;11;) (type 4)))
  (import "env" "___unlock" (func (;12;) (type 3)))
  (import "env" "_emscripten_memcpy_big" (func (;13;) (type 0)))
  (func (;14;) (type 1) (param i32) (result i32)
...  omit plenty of lines

那么当我实例化 wasm 实例时,我应该如何自动导出这些部分?

【问题讨论】:

    标签: webassembly


    【解决方案1】:

    当使用 Emscripten 编译 C/C++ 文件时,它会在生成的 wasm 输出中添加一个轻量级运行时,这就是为什么您会看到这些不同的导入。

    为了执行,你需要设置一个合适的 JavaScript 环境来托管 wasm 模块。 Emscripten 可以为你生成这个:

    ./emcc tests/hello_world.c -o hello.html
    

    上面将生成一个hello.html 文件,其中包含相关的 JavaScript 代码,允许您执行您的模块。

    对于简单的应用程序,您可以自行创建 require 环境。这是一个例子:

      const imports = {
        env: {
          memoryBase: 0,
          tableBase: 0,
          memory: new WebAssembly.Memory({
            initial: 512
          }),
          table: new WebAssembly.Table({
            initial: 0,
            element: 'anyfunc'
          })
        }
      };
    
      const instance = new WebAssembly.Instance(module, imports);
    

    这是来自example I wrote,它使用 C / Emscripten 创建了 Mandelbrot

    【讨论】:

    • 感谢您的回复。我只是不想手动填写imports。你有想法更自动地做到这一点吗?
    • 当您使用选项生成html文件./emcc tests/hello_world.c -o hello.html时,也会创建所需的JS主机。
    • 我明白了。谢谢。我还有另一个场景,我想通过非 Web 解释器运行 wasm,比如 WAVM 或 go-interpreter/wagon。你还有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 2019-09-16
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 2022-11-26
    • 2018-07-09
    相关资源
    最近更新 更多