Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
run: cargo fmt -- --check

- name: Run clippy
run: cargo clippy --workspace --all-targets --all-features
run: cargo clippy --locked --workspace --all-targets --all-features

#########
# Build #
Expand All @@ -59,7 +59,7 @@ jobs:
uses: actions/cache@v4
id: splicer-build
with:
key: splicer-${{ matrix.os }}-${{ hashFiles('Cargo.lock', 'crates/spidermonkey-embedding-splicer/src/**/*.rs') }}
key: splicer-${{ matrix.os }}-${{ hashFiles('Makefile', 'Cargo.lock', 'crates/spidermonkey-embedding-splicer/src/**/*.rs') }}
path: |
lib
target
Expand Down Expand Up @@ -134,7 +134,7 @@ jobs:
uses: actions/cache/restore@v4
id: splicer-build
with:
key: splicer-${{ matrix.os }}-${{ hashFiles('Cargo.lock', 'crates/spidermonkey-embedding-splicer/src/**/*.rs') }}
key: splicer-${{ matrix.os }}-${{ hashFiles('Makefile', 'Cargo.lock', 'crates/spidermonkey-embedding-splicer/src/**/*.rs') }}
path: |
lib
target
Expand Down Expand Up @@ -195,7 +195,7 @@ jobs:
uses: actions/cache/restore@v4
id: splicer-build
with:
key: splicer-${{ matrix.os }}-${{ hashFiles('Cargo.lock', 'crates/spidermonkey-embedding-splicer/src/**/*.rs') }}
key: splicer-${{ matrix.os }}-${{ hashFiles('Makefile', 'Cargo.lock', 'crates/spidermonkey-embedding-splicer/src/**/*.rs') }}
path: |
lib
target
Expand Down Expand Up @@ -239,7 +239,7 @@ jobs:
uses: actions/cache/restore@v4
id: splicer-build
with:
key: splicer-${{ matrix.os }}-${{ hashFiles('Cargo.lock', 'crates/spidermonkey-embedding-splicer/src/**/*.rs') }}
key: splicer-${{ matrix.os }}-${{ hashFiles('Makefile', 'Cargo.lock', 'crates/spidermonkey-embedding-splicer/src/**/*.rs') }}
path: |
lib
target
Expand Down
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ wit-parser = { version = "0.258.0", features = ["decoding", "serde"] }
wit-bindgen = { version = "0.54.0", features = ["macros", "async", "realloc"] }
wit-bindgen-core = { version = "0.61.1", default-features = false }

js-component-bindgen = { version = "2.11.0" }
js-component-bindgen = { version = "=2.11.0" }
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ debug: lib/starlingmonkey_embedding.debug.wasm lib/spidermonkey-embedding-splice
release: lib/starlingmonkey_embedding.wasm lib/spidermonkey-embedding-splicer.js
release-weval: lib/starlingmonkey_ics.wevalcache lib/spidermonkey-embedding-splicer.js

lib/spidermonkey-embedding-splicer.js: target/wasm32-wasip1/release/splicer_component.wasm crates/spidermonkey-embedding-splicer/wit/spidermonkey-embedding-splicer.wit | obj lib
lib/spidermonkey-embedding-splicer.js: Makefile target/wasm32-wasip1/release/splicer_component.wasm crates/spidermonkey-embedding-splicer/wit/spidermonkey-embedding-splicer.wit | obj lib
@$(JCO) new target/wasm32-wasip1/release/splicer_component.wasm -o obj/spidermonkey-embedding-splicer.wasm --wasi-reactor
@$(JCO) transpile -q --name spidermonkey-embedding-splicer obj/spidermonkey-embedding-splicer.wasm -o lib -- -O1
@$(JCO) transpile -q --instantiation async --no-wasi-shim --name spidermonkey-embedding-splicer obj/spidermonkey-embedding-splicer.wasm -o lib -- -O1

