Skip to content

Commit dafa0c9

Browse files
committed
Add NightMonkey support.
NightMonkey is an ahead-of-time JS-to-Wasm compiler built on top of SpiderMonkey. This PR adds a `--nightmonkey` CLI flag that selects the NightMonkey variant of the engine, and invokes the NightMonkey compiler (which is a native binary) on a snapshotted image to AOT-compile JS bytecode into Wasm bytecode. Because the NightMonkey compiler itself is a native program, this PR incorporates binaries for four platforms (Linux/x86-64, Linux/aarch64, Windows/x86-64, macOS/aarch64) and bundles them all into the monolithic NPM package, adding ~25MiB. If we instead want to split out per-platform sub-packages and dynamically download them as needed, I am happy to do that instead.
1 parent 7df33dd commit dafa0c9

22 files changed

Lines changed: 340 additions & 9 deletions

‎.github/workflows/main.yml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ jobs:
103103
- 'release'
104104
- 'debug'
105105
- 'weval'
106+
- 'nightmonkey'
106107
steps:
107108
- uses: actions/checkout@v4
108109
with:
@@ -154,6 +155,9 @@ jobs:
154155
npm run clean
155156
npm run build:${{matrix.build-type}}
156157
158+
nightmonkey-compilers:
159+
uses: ./.github/workflows/nightmonkey-compilers.yml
160+
157161
########
158162
# Test #
159163
########
@@ -162,6 +166,7 @@ jobs:
162166
runs-on: ${{ matrix.os }}
163167
needs:
164168
- build
169+
- nightmonkey-compilers
165170
strategy:
166171
fail-fast: false
167172
matrix:
@@ -176,6 +181,7 @@ jobs:
176181
- 'release'
177182
- 'debug'
178183
- 'weval'
184+
- 'nightmonkey'
179185
steps:
180186
- uses: actions/checkout@v4
181187

@@ -200,6 +206,14 @@ jobs:
200206
lib
201207
target
202208
209+
- name: Download NightMonkey compilers
210+
if: matrix.build-type == 'nightmonkey'
211+
uses: actions/download-artifact@v4
212+
with:
213+
pattern: nightmonkey-*
214+
path: lib/nightmonkey
215+
merge-multiple: true
216+
203217
- uses: actions/setup-node@2028fbc5c25fe9cf00d9f06a71cc4710d4507903 # v6.0.0
204218
with:
205219
node-version: ${{matrix.node-version}}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
name: nightmonkey-compilers
2+
3+
# Builds the NightMonkey compiler for each supported host from the NightMonkey
4+
# revision and engine version the StarlingMonkey submodule pins, and uploads
5+
# each as an artifact named after its file in lib/nightmonkey/ (see
6+
# NIGHTMONKEY_HOST_ASSETS in src/componentize.js). The compiler is a native
7+
# binary, so unlike the engine it cannot be built once on Linux.
8+
on:
9+
workflow_call:
10+
11+
defaults:
12+
run:
13+
shell: bash
14+
15+
jobs:
16+
build:
17+
name: NightMonkey compiler (${{ matrix.asset }})
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
include:
22+
- os: ubuntu-latest
23+
asset: nightmonkey-x86_64-linux
24+
binary: nightmonkey
25+
- os: ubuntu-24.04-arm
26+
asset: nightmonkey-aarch64-linux
27+
binary: nightmonkey
28+
- os: macos-15
29+
asset: nightmonkey-aarch64-macos
30+
binary: nightmonkey
31+
- os: windows-2022
32+
asset: nightmonkey-x86_64-windows.exe
33+
binary: nightmonkey.exe
34+
runs-on: ${{ matrix.os }}
35+
steps:
36+
- uses: actions/checkout@v4
37+
with:
38+
submodules: true
39+
40+
- name: Read NightMonkey pins
41+
id: pins
42+
run: |
43+
file=StarlingMonkey/cmake/nightmonkey.cmake
44+
revision="$(awk '/^set\(NIGHTMONKEY_TAG / {gsub(/set\(NIGHTMONKEY_TAG |\)/, ""); print}' "$file")"
45+
engine="$(awk '/^set\(NIGHTMONKEY_ENGINE_VERSION / {gsub(/"/, "", $2); print $2}' "$file")"
46+
echo "revision=$revision" >> "$GITHUB_OUTPUT"
47+
echo "engine=$engine" >> "$GITHUB_OUTPUT"
48+
49+
- uses: actions/cache@v4
50+
id: cache
51+
with:
52+
key: ${{ matrix.asset }}-${{ steps.pins.outputs.revision }}-${{ steps.pins.outputs.engine }}
53+
path: ${{ matrix.asset }}
54+
55+
- uses: actions/checkout@v4
56+
if: steps.cache.outputs.cache-hit != 'true'
57+
with:
58+
repository: bytecodealliance/nightmonkey
59+
ref: ${{ steps.pins.outputs.revision }}
60+
path: nightmonkey
61+
62+
- name: Build compiler
63+
if: steps.cache.outputs.cache-hit != 'true'
64+
# Run from the StarlingMonkey tree so that rustup uses its toolchain, as
65+
# the engine build does.
66+
working-directory: StarlingMonkey
67+
run: |
68+
cargo build --release -p nightmonkey --manifest-path ../nightmonkey/Cargo.toml \
69+
--target-dir ../nightmonkey-target \
70+
--no-default-features --features "${{ steps.pins.outputs.engine }}"
71+
cp "../nightmonkey-target/release/${{ matrix.binary }}" "../${{ matrix.asset }}"
72+
73+
- name: Check compiler
74+
run: ./${{ matrix.asset }} --help
75+
76+
- uses: actions/upload-artifact@v4
77+
with:
78+
name: ${{ matrix.asset }}
79+
path: ${{ matrix.asset }}
80+
if-no-files-found: error

