Open xgqfrms opened 2 years ago
https://developer.mozilla.org/zh-CN/docs/WebAssembly/Text_format_to_wasm
把
.wat
文本文件转换为.wasm
二进制文件
WebAssembly 有一个基于 S-表达式
的文本表示形式,设计为在文本编辑器,浏览器开发人员工具等中暴露的一个中间形式。
本文解释了它是如何工作的一些内容以及如何使用可用的工具把文本格式文件转换为.wasm 汇编格式文件。
备注:
文本格式文件通常被保存为 .wat
扩展名;
有时 .wast
也被使用,它是说文件包含了额外的测试命令
(断言等)并且它们不需要转换
到.wasm 中。
https://webassembly.js.org/docs/contrib-wat-vs-wast.html
https://webassembly.github.io/wabt/demo/wasm2wat/
https://webassembly.github.io/spec/core/text/index.html https://github.com/WebAssembly/spec/tree/master/interpreter#s-expression-syntax
https://github.com/xtuc/webassemblyjs/tree/master/packages/ast/src/transform
Release 2.0 (Draft 2022-09-01)
https://webassembly.github.io/spec/core/_download/WebAssembly.pdf
https://github.com/webassembly/wabt
$ wat2wasm demo.wat -o demo.wasm
https://webassembly.github.io/wabt/demo/wat2wasm/
;; https://mbebenita.github.io/WasmExplorer/
(module
(export "sqrt" (func $sqrt))
(func $sqrt
(param $num f32)
(result f32)
;; (f32.sqrt (get_local $num))
(f32.sqrt (local.get $num))
)
)
;; https://mbebenita.github.io/WasmExplorer/
(module
(export "sqrt" (func $sqrt))
(func $sqrt
(param $num f32)
(result f32)
;; (f32.sqrt (get_local $num))
(f32.sqrt (local.get $num))
)
)
0000000: 0061 736d ; WASM_BINARY_MAGIC
0000004: 0100 0000 ; WASM_BINARY_VERSION
; section "Type" (1)
0000008: 01 ; section code
0000009: 00 ; section size (guess)
000000a: 01 ; num types
; func type 0
000000b: 60 ; func
000000c: 01 ; num params
000000d: 7d ; f32
000000e: 01 ; num results
000000f: 7d ; f32
0000009: 06 ; FIXUP section size
; section "Function" (3)
0000010: 03 ; section code
0000011: 00 ; section size (guess)
0000012: 01 ; num functions
0000013: 00 ; function 0 signature index
0000011: 02 ; FIXUP section size
; section "Export" (7)
0000014: 07 ; section code
0000015: 00 ; section size (guess)
0000016: 01 ; num exports
0000017: 04 ; string length
0000018: 7371 7274 sqrt ; export name
000001c: 00 ; export kind
000001d: 00 ; export func index
0000015: 08 ; FIXUP section size
; section "Code" (10)
000001e: 0a ; section code
000001f: 00 ; section size (guess)
0000020: 01 ; num functions
; function body 0
0000021: 00 ; func body size (guess)
0000022: 00 ; local decl count
0000023: 20 ; local.get
0000024: 00 ; local index
0000025: 91 ; f32.sqrt
0000026: 0b ; end
0000021: 05 ; FIXUP func body size
000001f: 07 ; FIXUP section size
; section "name"
0000027: 00 ; section code
0000028: 00 ; section size (guess)
0000029: 04 ; string length
000002a: 6e61 6d65 name ; custom section name
000002e: 01 ; name subsection type
000002f: 00 ; subsection size (guess)
0000030: 01 ; num names
0000031: 00 ; elem index
0000032: 04 ; string length
0000033: 7371 7274 sqrt ; elem name 0
000002f: 07 ; FIXUP subsection size
0000037: 02 ; local name type
0000038: 00 ; subsection size (guess)
0000039: 01 ; num functions
000003a: 00 ; function index
000003b: 01 ; num locals
000003c: 00 ; local index
000003d: 03 ; string length
000003e: 6e75 6d num ; local name 0
0000038: 08 ; FIXUP subsection size
0000028: 18 ; FIXUP section size
// IIFE
(() => {
// const WebA_URL = `https://cdn.xgqfrms.xyz/webassembly/xgqfrms.wasm`;
// const WebA_URL = `./demo.wasm`;
const WebA_URL = `./test.wasm`;
const ThrowErrorInfo = () => {throw new Error(`fetch WASM failed!`)};
fetch(`${WebA_URL}`)
.then(res => res.ok ? res.arrayBuffer() : ThrowErrorInfo())
.then(bytes => WebAssembly.compile(bytes))
.then(module => WebAssembly.instantiate(module))
// .then(instance => window.WebAssembly.Sqrt = instance.exports.sqrt);
.then(instance => {
if(!window.WA) {
window.WA = {};
window.WA.sqrt = instance.exports.sqrt;
console.log(`window.WA.sqrt(4) =`, window.WA.sqrt(4), window.WA.sqrt(4) === 2 ? `✅` : `❌`);
}
// window.WebAssembly
const result = instance.exports.sqrt(4);
console.log(`sqrt(4)'s result =`, result, result === 2 ? `✅` : `❌`)
});
})();
// const wasmInstance = new WebAssembly.Instance(wasmModule, {});
// const { addTwo } = wasmInstance.exports;
// for (let i = 0; i < 10; i++) {
// console.log(addTwo(i, i));
// }
(module
(func (export "addTwo") (param i32 i32) (result i32)
local.get 0
local.get 1
i32.add))
0000000: 0061 736d ; WASM_BINARY_MAGIC
0000004: 0100 0000 ; WASM_BINARY_VERSION
; section "Type" (1)
0000008: 01 ; section code
0000009: 00 ; section size (guess)
000000a: 01 ; num types
; func type 0
000000b: 60 ; func
000000c: 02 ; num params
000000d: 7f ; i32
000000e: 7f ; i32
000000f: 01 ; num results
0000010: 7f ; i32
0000009: 07 ; FIXUP section size
; section "Function" (3)
0000011: 03 ; section code
0000012: 00 ; section size (guess)
0000013: 01 ; num functions
0000014: 00 ; function 0 signature index
0000012: 02 ; FIXUP section size
; section "Export" (7)
0000015: 07 ; section code
0000016: 00 ; section size (guess)
0000017: 01 ; num exports
0000018: 06 ; string length
0000019: 6164 6454 776f addTwo ; export name
000001f: 00 ; export kind
0000020: 00 ; export func index
0000016: 0a ; FIXUP section size
; section "Code" (10)
0000021: 0a ; section code
0000022: 00 ; section size (guess)
0000023: 01 ; num functions
; function body 0
0000024: 00 ; func body size (guess)
0000025: 00 ; local decl count
0000026: 20 ; local.get
0000027: 00 ; local index
0000028: 20 ; local.get
0000029: 01 ; local index
000002a: 6a ; i32.add
000002b: 0b ; end
0000024: 07 ; FIXUP func body size
0000022: 09 ; FIXUP section size
; section "name"
000002c: 00 ; section code
000002d: 00 ; section size (guess)
000002e: 04 ; string length
000002f: 6e61 6d65 name ; custom section name
0000033: 02 ; local name type
0000034: 00 ; subsection size (guess)
0000035: 01 ; num functions
0000036: 00 ; function index
0000037: 00 ; num locals
0000034: 03 ; FIXUP subsection size
000002d: 0a ; FIXUP section size
const wasmInstance = new WebAssembly.Instance(wasmModule, {});
const { addTwo } = wasmInstance.exports;
for (let i = 0; i < 10; i++) {
console.log(addTwo(i, i));
}
const wasmInstance = new WebAssembly.Instance(wasmModule, {});
const addTwo = wasmInstance.exports.addTwo;
for (let i = 0; i < 10; i++) {
console.log(addTwo(i, i));
}
wast to wasm & wat to wasm & wasm to wat
wat 是 WebAssembly 有一个基于 S-表达式的文本表示形式 wast 是用于测试的 wat, 是 wat 的超集
https://mbebenita.github.io/WasmExplorer/
https://github.com/xgqfrms/cdn/blob/gh-pages/webassembly/readme.md
https://github.com/xgqfrms/cdn/tree/gh-pages/webassembly