Skip to content

Commit

Permalink
add es module
Browse files Browse the repository at this point in the history
  • Loading branch information
taisukef committed Dec 28, 2022
1 parent e5b042a commit b2bcb41
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Makefile
Expand Up @@ -151,6 +151,10 @@ update-wasm2c-fac:
.PHONY: demo
demo: emscripten-release
cp out/emscripten/Release/libwabt.js docs/demo
es: #demo
echo "let f, v;" > ./WABT.js
cat docs/demo/libwabt.js >> ./WABT.js
echo "export const WABT = WabtModule;" >> ./WABT.js

# running CMake
$(foreach CONFIG,$(CONFIGS), \
Expand Down
26 changes: 25 additions & 1 deletion README.md
@@ -1,9 +1,33 @@
[![Github CI Status](https://github.com/WebAssembly/wabt/workflows/CI/badge.svg)](https://github.com/WebAssembly/wabt)

# WABT: The WebAssembly Binary Toolkit
# WABT-es: The WebAssembly Binary Toolkit as an ESmodule

WABT (we pronounce it "wabbit") is a suite of tools for WebAssembly, including:

```JavaScript
import { WABT } from "https://code4fukui.github.io/WABT-es/WABT.js";

const src = `(module
(func (export "addTwo") (param i32 i32) (result i32)
local.get 0
local.get 1
i32.add))
`;
const wabt = await WABT();
const module = wabt.parseWat("test.wast", src);
const mbin = module.toBinary({ log: true });
console.log(mbin.log);
const wasm = new WebAssembly.Module(mbin.buffer);
const instance = new WebAssembly.Instance(wasm, {});
const { addTwo } = instance.exports;
console.log(addTwo(10, 5));
```

to make WABT.js from docs/demo/libwabt.js
```sh
$ make es
```

- [**wat2wasm**](https://webassembly.github.io/wabt/doc/wat2wasm.1.html): translate from [WebAssembly text format](https://webassembly.github.io/spec/core/text/index.html) to the [WebAssembly binary format](https://webassembly.github.io/spec/core/binary/index.html)
- [**wasm2wat**](https://webassembly.github.io/wabt/doc/wasm2wat.1.html): the inverse of wat2wasm, translate from the binary format back to the text format (also known as a .wat)
- [**wasm-objdump**](https://webassembly.github.io/wabt/doc/wasm-objdump.1.html): print information about a wasm binary. Similiar to objdump.
Expand Down
16 changes: 16 additions & 0 deletions WABT.example.js
@@ -0,0 +1,16 @@
import { WABT } from "./WABT.js";

const src = `(module
(func (export "addTwo") (param i32 i32) (result i32)
local.get 0
local.get 1
i32.add))
`;
const wabt = await WABT();
const module = wabt.parseWat("test.wast", src);
const mbin = module.toBinary({ log: true });
console.log(mbin.log);
const wasm = new WebAssembly.Module(mbin.buffer);
const instance = new WebAssembly.Instance(wasm, {});
const { addTwo } = instance.exports;
console.log(addTwo(10, 5));
42 changes: 42 additions & 0 deletions WABT.js

Large diffs are not rendered by default.

61 changes: 61 additions & 0 deletions WABT.test.js
@@ -0,0 +1,61 @@
import * as t from "https://deno.land/std/testing/asserts.ts";
import { WABT } from "./WABT.js";

const wabt = await WABT(); // parseWat, readWasm

const src = `(module
(func (export "addTwo") (param i32 i32) (result i32)
local.get 0
local.get 1
i32.add))
`;
const features = { // wabt.FEATURES
exceptions: false,
mutable_globals: true,
sat_float_to_int: true,
sign_extension: true,
simd: true,
threads: true,
function_references: true,
multi_value: true,
tail_call: true,
bulk_memory: true,
reference_types: true,
annotations: true,
code_metadata: true,
gc: true,
memory64: true,
extended_const: true,
relaxed_simd: true,
};
const module = wabt.parseWat("test.wast", src, features);
module.resolveNames();
module.validate(features);
const mbin = module.toBinary({ log: true, write_debug_names: true });
const bin = mbin.buffer;
//console.log(mbin.log);

const wasm = new WebAssembly.Module(bin);
const instance = new WebAssembly.Instance(wasm, {});
const { addTwo } = instance.exports;

Deno.test("parseWat", () => {
for (let i = 0; i < 10; i++) {
t.assertEquals(addTwo(i, i), i + i);
}
});

const module2 = wabt.readWasm(bin, { readDebugNames: true });
const src2 = module2.toText({});
//console.log(src2);
const srct = `(module
(type (;0;) (func (param i32 i32) (result i32)))
(func (;0;) (type 0) (param i32 i32) (result i32)
local.get 0
local.get 1
i32.add)
(export "addTwo" (func 0)))
`;
Deno.test("readWasm", () => {
t.assertEquals(src2, srct);
});

0 comments on commit b2bcb41

Please sign in to comment.