如何使用emscripten生成独立的webassembly [英] How to generate standalone webassembly with emscripten

查看:85
本文介绍了如何使用emscripten生成独立的webassembly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档提供了两种选择:让优化程序剥离不必要的代码,然后用您自己的.js胶水替换,或使用 SIDE_MODULE 标志.

The documentation offers two options: let optimizer strip unnecessary code and then replace the .js glue with your own, or use SIDE_MODULE flag.

这两个选项都会导致导入 memory (而不是导出),并且在 SIDE_MODULE 的情况下,还会定义大量的导入/导出.

Both options result in a memory import (instead of export), and in the case of SIDE_MODULE a ton of extra imports/exports are also defined.

将其与由 Webassembly Studio 提供的纯净输出:

Compare it to a clean output provided by Webassembly Studio:

(module
  (type $t0 (func))
  (type $t1 (func (result i32)))
  (func $__wasm_call_ctors (type $t0))
  (func $main (export "main") (type $t1) (result i32)
    i32.const 42)
  (table $T0 1 1 anyfunc)
  (memory $memory (export "memory") 2)
  (global $g0 (mut i32) (i32.const 66560))
  (global $__heap_base (export "__heap_base") i32 (i32.const 66560))
  (global $__data_end (export "__data_end") i32 (i32.const 1024)))

在这里,内存被导出,并且提供了 __ heap_base ,这使得编写我们自己的分配器变得很容易.Emscripten输出不会导出任何此类值,因此我们不知道从哪里开始分配内存.

Here, the memory is exported, and __heap_base is provided, making it easy to write our own allocator. Emscripten output does not export any such values, so we can't know where to start memory allocation.

是否可以通过 emcc 获得类似的输出?

Is it possible to get similar output with emcc?

更新:看来,静态/堆栈大小是由emcc在内部确定的,检索它们的唯一方法是解析生成的.js文件.副模块是另一种野兽,如果我实际上不需要可重定位的模块,则应该避免使用它们.

Update: it seems that static/stack sizes are determined internally by emcc, and the only way to retrieve them would be by parsing the generated .js file. Side modules are a different beast and I should probably avoid them if I don't actually need relocatable modules.

推荐答案

您可以简单地将输出选项( -o )设置为扩展名为 .wasm 的文件名.然后Emscripten将仅输出wasm文件.

You can simply set the output option (-o) to a filename with the .wasm extension. Emscripten will then output only the wasm file.

因此,如果您具有以下 test.c 文件:

So if you have the following test.c file:

#include "emscripten.h"

EMSCRIPTEN_KEEPALIVE
int add(int a, int b) {
    return a + b;
}

然后使用如下脚本编译它:

And you compile it with emscripten like this:

emcc -O3 test.c -o test.wasm

您将仅获得文件 test.wasm ,而没有额外的 .js 文件.另外,如果您使用例如反汇编 test.wasm wasm2wat ,您将获得以下WebAssembly文本:

you will only get the file test.wasm and no extra .js file. Also if you disassemble test.wasm using e.g. wasm2wat you'll get this WebAssembly text:

(module
  (type (;0;) (func))
  (type (;1;) (func (param i32 i32) (result i32)))
  (func (;0;) (type 0)
    nop)
  (func (;1;) (type 1) (param i32 i32) (result i32)
    local.get 0
    local.get 1
    i32.add)
  (memory (;0;) 256 256)
  (export "memory" (memory 0))
  (export "add" (func 1))
  (export "_start" (func 0))
  (data (;0;) (i32.const 1536) "\a0\06P"))

如您所见,这正是您从最小的webassembly二进制文件中所期望的.

as you can see it's just what you would expect from a minimum webassembly binary.

这篇关于如何使用emscripten生成独立的webassembly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