Skip to content

Commit 6173427

Browse files
Guy BedfordJakeChampion
andauthored
component: --component option for outputting a component (#359)
Co-authored-by: Jake Champion <[email protected]>
1 parent d88edc1 commit 6173427

File tree

11 files changed

+83
-7
lines changed

11 files changed

+83
-7
lines changed

.github/workflows/main.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ jobs:
9090
- uses: actions/upload-artifact@v3
9191
with:
9292
name: engine-${{ matrix.profile }}
93-
path: js-compute-runtime.wasm
93+
path: |
94+
js-compute-runtime.wasm
95+
js-compute-runtime-component.wasm
9496
9597
ensure_cargo_installs:
9698
name: Ensure that all required "cargo install" commands are run, or we have a cache hit
@@ -285,6 +287,10 @@ jobs:
285287
uses: actions/download-artifact@v3
286288
with:
287289
name: engine-release
290+
- name: Locate component resources
291+
run: |
292+
cp c-dependencies/js-compute-runtime/xqd.wit .
293+
curl -L https://github.com/bytecodealliance/preview2-prototyping/releases/download/latest/wasi_snapshot_preview1.wasm -o wasi_snapshot_preview1.wasm
288294
- run: yarn
289295
shell: bash
290296
- run: npm test

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ node_modules/
1212
/rusturl/
1313
# compiler_flags
1414
/js-compute-runtime.wasm
15+
/js-compute-runtime-component.wasm
1516
/c-dependencies/js-compute-runtime/obj
1617
tests/wpt-harness/wpt-test-runner.js
18+
/xqd.wit
19+
/wasi_snapshot_preview1.wasm
1720
wpt-runtime.wasm
1821
docs-app/bin/main.wasm
1922
docs-app/pkg/*.tar.gz

c-dependencies/js-compute-runtime/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
/rusturl
66
/compiler_flags
7-
/*.wasm
7+
/js-compute-runtime*.wasm
8+
/obj
89

910
*.o
1011
*.d

c-dependencies/js-compute-runtime/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ DEFINES += -DMOZ_JS_STREAMS
5151

5252
.PHONY: all install clean distclean
5353

54-
all: js-compute-runtime.wasm js-compute-runtime-component.wasm
54+
all: js-compute-runtime.wasm js-compute-runtime-component.wasm | wasi_snapshot_preview1.wasm
5555

5656
compiler_flags:
5757
echo '$(CXX_OPT) $(CXX_FLAGS)' | cmp -s - $@ || echo '$(CXX_OPT) $(CXX_FLAGS)' > $@
@@ -109,6 +109,9 @@ js-compute-runtime-component.wasm: $(FSM_OBJ) $(OBJ_DIR)xqd-world/xqd_world.o $(
109109
install: js-compute-runtime.wasm
110110
install -m 444 -Dt $(DESTDIR)/dist js-compute-runtime.wasm
111111

112+
wasi_snapshot_preview1.wasm:
113+
curl -L https://github.com/bytecodealliance/preview2-prototyping/releases/download/latest/wasi_snapshot_preview1.wasm -o $@
114+
112115
regenerate-world:
113116
$(WIT_BINDGEN) guest c xqd.wit --no-helpers --out-dir xqd-world
114117

Binary file not shown.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import test from 'brittle';
2+
import { getBinPath } from 'get-bin-path'
3+
import { prepareEnvironment } from '@jakechampion/cli-testing-library';
4+
import { print } from 'js-component-tools';
5+
import { resolve } from 'node:path';
6+
import { readFile } from 'node:fs/promises';
7+
8+
const cli = await getBinPath()
9+
10+
test('should create component wasm file and return zero exit code', async function (t) {
11+
const { execute, cleanup, path, writeFile, exists } = await prepareEnvironment();
12+
t.teardown(async function () {
13+
await cleanup();
14+
});
15+
16+
await writeFile('./bin/index.js', `addEventListener('fetch', function(){})`)
17+
18+
t.is(await exists('./bin/main.wasm'), false)
19+
20+
const { code, stdout, stderr } = await execute(process.execPath, cli + ' --component');
21+
22+
t.alike(stdout, []);
23+
t.alike(stderr, []);
24+
t.is(code, 0);
25+
26+
t.is(await exists('./bin/main.wasm'), true);
27+
28+
// (necessary because readFile gives a string)
29+
const wasmBuffer = await readFile(resolve(path, './bin/main.wasm'));
30+
31+
const wat = print(wasmBuffer);
32+
33+
t.is(wat.slice(0, 10), '(component');
34+
});

js-compute-runtime-cli.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { parseInputs } from './src/parseInputs.js'
44
import { printVersion } from "./src/printVersion.js";
55
import { printHelp } from "./src/printHelp.js";
66

7-
const {wasmEngine, input, output, version, help} = await parseInputs(process.argv.slice(2))
7+
const {wasmEngine, input, component, output, version, help} = await parseInputs(process.argv.slice(2))
88

99
if (version) {
1010
await printVersion();
@@ -19,4 +19,8 @@ if (version) {
1919
// and a newer version does support the platform they are using.
2020
const {compileApplicationToWasm} = await import('./src/compileApplicationToWasm.js')
2121
await compileApplicationToWasm(input, output, wasmEngine);
22+
if (component) {
23+
const {compileComponent} = await import('./src/component.js');
24+
await compileComponent(output);
25+
}
2226
}

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"files": [
2020
"types",
2121
"js-compute-runtime-cli.js",
22-
"js-compute-runtime.wasm",
22+
"*.wasm",
23+
"xqd.wit",
2324
"src",
2425
"index.d.ts",
2526
"package.json",
@@ -30,7 +31,7 @@
3031
"test": "npm run test:types && npm run test:cli",
3132
"test:cli": "brittle --bail integration-tests/cli/**.test.js",
3233
"test:types": "tsd",
33-
"build": "make -j8 -C ./c-dependencies/js-compute-runtime && cp ./c-dependencies/js-compute-runtime/js-compute-runtime.wasm ."
34+
"build": "make -j8 -C c-dependencies/js-compute-runtime && cp c-dependencies/js-compute-runtime/*.wasm c-dependencies/js-compute-runtime/xqd.wit ."
3435
},
3536
"devDependencies": {
3637
"@jakechampion/cli-testing-library": "^1.0.0",
@@ -43,6 +44,7 @@
4344
"dependencies": {
4445
"@jakechampion/wizer": "^1.6.0",
4546
"esbuild": "^0.15.16",
47+
"js-component-tools": "^0.2.2",
4648
"tree-sitter": "^0.20.1",
4749
"tree-sitter-javascript": "^0.19.0"
4850
}

src/component.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { componentNew } from 'js-component-tools';
2+
import { readFile, writeFile } from 'node:fs/promises';
3+
4+
export async function compileComponent (path) {
5+
const coreComponent = await readFile(path);
6+
const wit = await readFile(new URL('../xqd.wit', import.meta.url), 'utf8');
7+
const generatedComponent = await componentNew(coreComponent, {
8+
adapters: [['wasi_snapshot_preview1', await readFile(new URL('../wasi_snapshot_preview1.wasm', import.meta.url))]],
9+
wit
10+
});
11+
await writeFile(path, generatedComponent);
12+
}

src/parseInputs.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { tooManyEngines } from "./tooManyEngines.js";
66
export async function parseInputs(cliInputs) {
77
const __dirname = dirname(fileURLToPath(import.meta.url));
88

9+
let component = false;
910
let customEngineSet = false;
1011
let wasmEngine = join(__dirname, "../js-compute-runtime.wasm");
1112
let customInputSet = false;
@@ -27,6 +28,11 @@ export async function parseInputs(cliInputs) {
2728
case "--help": {
2829
return { help: true };
2930
}
31+
case "--component": {
32+
component = true;
33+
wasmEngine = join(__dirname, "../js-compute-runtime-component.wasm");
34+
break;
35+
}
3036
case "--engine-wasm": {
3137
if (customEngineSet) {
3238
tooManyEngines();
@@ -82,5 +88,5 @@ export async function parseInputs(cliInputs) {
8288
}
8389
}
8490
}
85-
return { wasmEngine, input, output };
91+
return { wasmEngine, component, input, output };
8692
}

0 commit comments

Comments
 (0)