Converting WebAssembly text format to wasm - WebAssembly 编辑

WebAssembly has an S-expression-based textual representation, an intermediate form designed to be exposed in text editors, browser developer tools, etc. This article explains a little bit about how it works, and how to use available tools to convert text format files to the .wasm assembly format.

Note: Text format files are usually saved with a .wat extension. Historically, a .wast extension was also used, however that's now used for the scripting language used by the WebAssembly testsuite.

A first look at the text format

Let’s look at a simple example of this — the following program imports a function called imported_func from a module called imports, and exports a function called exported_func:

(module
  (func $i (import "imports" "imported_func") (param i32))
  (func (export "exported_func")
    i32.const 42
    call $i
  )
)

The WebAssembly function exported_func is exported for use in our environment (e.g. the web app in which we are using our WebAssembly module). When it is called, it calls an imported JavaScript function called imported_func, which is run with the value (42) provided as a parameter.

Converting the text .wat into a binary .wasm file

Let’s have a go at converting the above .wat text representation example into .wasmassembly format.

  1. To start with, make a copy of the above listing inside a text file; call it simple.wat.
  2. We need to assemble this textual representation into the assembly language the browser actually reads before we can use it. To do this, we can use the wabt tool, which includes compilers to convert between WebAssembly’s text representation and wasm, and vice versa, plus more besides. Go to https://github.com/webassembly/wabt — follow the instructions on this page to set up the tool.
  3. Once you’ve got the tool built, add the /wabt/out/clang/Debug directory to your system PATH.
  4. Next, execute the wat2wasm program, passing it the path to the input file, followed by an -o parameter, followed by the path to the output file:
    wat2wasm simple.wat -o simple.wasm

This will convert the wasm into a file called simple.wasm, which contains the .wasm assembly code.

Note: You can also convert the assembly back into the text representation using the wasm2wat tool; for example wasm2wat simple.wasm -o text.wat.

Viewing the assembly output

Because the output file is assembly-based, it can’t be viewed in a normal text editor. However, you can view it using the wat2wasm tool’s -v option. Try this:

wat2wasm simple.wat -v

This will give you an output in your terminal in the following way:

several strings of binary with textual descriptions beside them. For example: 0000008: 01 ; section code

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:91 次

字数:5102

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文