Skip to content

Commit 5b3b516

Browse files
authored
WebAssembly Module (#204)
* 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 <[email protected]>
1 parent 1e5e170 commit 5b3b516

File tree

5 files changed

+145
-0
lines changed

5 files changed

+145
-0
lines changed

modules.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@
8686
"remote_execution": {
8787
"tabs": []
8888
},
89+
"wasm": {
90+
"tabs": []
91+
},
8992
"arcade_2d": {
9093
"tabs": [
9194
"ArcadeTwod"

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@
9999
"react-ace": "^10.1.0",
100100
"react-dom": "^17.0.2",
101101
"regl": "^2.1.0",
102+
"source-academy-utils": "^1.0.0",
103+
"source-academy-wabt": "^1.0.4",
102104
"tslib": "^2.3.1"
103105
},
104106
"jest": {

src/bundles/wasm/index.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* WebAssembly Module for Source Academy, for developing with WebAssembly Text and Binaries.
3+
*
4+
* Examples:
5+
* Simple 'add' library:
6+
* ```wat
7+
* import {wcompile, wrun} from "wasm";
8+
*
9+
* const program = `
10+
* (module
11+
* (func (param f64) (param f64) (result f64)
12+
* local.get 0
13+
* local.get 1
14+
* f64.add)
15+
* (export "add" (func 0))
16+
* )
17+
* `;
18+
*
19+
*
20+
* const binary = wcompile(program);
21+
* const exports = wrun(binary);
22+
*
23+
* display(binary);
24+
* display_list(exports);
25+
*
26+
* const add_fn = head(tail(exports));
27+
*
28+
* display(add_fn(10, 35));
29+
* ```
30+
*
31+
* 'Calculator Language':
32+
* ```wat
33+
* // Type your program in here!
34+
* import {wcompile, wrun} from "wasm";
35+
*
36+
* const program = `
37+
* (module
38+
* (func (param f64) (param f64) (result f64)
39+
* local.get 0
40+
* local.get 1
41+
* f64.add)
42+
* (func (param f64) (param f64) (result f64)
43+
* local.get 0
44+
* local.get 1
45+
* f64.sub)
46+
* (func (param f64) (param f64) (result f64)
47+
* local.get 0
48+
* local.get 1
49+
* f64.mul)
50+
* (func (param f64) (param f64) (result f64)
51+
* local.get 0
52+
* local.get 1
53+
* f64.div)
54+
* (export "add" (func 0))
55+
* (export "sub" (func 1))
56+
* (export "mul" (func 2))
57+
* (export "div" (func 3))
58+
* )`;
59+
*
60+
*
61+
* const encoding = wcompile(program);
62+
* let exports = wrun(encoding);
63+
*
64+
* display_list(exports);
65+
*
66+
* const div = head(tail(exports));
67+
* exports = tail(tail(exports));
68+
* const mul = head(tail(exports));
69+
* exports = tail(tail(exports));
70+
* const sub = head(tail(exports));
71+
* exports = tail(tail(exports));
72+
* const add = head(tail(exports));
73+
*
74+
* display(div(10, -35));
75+
* display(mul(10, 12347));
76+
* display(sub(12347, 12347));
77+
* display(add(10, 0));
78+
* ```
79+
*
80+
*
81+
* @module wasm
82+
* @author Kim Yongbeom
83+
*/
84+
export {
85+
wcompile,
86+
wrun,
87+
} from './wabt';

src/bundles/wasm/wabt.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { compile } from 'source-academy-wabt';
2+
import { objectToLinkedList } from 'source-academy-utils';
3+
4+
/**
5+
* Compile a (hopefully valid) WebAssembly Text module to binary.
6+
* @param program program to compile
7+
* @returns an array of 8-bit unsigned integers.
8+
*/
9+
export const wcompile = (program: string) => Array.from(compile(program));
10+
11+
/**
12+
* Run a compiled WebAssembly Binary Buffer.
13+
* @param buffer an array of 8-bit unsigned integers to run
14+
* @returns a linked list of exports that the relevant WebAssembly Module exports
15+
*/
16+
export const wrun = (buffer: number[] | Uint8Array) => {
17+
if (buffer instanceof Array) {
18+
buffer = new Uint8Array(buffer);
19+
}
20+
21+
const exps = new WebAssembly.Instance(new WebAssembly.Module(buffer)).exports;
22+
return objectToLinkedList(exps);
23+
};

yarn.lock

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,11 @@ cjs-module-lexer@^1.0.0:
17061706
resolved "https://registry.yarnpkg.com/cjs-module-lexer/-/cjs-module-lexer-1.2.2.tgz#9f84ba3244a512f3a54e5277e8eef4c489864e40"
17071707
integrity sha512-cOU9usZw8/dXIXKtwa8pM0OTJQuJkxMN6w30csNRUerHfeQ5R6U3kkU/FtJeIf3M202OHfY2U8ccInBG7/xogA==
17081708

1709+
class-transformer@^0.5.1:
1710+
version "0.5.1"
1711+
resolved "https://registry.yarnpkg.com/class-transformer/-/class-transformer-0.5.1.tgz#24147d5dffd2a6cea930a3250a677addf96ab336"
1712+
integrity sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw==
1713+
17091714
classnames@^2.3.1:
17101715
version "2.3.2"
17111716
resolved "https://registry.yarnpkg.com/classnames/-/classnames-2.3.2.tgz#351d813bf0137fcc6a76a16b88208d2560a0d924"
@@ -4938,6 +4943,11 @@ readable-stream@~2.3.6:
49384943
string_decoder "~1.1.1"
49394944
util-deprecate "~1.0.1"
49404945

4946+
reflect-metadata@^0.1.13:
4947+
version "0.1.13"
4948+
resolved "https://registry.yarnpkg.com/reflect-metadata/-/reflect-metadata-0.1.13.tgz#67ae3ca57c972a2aa1642b10fe363fe32d49dc08"
4949+
integrity sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg==
4950+
49414951
regenerator-runtime@^0.13.11:
49424952
version "0.13.11"
49434953
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz#f6dca3e7ceec20590d07ada785636a90cdca17f9"
@@ -5206,6 +5216,21 @@ socks@^2.6.2:
52065216
ip "^2.0.0"
52075217
smart-buffer "^4.2.0"
52085218

5219+
source-academy-utils@^1.0.0:
5220+
version "1.0.2"
5221+
resolved "https://registry.yarnpkg.com/source-academy-utils/-/source-academy-utils-1.0.2.tgz#fc35a4e21e6e6a14743ed560978a9701af1a8bdc"
5222+
integrity sha512-cSx/Rxr0CEOr+KJKILKicOVSVknG82fMEozaituD5mjh92przLW8C4kafzXrfGMjPVb6p7lxFMk5S6QyiYI2/g==
5223+
5224+
source-academy-wabt@^1.0.4:
5225+
version "1.0.10"
5226+
resolved "https://registry.yarnpkg.com/source-academy-wabt/-/source-academy-wabt-1.0.10.tgz#4187804a10b8233dc0f3498b49d07523940b4789"
5227+
integrity sha512-eRm9Q+wm9rNKpaX3X+ykKjcLyrV2O6elAIG3qmkuOeOLk3f26QEFfroBvNxLtvVokkItWRHek9T/d5Gqrqo5tQ==
5228+
dependencies:
5229+
class-transformer "^0.5.1"
5230+
lodash "^4.17.21"
5231+
reflect-metadata "^0.1.13"
5232+
typescript "4"
5233+
52095234
52105235
version "0.5.13"
52115236
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.13.tgz#31b24a9c2e73c2de85066c0feb7d44767ed52932"
@@ -5584,6 +5609,11 @@ typedoc@^0.23.23:
55845609
minimatch "^6.1.6"
55855610
shiki "^0.14.1"
55865611

5612+
typescript@4:
5613+
version "4.9.5"
5614+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.9.5.tgz#095979f9bcc0d09da324d58d03ce8f8374cbe65a"
5615+
integrity sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==
5616+
55875617
55885618
version "4.8.4"
55895619
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.4.tgz#c464abca159669597be5f96b8943500b238e60e6"

0 commit comments

Comments
 (0)