Skip to content

Commit

Permalink
feat!(wat): migrate from wabt (npm) to wat (cargo) to support latest …
Browse files Browse the repository at this point in the history
…features (#1776)

feat!(wat): support latest features

This remove wabt and moves to [wat](https://crates.io/crates/wat)
which is maintained by the Bytecode Alliance.

We check the resulting wasm in so we don't depend on the rust toolchain
for building.

Co-authored-by: Andi Pieper <[email protected]>
  • Loading branch information
fiji-flo and argl authored May 27, 2024
1 parent 893f4c2 commit c7f30b6
Show file tree
Hide file tree
Showing 22 changed files with 580 additions and 41 deletions.
21 changes: 3 additions & 18 deletions editor/js/editable-wat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { EditorView } from "codemirror";
import * as featureDetector from "./editor-libs/feature-detector.js";
import mceConsole from "./editor-libs/console.js";
import * as mceEvents from "./editor-libs/events.js";
import wabtConstructor from "wabt";
import { watify } from "watify";

import "../css/editor-libs/ui-fonts.css";
import "../css/editor-libs/common.css";
Expand All @@ -22,7 +22,6 @@ import {
const execute = document.getElementById("execute") as HTMLElement;
const output = document.querySelector("#console code") as HTMLElement;
const reset = document.getElementById("reset") as HTMLElement;
const wabt = await wabtConstructor();

const tabContainer = document.getElementById("tab-container") as HTMLElement;
const tabs =
Expand Down Expand Up @@ -214,23 +213,9 @@ import {
* @returns {Blob} a blob with the newly created wasm module
*/
async function compileWat(wat: string): Promise<Blob> {
const encoder = new TextEncoder();
const watBuffer = encoder.encode(wat);
const module = wabt.parseWat("", watBuffer, {
exceptions: true,
mutable_globals: true,
sat_float_to_int: true,
sign_extension: true,
simd: true,
multi_value: true,
bulk_memory: true,
reference_types: true,
});
module.resolveNames();
module.validate();
const binary = module.toBinary({ log: true, write_debug_names: true });
const binary = await watify(wat);

const blob = new Blob([binary.buffer.buffer], { type: "application/wasm" });
const blob = new Blob([binary], { type: "application/wasm" });
return blob;
}

Expand Down
7 changes: 7 additions & 0 deletions live-examples/wat-examples/statements/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
"fileName": "loop.html",
"title": "Wat Demo: loop",
"type": "wat"
},
"return_func": {
"watExampleCode": "live-examples/wat-examples/statements/return_func.wat",
"jsExampleCode": "live-examples/wat-examples/statements/return_func.js",
"fileName": "return_func.html",
"title": "Wat Demo: Return func",
"type": "wat"
}
}
}
5 changes: 5 additions & 0 deletions live-examples/wat-examples/statements/return_func.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const url = "{%wasm-url%}";
const { instance } = await WebAssembly.instantiateStreaming(fetch(url));
const result = instance.exports.fac(5n);

console.log(result);
17 changes: 17 additions & 0 deletions live-examples/wat-examples/statements/return_func.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(module
(func $fac (export "fac") (param $x i64) (result i64)
(return_call $fac-aux (local.get $x) (i64.const 1))
)

(func $fac-aux (param $x i64) (param $r i64) (result i64)
(if (result i64) (i64.eqz (local.get $x))
(then (return (local.get $r)))
(else
(return_call $fac-aux
(i64.sub (local.get $x) (i64.const 1))
(i64.mul (local.get $x) (local.get $r))
)
)
)
)
)
35 changes: 13 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
"mini-css-extract-plugin": "^2.6.1",
"path-browserify": "^1.0.1",
"uglify-js": "^3.17.4",
"wabt": "^1.0.29",
"watify": "file:./watify/pkg",
"webpack": "5.91.0"
},
"resolutions": {
Expand Down
1 change: 1 addition & 0 deletions watify/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
173 changes: 173 additions & 0 deletions watify/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions watify/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "watify"
version = "0.0.1"
edition = "2021"
resolver = "2"
license = "MIT"

[lib]
crate-type = ["cdylib"]

[profile.release]
lto = true
opt-level = 's'

[dependencies]
wat = "1"
wasm-bindgen = "0.2"
21 changes: 21 additions & 0 deletions watify/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Florian Dieminger

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit c7f30b6

Please sign in to comment.