Skip to content

Commit 814bdc0

Browse files
authored
add option to enable weval (#134)
1 parent 8159c87 commit 814bdc0

9 files changed

Lines changed: 131 additions & 52 deletions

File tree

.github/workflows/main.yml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ jobs:
2121
strategy:
2222
fail-fast: false
2323
matrix:
24-
os: [ubuntu-latest]
25-
runs-on: ${{ matrix.os }}
24+
config:
25+
- { os: ubuntu-latest, build: "npm run build", test: "npm run test", cacheDirectory: "build-release", cacheKeySuffix: "", enableAot: "0" }
26+
- { os: ubuntu-latest, build: "npm run build:weval", test: "npm run test:weval", cacheDirectory: "build-release-weval", cacheKeySuffix: "-weval", enableAot: "1" }
27+
runs-on: ${{ matrix.config.os }}
2628
steps:
2729
- uses: actions/checkout@v2
2830
with:
@@ -56,14 +58,14 @@ jobs:
5658
uses: actions/cache@v3
5759
id: starlingmonkey-build
5860
with:
59-
path: build-release
60-
key: engine-build-${{ steps.starlingmonkey-commit.outputs.STARLINGMONKEY_HASH }}
61+
path: ${{matrix.config.cacheDirectory}}
62+
key: engine-build${{matrix.config.cacheKeySuffix}}-${{ steps.starlingmonkey-commit.outputs.STARLINGMONKEY_HASH }}
6163

6264
- name: Build
63-
run: npm run build
64-
65+
run: ${{matrix.config.build}}
66+
6567
- name: Test
66-
run: npm run test
68+
run: ${{matrix.config.test}}
6769

6870
- name: Cache Example build
6971
uses: actions/cache@v3
@@ -72,7 +74,7 @@ jobs:
7274
key: engine-cargo-${{ hashFiles('example/src/main.rs', 'example/Cargo.lock', 'example/hello.wit') }}
7375

7476
- name: Test Example
75-
run: cd example && npm run build && ./test.sh
77+
run: cd example && ENABLE_AOT=${{matrix.config.enableAot}} npm run build && ./test.sh
7678

7779
rustfmt:
7880
name: Rustfmt

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ example/package-lock.json
1010
example/hello.component.wasm
1111
/build-debug
1212
/build-release
13+
/build-release-weval
1314
.vscode
1415
/package-lock.json

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ endif
1212
all: release
1313
debug: lib/starlingmonkey_embedding.debug.wasm lib/spidermonkey-embedding-splicer.js
1414
release: lib/starlingmonkey_embedding.wasm lib/spidermonkey-embedding-splicer.js
15+
release-weval: lib/starlingmonkey_ics.wevalcache lib/spidermonkey-embedding-splicer.js
1516

1617
lib/spidermonkey-embedding-splicer.js: target/wasm32-wasi/release/spidermonkey_embedding_splicer.wasm crates/spidermonkey-embedding-splicer/wit/spidermonkey-embedding-splicer.wit | obj lib
1718
@$(JCO) new target/wasm32-wasi/release/spidermonkey_embedding_splicer.wasm -o obj/spidermonkey-embedding-splicer.wasm --wasi-reactor
@@ -25,6 +26,14 @@ lib/starlingmonkey_embedding.wasm: StarlingMonkey/cmake/* embedding/* StarlingMo
2526
make -j16 -C build-release
2627
@cp build-release/starling.wasm/starling.wasm $@
2728

29+
lib/starlingmonkey_embedding_weval.wasm: StarlingMonkey/cmake/* embedding/* StarlingMonkey/runtime/* StarlingMonkey/builtins/* StarlingMonkey/builtins/*/* StarlingMonkey/builtins/*/*/* StarlingMonkey/include/* | lib
30+
cmake -B build-release-weval -DCMAKE_BUILD_TYPE=Release -DWEVAL=ON
31+
make -j16 -C build-release-weval
32+
@cp build-release-weval/starling.wasm/starling.wasm $@
33+
34+
lib/starlingmonkey_ics.wevalcache: lib/starlingmonkey_embedding_weval.wasm
35+
@cp build-release-weval/starling.wasm/starling-ics.wevalcache $@
36+
2837
lib/starlingmonkey_embedding.debug.wasm: StarlingMonkey/cmake/* embedding/* StarlingMonkey/runtime/* StarlingMonkey/builtins/* StarlingMonkey/builtins/*/* StarlingMonkey/builtins/*/*/* StarlingMonkey/include/* | lib
2938
cmake -B build-debug -DCMAKE_BUILD_TYPE=RelWithDebInfo
3039
make -j16 -C build-debug

example/componentize.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import { componentize } from '@bytecodealliance/componentize-js';
22
import { readFile, writeFile } from 'node:fs/promises';
33
import { resolve } from 'node:path';
44

5+
const enableAot = process.env.ENABLE_AOT == '1'
6+
57
const jsSource = await readFile('hello.js', 'utf8');
68

79
const { component } = await componentize(jsSource, {
8-
witPath: resolve('hello.wit')
10+
witPath: resolve('hello.wit'),
11+
enableAot
912
});
1013

1114
await writeFile('hello.component.wasm', component);

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,26 @@
1111
"dependencies": {
1212
"@bytecodealliance/jco": "1.4.4",
1313
"@bytecodealliance/wizer": "^7.0.4",
14+
"@cfallin/weval": "^0.2.14",
1415
"es-module-lexer": "^1.5.4"
1516
},
1617
"types": "types.d.ts",
1718
"scripts": {
1819
"build": "make release",
20+
"build:weval": "make release-weval",
1921
"build:debug": "make debug",
20-
"test": "mocha -u tdd test/test.js --timeout 120000"
22+
"test": "mocha -u tdd test/test.js --timeout 120000",
23+
"test:weval": "WEVAL_TEST=1 mocha -u tdd test/test.js --timeout 120000"
2124
},
2225
"files": [
2326
"lib/interfaces",
2427
"lib/spidermonkey-*",
2528
"lib/starlingmonkey_embedding.wasm",
29+
"lib/starlingmonkey_embedding_weval.wasm",
30+
"lib/starlingmonkey_ics.wevalcache",
2631
"src"
2732
],
2833
"workspaces": [
2934
"."
3035
]
31-
}
36+
}

src/componentize.js

Lines changed: 80 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import wizer from '@bytecodealliance/wizer';
2+
import getWeval from '@cfallin/weval';
23
import {
34
componentNew,
45
metadataAdd,
56
preview1AdapterReactorPath,
67
} from '@bytecodealliance/jco';
78
import { spawnSync } from 'node:child_process';
89
import { tmpdir } from 'node:os';
9-
import { resolve, join } from 'node:path';
10+
import { resolve, join, dirname } from 'node:path';
1011
import { readFile, writeFile, mkdir, rm } from 'node:fs/promises';
1112
import { rmSync } from 'node:fs';
1213
import { createHash } from 'node:crypto';
@@ -37,16 +38,17 @@ export async function componentize(jsSource, witWorld, opts) {
3738
}
3839
const {
3940
sourceName = 'source.js',
40-
engine = fileURLToPath(
41-
new URL(`../lib/starlingmonkey_embedding.wasm`, import.meta.url)
42-
),
4341
preview2Adapter = preview1AdapterReactorPath(),
4442
witPath,
4543
worldName,
4644
disableFeatures = [],
4745
enableFeatures = [],
46+
aotCache = fileURLToPath(new URL(`../lib/starlingmonkey_ics.wevalcache`, import.meta.url))
4847
} = opts || {};
4948

49+
const engine = opts.engine || fileURLToPath(
50+
new URL(opts.enableAot ? `../lib/starlingmonkey_embedding_weval.wasm` : `../lib/starlingmonkey_embedding.wasm`, import.meta.url));
51+
5052
await lexerInit;
5153
let jsImports = [];
5254
let jsExports = [];
@@ -181,42 +183,82 @@ export async function componentize(jsSource, witWorld, opts) {
181183
console.log('--- Wizer Env ---');
182184
console.log(env);
183185
}
184-
185-
try {
186-
let wizerProcess = spawnSync(
187-
wizer,
188-
[
189-
'--allow-wasi',
190-
'--init-func',
191-
'componentize.wizer',
192-
`--dir=${maybeWindowsPath(sourceDir)}`,
193-
`--wasm-bulk-memory=true`,
194-
'--inherit-env=true',
195-
`-o=${output}`,
196-
input,
197-
],
198-
{
199-
stdio: [null, stdout, stderr],
200-
env,
201-
input: maybeWindowsPath(
202-
join(sourceDir, sourceName.slice(0, -3) + '.bindings.js')
203-
),
204-
shell: true,
205-
encoding: 'utf-8',
186+
if (opts.enableAot) {
187+
const wevalBin = await getWeval();
188+
189+
try {
190+
let wevalProcess = spawnSync(
191+
wevalBin,
192+
[
193+
'weval',
194+
`--cache-ro ${aotCache}`,
195+
`--dir ${maybeWindowsPath(sourceDir)}`,
196+
'-w',
197+
'--init-func',
198+
'componentize.wizer',
199+
`-i ${input}`,
200+
`-o ${output}`
201+
],
202+
{
203+
stdio: [null, stdout, stderr],
204+
env,
205+
input: maybeWindowsPath(
206+
join(sourceDir, sourceName.slice(0, -3) + '.bindings.js')
207+
),
208+
shell: true,
209+
encoding: 'utf-8',
210+
}
211+
);
212+
if (wevalProcess.status !== 0)
213+
throw new Error('Wevaling failed to complete');
214+
} catch (error) {
215+
let err =
216+
`Failed to initialize the compiled Wasm binary with Weval:\n` +
217+
error.message;
218+
if (DEBUG_BINDINGS) {
219+
err += `\nBinary and sources available for debugging at ${tmpDir}\n`;
220+
} else {
221+
rmSync(tmpDir, { recursive: true });
206222
}
207-
);
208-
if (wizerProcess.status !== 0)
209-
throw new Error('Wizering failed to complete');
210-
} catch (error) {
211-
let err =
212-
`Failed to initialize the compiled Wasm binary with Wizer:\n` +
213-
error.message;
214-
if (DEBUG_BINDINGS) {
215-
err += `\nBinary and sources available for debugging at ${tmpDir}\n`;
216-
} else {
217-
rmSync(tmpDir, { recursive: true });
223+
throw new Error(err);
224+
}
225+
} else {
226+
try {
227+
let wizerProcess = spawnSync(
228+
wizer,
229+
[
230+
'--allow-wasi',
231+
'--init-func',
232+
'componentize.wizer',
233+
`--dir=${maybeWindowsPath(sourceDir)}`,
234+
`--wasm-bulk-memory=true`,
235+
'--inherit-env=true',
236+
`-o=${output}`,
237+
input,
238+
],
239+
{
240+
stdio: [null, stdout, stderr],
241+
env,
242+
input: maybeWindowsPath(
243+
join(sourceDir, sourceName.slice(0, -3) + '.bindings.js')
244+
),
245+
shell: true,
246+
encoding: 'utf-8',
247+
}
248+
);
249+
if (wizerProcess.status !== 0)
250+
throw new Error('Wizering failed to complete');
251+
} catch (error) {
252+
let err =
253+
`Failed to initialize the compiled Wasm binary with Wizer:\n` +
254+
error.message;
255+
if (DEBUG_BINDINGS) {
256+
err += `\nBinary and sources available for debugging at ${tmpDir}\n`;
257+
} else {
258+
rmSync(tmpDir, { recursive: true });
259+
}
260+
throw new Error(err);
218261
}
219-
throw new Error(err);
220262
}
221263

222264
const bin = await readFile(output);

test/builtins/now-disabled.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ export async function test(run) {
1818
strictEqual(stderr, '');
1919
const times = stdout.split('\n');
2020

21-
// verify now was taken at build time (within than 5 seconds ago)
21+
// verify now was taken at build time (within 15 seconds ago)
2222
ok(Number(times[0]) < curNow);
23-
ok(Number(times[0]) > curNow - 5000);
23+
ok(Number(times[0]) > curNow - 15_000);
2424

2525
// verify disabled time doesn't progress
2626
strictEqual(times[1], times[0]);

test/test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const DEBUG_TRACING = false;
99

1010
const builtinsCases = await readdir(new URL('./builtins', import.meta.url));
1111
suite('Builtins', () => {
12+
const enableAot = process.env.WEVAL_TEST == '1'
13+
1214
for (const filename of builtinsCases) {
1315
const name = filename.slice(0, -3);
1416
test(name, async () => {
@@ -31,6 +33,7 @@ suite('Builtins', () => {
3133
sourceName: `${name}.js`,
3234
enableFeatures,
3335
disableFeatures,
36+
enableAot
3437
}
3538
);
3639

@@ -112,6 +115,8 @@ suite('Builtins', () => {
112115

113116
const bindingsCases = await readdir(new URL('./cases', import.meta.url));
114117
suite('Bindings', () => {
118+
const enableAot = process.env.WEVAL_TEST == '1'
119+
115120
for (const name of bindingsCases) {
116121
test(name, async () => {
117122
const source = await readFile(
@@ -159,7 +164,8 @@ suite('Bindings', () => {
159164
witPath,
160165
worldName,
161166
enableFeatures,
162-
disableFeatures
167+
disableFeatures,
168+
enableAot
163169
});
164170
const map = {
165171
'wasi:cli-base/*': '@bytecodealliance/preview2-shim/cli-base#*',
@@ -221,6 +227,8 @@ suite('Bindings', () => {
221227

222228
suite('WASI', () => {
223229
test('basic app', async () => {
230+
const enableAot = process.env.WEVAL_TEST == '1'
231+
224232
const { component } = await componentize(
225233
`
226234
import { now } from 'wasi:clocks/wall-clock@0.2.0';
@@ -238,6 +246,7 @@ suite('WASI', () => {
238246
{
239247
witPath: fileURLToPath(new URL('./wit', import.meta.url)),
240248
worldName: 'test1',
249+
enableAot
241250
}
242251
);
243252

types.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ interface ComponentizeOptions {
77
* Path to custom ComponentizeJS engine build to use
88
*/
99
engine?: string,
10+
/**
11+
* Path to custom weval cache to use
12+
*/
13+
aotCache?: string,
14+
/**
15+
* Enable AoT using weval
16+
*/
17+
enableAot?: boolean,
1018
/**
1119
* Path to custom Preview2 Adapter
1220
*/

0 commit comments

Comments
 (0)