‎.github/workflows/release.yml‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,16 @@ jobs:
9898
export PRERELEASE_TAG=$(node scripts/semver-get-prerelease.mjs $NEXT_VERSION);
9999
echo -e "prerelease-tag=$PRERELEASE_TAG" >> $GITHUB_OUTPUT;
100100
101+
nightmonkey-compilers:
102+
needs:
103+
- meta
104+
uses: ./.github/workflows/nightmonkey-compilers.yml
105+
101106
pack-npm-release:
102107
runs-on: ubuntu-24.04
103108
needs:
104109
- meta
110+
- nightmonkey-compilers
105111
strategy:
106112
matrix:
107113
rust-version:
@@ -138,6 +144,15 @@ jobs:
138144
run: |
139145
npm install
140146
147+
# The engines are built by `npm pack` below; the NightMonkey compilers
148+
# for every host are built natively by the job above.
149+
- name: Download NightMonkey compilers
150+
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
151+
with:
152+
pattern: nightmonkey-*
153+
path: lib/nightmonkey
154+
merge-multiple: true
155+
141156
- name: Create release package
142157
working-directory: ${{ needs.meta.outputs.project-dir }}
143158
run: |

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ examples/hello-world/guest/hello.component.wasm
1111
/build-debug
1212
/build-release
1313
/build-release-weval
14+
/build-release-nightmonkey
15+
/deps
1416
.vscode
1517
/package-lock.json
1618
.idea

‎CMakeLists.txt‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ set(EMBEDDING_DEP "starling-raw.wasm")
2727
set(OUTPUT_NAME_RELEASE "starlingmonkey_embedding.wasm")
2828
set(OUTPUT_NAME_DEBUG "starlingmonkey_embedding.debug.wasm")
2929
set(OUTPUT_NAME_WEVAL "starlingmonkey_embedding_weval.wasm")
30+
set(OUTPUT_NAME_NIGHTMONKEY "starlingmonkey_embedding_nightmonkey.wasm")
3031

3132
# Set the appropriate name based on current configuration
3233
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
3334
set(OUTPUT_FILENAME ${OUTPUT_NAME_DEBUG})
3435
elseif(WEVAL)
3536
set(OUTPUT_FILENAME ${OUTPUT_NAME_WEVAL})
3637
set(EMBEDDING_DEP "starling-ics.wevalcache")
38+
elseif(NIGHTMONKEY)
39+
set(OUTPUT_FILENAME ${OUTPUT_NAME_NIGHTMONKEY})
3740
else()
3841
set(OUTPUT_FILENAME ${OUTPUT_NAME_RELEASE})
3942
endif()
@@ -47,6 +50,33 @@ add_custom_target(starlingmonkey_embedding
4750
${OUTPUT_FILENAME}
4851
)
4952

