forked from danleh/wasabi
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab4ab4d
commit 83f0102
Showing
15 changed files
with
2,612 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ target/ | |
# Wasabi outputs, test outputs. | ||
out/ | ||
/test-outputs/ | ||
node_modules | ||
pkg |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
members = [ | ||
"wasabi", | ||
"wasabi_wasm", | ||
"wasabi_js", | ||
"test_utilities", | ||
] | ||
resolver = "2" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
Oops, something went wrong.