-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* minimal wasm MVP * Add comments for module methods * Add documentation * Change line endings to windows * Fix eslint * Update dependency * replace line ending --------- Co-authored-by: Dernbu <63487502+Dernbu@users.noreply.github.com>
- Loading branch information
1 parent
1e5e170
commit 5b3b516
Showing
5 changed files
with
145 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 |
---|---|---|
|
@@ -86,6 +86,9 @@ | |
"remote_execution": { | ||
"tabs": [] | ||
}, | ||
"wasm": { | ||
"tabs": [] | ||
}, | ||
"arcade_2d": { | ||
"tabs": [ | ||
"ArcadeTwod" | ||
|
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,87 @@ | ||
/** | ||
* WebAssembly Module for Source Academy, for developing with WebAssembly Text and Binaries. | ||
* | ||
* Examples: | ||
* Simple 'add' library: | ||
* ```wat | ||
* import {wcompile, wrun} from "wasm"; | ||
* | ||
* const program = ` | ||
* (module | ||
* (func (param f64) (param f64) (result f64) | ||
* local.get 0 | ||
* local.get 1 | ||
* f64.add) | ||
* (export "add" (func 0)) | ||
* ) | ||
* `; | ||
* | ||
* | ||
* const binary = wcompile(program); | ||
* const exports = wrun(binary); | ||
* | ||
* display(binary); | ||
* display_list(exports); | ||
* | ||
* const add_fn = head(tail(exports)); | ||
* | ||
* display(add_fn(10, 35)); | ||
* ``` | ||
* | ||
* 'Calculator Language': | ||
* ```wat | ||
* // Type your program in here! | ||
* import {wcompile, wrun} from "wasm"; | ||
* | ||
* const program = ` | ||
* (module | ||
* (func (param f64) (param f64) (result f64) | ||
* local.get 0 | ||
* local.get 1 | ||
* f64.add) | ||
* (func (param f64) (param f64) (result f64) | ||
* local.get 0 | ||
* local.get 1 | ||
* f64.sub) | ||
* (func (param f64) (param f64) (result f64) | ||
* local.get 0 | ||
* local.get 1 | ||
* f64.mul) | ||
* (func (param f64) (param f64) (result f64) | ||
* local.get 0 | ||
* local.get 1 | ||
* f64.div) | ||
* (export "add" (func 0)) | ||
* (export "sub" (func 1)) | ||
* (export "mul" (func 2)) | ||
* (export "div" (func 3)) | ||
* )`; | ||
* | ||
* | ||
* const encoding = wcompile(program); | ||
* let exports = wrun(encoding); | ||
* | ||
* display_list(exports); | ||
* | ||
* const div = head(tail(exports)); | ||
* exports = tail(tail(exports)); | ||
* const mul = head(tail(exports)); | ||
* exports = tail(tail(exports)); | ||
* const sub = head(tail(exports)); | ||
* exports = tail(tail(exports)); | ||
* const add = head(tail(exports)); | ||
* | ||
* display(div(10, -35)); | ||
* display(mul(10, 12347)); | ||
* display(sub(12347, 12347)); | ||
* display(add(10, 0)); | ||
* ``` | ||
* | ||
* | ||
* @module wasm | ||
* @author Kim Yongbeom | ||
*/ | ||
export { | ||
wcompile, | ||
wrun, | ||
} from './wabt'; |
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,23 @@ | ||
import { compile } from 'source-academy-wabt'; | ||
import { objectToLinkedList } from 'source-academy-utils'; | ||
|
||
/** | ||
* Compile a (hopefully valid) WebAssembly Text module to binary. | ||
* @param program program to compile | ||
* @returns an array of 8-bit unsigned integers. | ||
*/ | ||
export const wcompile = (program: string) => Array.from(compile(program)); | ||
|
||
/** | ||
* Run a compiled WebAssembly Binary Buffer. | ||
* @param buffer an array of 8-bit unsigned integers to run | ||
* @returns a linked list of exports that the relevant WebAssembly Module exports | ||
*/ | ||
export const wrun = (buffer: number[] | Uint8Array) => { | ||
if (buffer instanceof Array) { | ||
buffer = new Uint8Array(buffer); | ||
} | ||
|
||
const exps = new WebAssembly.Instance(new WebAssembly.Module(buffer)).exports; | ||
return objectToLinkedList(exps); | ||
}; |
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