53+
# The NightMonkey compiler is a native binary that must match the engine
54+
# exactly. The package ships one per supported host (built by CI from the
55+
# NightMonkey revision StarlingMonkey pins) in lib/nightmonkey/, named as in
56+
# NIGHTMONKEY_HOST_ASSETS in src/componentize.js; a local build puts the one
57+
# it built there for the host.
58+
if(NIGHTMONKEY)
59+
set(NIGHTMONKEY_HOST_ASSET "")
60+
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64)$")
61+
set(NIGHTMONKEY_HOST_ASSET "nightmonkey-x86_64-linux")
62+
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64)$")
63+
set(NIGHTMONKEY_HOST_ASSET "nightmonkey-aarch64-linux")
64+
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "arm64")
65+
set(NIGHTMONKEY_HOST_ASSET "nightmonkey-aarch64-macos")
66+
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64)$")
67+
set(NIGHTMONKEY_HOST_ASSET "nightmonkey-x86_64-windows.exe")
68+
endif()
69+
if(NIGHTMONKEY_HOST_ASSET)
70+
add_dependencies(starlingmonkey_embedding nightmonkey_compiler)
71+
add_custom_command(TARGET starlingmonkey_embedding POST_BUILD
72+
COMMAND ${CMAKE_COMMAND} -E copy
73+
${NIGHTMONKEY_BIN}
74+
${CMAKE_CURRENT_SOURCE_DIR}/lib/nightmonkey/${NIGHTMONKEY_HOST_ASSET}
75+
VERBATIM
76+
)
77+
endif()
78+
endif()
79+
5080
if(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
5181
add_custom_command(TARGET starlingmonkey_embedding POST_BUILD
5282
COMMAND ${WASM_TOOLS_BIN} strip ${OUTPUT_FILENAME} -d ".debug_(info|loc|ranges|abbrev|line|str)" -o ${OUTPUT_FILENAME}

‎Makefile‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ all: release
1616
debug: lib/starlingmonkey_embedding.debug.wasm lib/spidermonkey-embedding-splicer.js
1717
release: lib/starlingmonkey_embedding.wasm lib/spidermonkey-embedding-splicer.js
1818
release-weval: lib/starlingmonkey_ics.wevalcache lib/spidermonkey-embedding-splicer.js
19+
release-nightmonkey: lib/starlingmonkey_embedding_nightmonkey.wasm lib/spidermonkey-embedding-splicer.js
1920

2021
lib/spidermonkey-embedding-splicer.js: Makefile target/wasm32-wasip1/release/splicer_component.wasm crates/spidermonkey-embedding-splicer/wit/spidermonkey-embedding-splicer.wit | obj lib
2122
@$(JCO) new target/wasm32-wasip1/release/splicer_component.wasm -o obj/spidermonkey-embedding-splicer.wasm --wasi-reactor
@@ -32,6 +33,12 @@ lib/starlingmonkey_embedding_weval.wasm: $(STARLINGMONKEY_DEPS) | lib
3233
cmake -B build-release-weval -DCMAKE_BUILD_TYPE=Release -DUSE_WASM_OPT=OFF -DWEVAL=ON
3334
make -j16 -C build-release-weval starlingmonkey_embedding
3435

36+
# Also puts the NightMonkey compiler this build made for the host in
37+
# lib/nightmonkey/ (CI adds those for the other hosts when packaging).
38+
lib/starlingmonkey_embedding_nightmonkey.wasm: $(STARLINGMONKEY_DEPS) | lib
39+
cmake -B build-release-nightmonkey -DCMAKE_BUILD_TYPE=Release -DNIGHTMONKEY=ON
40+
make -j16 -C build-release-nightmonkey starlingmonkey_embedding
41+
3542
lib/starlingmonkey_ics.wevalcache: lib/starlingmonkey_embedding_weval.wasm
3643
@cp build-release-weval/starling-raw.wasm/starling-ics.wevalcache $@
3744

@@ -55,6 +62,8 @@ clean:
5562
rm lib/spidermonkey-embedding-splicer.js || true
5663
rm lib/starlingmonkey_embedding.wasm || true
5764
rm lib/starlingmonkey_embedding.debug.wasm || true
65+
rm -r lib/starlingmonkey_embedding_nightmonkey.wasm lib/nightmonkey || true
5866
echo "removing cmake outputs"
5967
rm build-debug || true
6068
rm build-release || true
69+
rm build-release-nightmonkey || true

‎README.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,21 @@ AOT compilation can also be configured with the following options:
6767

6868
To use a custom (pre-downloaded) [`weval`][weval] binary, set the `wevalBin` option to the path to your desired weval binary.
6969

70+
### NightMonkey AOT Compilation
71+
72+
[NightMonkey][nightmonkey] is an ahead-of-time JS-to-Wasm compiler: after the Wizer snapshot is taken, it compiles the JS functions in the snapshot to WebAssembly, using a whole-program type analysis with dynamic guards (and falling back to the interpreter where it cannot compile).
73+
74+
To enable it, set the `enableNightmonkey: true` option or use the `--nightmonkey` CLI flag. It cannot be combined with weval AOT compilation.
75+
76+
NightMonkey compiles with a native `nightmonkey` binary that must match the engine exactly, so the package includes one for each supported host (Linux x86-64 and AArch64, macOS AArch64 and Windows x86-64), built alongside the engine. A local engine build (`npm run build:nightmonkey`) adds the one it built for the host. It can also be configured with the following options:
77+
78+
| Option | Type | Example | Description |
79+
|-------------------|------------|---------------|----------------------------------------------------------------|
80+
| `nightmonkeyBin` | `string` | `./nightmonkey` | Path to the NightMonkey compiler matching the engine (CLI: `--nightmonkey-bin`) |
81+
| `nightmonkeyArgs` | `string[]` | `['--stats']` | Extra arguments to pass to the compiler |
82+
83+
[nightmonkey]: https://github.com/bytecodealliance/nightmonkey
84+
7085
## Platform APIs
7186

7287
The following APIs are available:
@@ -242,6 +257,19 @@ export function componentize(opts: {
242257
* Use a pre-existing path to the `weval` binary, if present
243258
*/
244259
wevalBin?: string;
260+
/**
261+
* Enable AoT using NightMonkey (cannot be combined with `enableAot`)
262+
*/
263+
enableNightmonkey?: boolean;
264+
/**
265+
* Path to the NightMonkey compiler; it must match the engine. Defaults to
266+
* the one for the host that ships with the NightMonkey engine.
267+
*/
268+
nightmonkeyBin?: string;
269+
/**
270+
* Extra arguments to pass to the NightMonkey compiler (e.g. `['--stats']`)
271+
*/
272+
nightmonkeyArgs?: string[];
245273
/**
246274
* Use a pre-existing path to the `wizer` binary, if present
247275
*/

‎StarlingMonkey‎

Submodule StarlingMonkey updated 146 files

‎embedding/embedding.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#include "debugger.h"
33
#include "builtins/web/performance.h"
44
#include "js/Conversions.h"
5+
#ifdef ENABLE_JS_NIGHTMONKEY
6+
#include "runtime/NightRegistration.h"
7+
#endif
58

69
namespace builtins::web::console {
710

@@ -161,6 +164,13 @@ cabi_realloc(void *ptr, size_t orig_size, size_t org_align, size_t new_size) {
161164
__attribute__((export_name("call"))) uint32_t call(uint32_t fn_idx,
162165
void *argptr) {
163166
if (Runtime.first_call) {
167+
#ifdef ENABLE_JS_NIGHTMONKEY
168+
// Enable the AOT-compiled bodies the NightMonkey snapshot transform
169+
// added, if any; they are not dispatched to before this.
170+
if (!JS::NightActivate(Runtime.cx)) {
171+
Runtime.engine->abort("(call) unable to activate NightMonkey");
172+
}
173+
#endif
164174
content_debugger::maybe_init_debugger(Runtime.engine, true);
165175
js::ResetMathRandomSeed(Runtime.cx);
166176
Runtime.first_call = false;

‎package.json‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@
3131
"scripts": {
3232
"clean": "npm run clean:starlingmonkey",
3333
"clean:starlingmonkey": "rm -rf build-release",
34-
"build": "npm run build:release && npm run build:debug && npm run build:weval",
34+
"build": "npm run build:release && npm run build:debug && npm run build:weval && npm run build:nightmonkey",
3535
"build:release": "make release",
3636
"build:debug": "make debug",
3737
"build:weval": "make release-weval",
38+
"build:nightmonkey": "make release-nightmonkey",
3839
"test": "vitest run -c test/vitest.ts",
3940
"test:release": "vitest run -c test/vitest.ts",
4041
"test:weval": "cross-env WEVAL_TEST=1 vitest run -c test/vitest.ts",
42+
"test:nightmonkey": "cross-env NIGHTMONKEY_TEST=1 vitest run -c test/vitest.ts",
4143
"test:debug": "cross-env DEBUG_TEST=1 vitest run -c test/vitest.ts",
4244
"prepack": "node scripts/prepack.mjs"
4345
},
@@ -48,6 +50,8 @@
4850
"lib/starlingmonkey_embedding.debug.wasm",
4951
"lib/starlingmonkey_embedding_weval.wasm",
5052
"lib/starlingmonkey_ics.wevalcache",
53+
"lib/starlingmonkey_embedding_nightmonkey.wasm",
54+
"lib/nightmonkey",
5155
"src",
5256
"types.d.ts"
5357
],

0 commit comments

Comments
 (0)