Skip to content

Commit

Permalink
add crate wasabi_js
Browse files Browse the repository at this point in the history
  • Loading branch information
doehyunbaek committed Aug 2, 2024
1 parent ab4ab4d commit 83f0102
Show file tree
Hide file tree
Showing 15 changed files with 2,612 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ target/
# Wasabi outputs, test outputs.
out/
/test-outputs/
node_modules
pkg
33 changes: 33 additions & 0 deletions crates/Cargo.lock

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

1 change: 1 addition & 0 deletions crates/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"wasabi",
"wasabi_wasm",
"wasabi_js",
"test_utilities",
]
resolver = "2"
Expand Down
1 change: 1 addition & 0 deletions crates/wasabi/js/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ const wireInstanceExports = function(instance) {
wireInstanceExports(instance);
});

WebAssembly.instantiate = oldInstantiate;
// FIXME Due to the added imports of __wasabi functions, host code that mutates the table
// might insert the wrong numerical index into the table.
// We could at least detect (and warn that this changes behavior), or fix it, by wrapping
Expand Down
16 changes: 16 additions & 0 deletions crates/wasabi_js/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "wasabi_js"
version = "0.3.0"
authors = ["Doehyun Baek <[email protected]>"]
edition = "2021"

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

[dependencies]
wasabi = { path = "../wasabi" }
wasabi_wasm = { path = "../wasabi_wasm" }
serde = { version = "1", features = ["derive"] }
serde-wasm-bindgen = "0.4"
wasm-bindgen = { version = "0.2.87" }
console_error_panic_hook = "0.1.7"
41 changes: 41 additions & 0 deletions crates/wasabi_js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Wasabi_js: A JavaScript binding for Wasabi

This is a small package that compiles Wasabi to Wasm and make it usable from Web and node.js.

## Prerequisites

You need [wasm-pack](https://github.com/rustwasm/wasm-pack) installed to build the package.

Follow the instructions in the link above to install wasm-pack.

## Usage

First, install required dependencies:

```bash
npm install
```

To make npm pacakge:

```bash
npm run build
```

To run some tests:

```bash
npm run test
```

To run the demo:

```bash
npm run demo
```

To instrument wasm code:

```bash
npm run instrument -- {OPTIONS} <input.wasm>
```
Binary file added crates/wasabi_js/example/add.wasm
Binary file not shown.
Binary file added crates/wasabi_js/example/global.wasm
Binary file not shown.
Binary file added crates/wasabi_js/example/hello.wasm
Binary file not shown.
38 changes: 38 additions & 0 deletions crates/wasabi_js/example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
</head>
<body>
<script type="module">
import init, { instrument_wasm } from './pkg/wasabi_js.js';
await init();
async function add() {
let buf = await fetch('./add.wasm')
.then(res => res.arrayBuffer())
const original_arr = new Uint8Array(buf);
console.log(`original wasm:`, original_arr)
const { instrumented, js } = instrument_wasm(original_arr);
const instrumented_arr = new Uint8Array(instrumented);
console.log(`instrumented wasm:`, instrumented_arr)
const res = eval(js);
const { instance } = await WebAssembly.instantiate(instrumented_arr, res);
console.log(`1 + 2 = `, instance.exports.add(1, 2))
}
async function hello() {
let buf = await fetch('./hello.wasm')
.then(res => res.arrayBuffer())
const original_arr = new Uint8Array(buf);
console.log(`original wasm:`, original_arr)
const { instrumented, js } = instrument_wasm(original_arr);
const instrumented_arr = new Uint8Array(instrumented);
console.log(`instrumented wasm:`, instrumented_arr)
const res = eval(js);
res.env = {print: console.log}
const { instance } = await WebAssembly.instantiate(instrumented_arr, res);
}
await add();
await hello();
</script>
<h1>Open Console</h1>
</body>
</html>
37 changes: 37 additions & 0 deletions crates/wasabi_js/example/wasabi.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { expect, test } from 'vitest'

const wasabi = require("../pkg");
const fs = require('fs');

// Binary from https://github.com/danleh/wasabi/blob/fe12347f3557ca1db64b33ce9a83026143fa2e3f/tutorial-pldi2019/task0/2-add/add.wat
test("add.wasm", async () => {
const { buff, res } = setup('example/add.wasm');
const { instance } = await WebAssembly.instantiate(buff, res);
expect(instance.exports.add(1, 2)).toBe(3);
});

// Binary from https://github.com/danleh/wasabi/blob/fe12347f3557ca1db64b33ce9a83026143fa2e3f/tutorial-pldi2019/task0/1-hello/hello.wat
test("hello.wasm", async () => {
const { buff, res } = setup('example/add.wasm');
const arr = new Uint8Array(buff);
const { instance } = await WebAssembly.instantiate(buff, res);
});

// Binary from https://gist.github.com/doehyunbaek/3ffa3140c41b7283bf35f34d4d9ecf64
test("global.wasm", async () => {
const { buff, res } = setup('example/global.wasm');
res.console = console
res.env = {from_js: new WebAssembly.Global({value: "i64", mutable: false}, 0n)}
const { instance } = await WebAssembly.instantiate(buff, res);
expect(instance.exports.export_global()).toBe(10n)
});


function setup(path) {
const buf = fs.readFileSync(path);
const arr = new Uint8Array(buf);
const { instrumented, js } = wasabi.instrument_wasm(arr);
const res = eval(js);
const buff = new Uint8Array(instrumented);
return { buff, res };
}
Loading

0 comments on commit 83f0102

Please sign in to comment.