target/wasm32-wasip1/release/splicer_component.wasm: Cargo.toml crates/spidermonkey-embedding-splicer/Cargo.toml crates/spidermonkey-embedding-splicer/src/*.rs crates/splicer-component/src/*.rs
cargo build --lib --release --target wasm32-wasip1
target/wasm32-wasip1/release/splicer_component.wasm: Cargo.toml Cargo.lock crates/spidermonkey-embedding-splicer/Cargo.toml crates/spidermonkey-embedding-splicer/src/*.rs crates/splicer-component/src/*.rs
cargo build --locked --lib --release --target wasm32-wasip1

lib/starlingmonkey_embedding.wasm: $(STARLINGMONKEY_DEPS) | lib
cmake -B build-release -DCMAKE_BUILD_TYPE=Release
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ export function componentize(opts: {
*/
sourcePath: string;
/**
* Path to WIT file or folder
* Path to a WIT file or folder. Dependencies must be inside the given folder
* or, for a file, its parent folder. Subfolders are included.
*/
witPath?: string;
/**
Expand Down
2 changes: 1 addition & 1 deletion examples/hello-world/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export TEST_BINARY_PATH=$(realpath host)/target/release/wasmtime-test
# Build the JS component if not present
echo -e "[info] expecting component WASM at [$COMPONENT_WASM_PATH]...";
if [ ! -f "$COMPONENT_WASM_PATH" ]; then
cd guest && npm install && npm run build && cd ..
cd guest && npm install --no-save ../../.. && npm run build && cd ..
fi

# Build the Rust embedding test binary if not present
Expand Down
170 changes: 163 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
"import": "./src/componentize.js"
},
"devDependencies": {
"@bytecodealliance/preview2-shim": "^0.17.4",
"cross-env": "^7.0.3",
"semver": "^7.7.2",
"vitest": "^3.2.4"
},
"dependencies": {
"@bytecodealliance/jco": "^1.15.1",
"@bytecodealliance/preview2-shim": "^0.29.0",
"@bytecodealliance/weval": "^0.5.0",
"@bytecodealliance/wizer": "^10.0.0",
"es-module-lexer": "^1.6.0",
Expand Down
12 changes: 8 additions & 4 deletions src/componentize.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
preview1AdapterReactorPath,
} from '@bytecodealliance/jco';

import { splicer } from '../lib/spidermonkey-embedding-splicer.js';
import { createSplicer } from './splicer.js';

import { maybeWindowsPath } from './platform.js';

Expand Down Expand Up @@ -148,12 +148,16 @@ export async function componentize(
features.delete('fetch-event');
}

// Splice the bindigns for the given WIT world into the engine WASM
const { splicer, witPath: splicerWitPath } = await createSplicer(
witWorld == null ? witPath : undefined,
);

// Splice the bindings for the given WIT world into the engine WASM
let { wasm, jsBindings, exports, imports } = splicer.spliceBindings(
await readFile(engine),
[...features],
witWorld,
maybeWindowsPath(witPath),
splicerWitPath,
worldName,
false,
);
Expand Down Expand Up @@ -393,7 +397,7 @@ export async function componentize(
bin,
[...features],
witWorld,
maybeWindowsPath(witPath),
splicerWitPath,
worldName,
);

Expand Down
34 changes: 34 additions & 0 deletions src/splicer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { readFile, realpath, stat } from 'node:fs/promises';
import { basename, dirname } from 'node:path';
import { WASIShim } from '@bytecodealliance/preview2-shim/instantiation';

import { instantiate } from '../lib/spidermonkey-embedding-splicer.js';

// Compiled modules can be shared, but instances must retain isolated preopens.
const coreModules = new Map();

function getCoreModule(name) {
if (!coreModules.has(name)) {
coreModules.set(
name,
readFile(new URL(`../lib/${name}`, import.meta.url)).then(WebAssembly.compile),
);
}
return coreModules.get(name);
}

export async function createSplicer(witPath) {
const preopens = {};
let guestWitPath;

if (witPath != null) {
const hostPath = await realpath(witPath);
const isDirectory = (await stat(hostPath)).isDirectory();
preopens['/wit'] = isDirectory ? hostPath : dirname(hostPath);
guestWitPath = isDirectory ? '/wit' : `/wit/${basename(hostPath)}`;
}

const wasi = new WASIShim({ sandbox: { preopens } });
const { splicer } = await instantiate(getCoreModule, wasi.getImportObject());
return { splicer, witPath: guestWitPath };
}
Loading
Loading