From 3e1ca65b4437d1dec231f0ad5f1e1123fab73b6f Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Tue, 2 Jul 2024 15:27:26 -0700 Subject: [PATCH 1/6] Load `runtime.js` from bytecode --- .changeset/plenty-cars-cheer.md | 5 +++++ .gitignore | 3 +++ Makefile | 9 ++++++++- build.sh | 2 -- source/main.c | 25 +++++++++++++------------ 5 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 .changeset/plenty-cars-cheer.md diff --git a/.changeset/plenty-cars-cheer.md b/.changeset/plenty-cars-cheer.md new file mode 100644 index 00000000..4d001a7e --- /dev/null +++ b/.changeset/plenty-cars-cheer.md @@ -0,0 +1,5 @@ +--- +"nxjs-runtime": patch +--- + +Load `runtime.js` from bytecode diff --git a/.gitignore b/.gitignore index ee54b858..8e9442e1 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,9 @@ node_modules /build /romfs +# Generated by `qjsc` +/source/runtime.c + /? /?.* .vercel diff --git a/Makefile b/Makefile index c2e8cc17..bcd3f7af 100644 --- a/Makefile +++ b/Makefile @@ -163,7 +163,14 @@ endif #--------------------------------------------------------------------------------- all: $(BUILD) -$(BUILD): +source/runtime.c: packages/runtime/runtime.js + @qjsc -o source/runtime.c packages/runtime/runtime.js + +romfs/runtime.js.map: packages/runtime/runtime.js.map + @mkdir -p romfs + @cp -v packages/runtime/runtime.js.map romfs + +$(BUILD): source/runtime.c romfs/runtime.js.map @[ -d $@ ] || mkdir -p $@ @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile diff --git a/build.sh b/build.sh index ac51acef..ba2d6235 100755 --- a/build.sh +++ b/build.sh @@ -5,8 +5,6 @@ APP="${1-hello-world}" # Build JS runtime pnpm bundle -mkdir -p romfs -cp -v ./packages/runtime/runtime.* ./romfs/ # Build packages and example app pnpm build --filter "$APP" diff --git a/source/main.c b/source/main.c index 82abf585..c40ac9f3 100644 --- a/source/main.c +++ b/source/main.c @@ -35,6 +35,10 @@ #define LOG_FILENAME "nxjs-debug.log" +// Defined in runtime.c +extern const uint32_t qjsc_runtime_size; +extern const uint8_t qjsc_runtime[]; + // Text renderer static PrintConsole *print_console = NULL; @@ -685,28 +689,25 @@ int main(int argc, char *argv[]) JS_SetPropertyStr(ctx, global_obj, "$", nx_ctx->init_obj); - size_t runtime_buffer_size; - char *runtime_path = "romfs:/runtime.js"; - char *runtime_buffer = (char *)read_file(runtime_path, &runtime_buffer_size); - if (runtime_buffer == NULL) + // Initialize runtime + JSValue runtime_init_func, runtime_init_result; + runtime_init_func = JS_ReadObject(ctx, qjsc_runtime, qjsc_runtime_size, JS_READ_OBJ_BYTECODE); + if (JS_IsException(runtime_init_func)) { - printf("%s: %s\n", strerror(errno), runtime_path); + printf("Runtime initialization failed\n"); + print_js_error(ctx); nx_ctx->had_error = 1; goto main_loop; } - - JSValue runtime_init_result = JS_Eval(ctx, runtime_buffer, runtime_buffer_size, runtime_path, JS_EVAL_TYPE_GLOBAL); + runtime_init_result = JS_EvalFunction(ctx, runtime_init_func); if (JS_IsException(runtime_init_result)) { + printf("Runtime initialization failed\n"); print_js_error(ctx); nx_ctx->had_error = 1; - } - JS_FreeValue(ctx, runtime_init_result); - free(runtime_buffer); - if (nx_ctx->had_error) - { goto main_loop; } + JS_FreeValue(ctx, runtime_init_result); // Run the user code JSValue user_code_result = JS_Eval(ctx, user_code, user_code_size, js_path, JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY); From 2708e932f4e3e64561bc084d757442485d176c7a Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 3 Jul 2024 00:56:02 -0700 Subject: [PATCH 2/6] Update docker tag to include `qjsc` --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c1225cb9..caa2c847 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,7 +14,7 @@ jobs: build: name: Build runs-on: ubuntu-latest - container: ghcr.io/tootallnate/pacman-packages@sha256:7ab73f3b328fc18e6263b59dc195b149a4ee9cf6a913115e1d31d30baa8845f0 + container: ghcr.io/tootallnate/pacman-packages@sha256:f2591b6cbdc4cd828a77940bf7e5c12267bb0a9e76f3a82fb4a9d1073e5fb333 steps: - name: Checkout Repo uses: actions/checkout@v4 From 99bd9714aa8465c5bb06448d60e45214d2551797 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 3 Jul 2024 01:21:12 -0700 Subject: [PATCH 3/6] . --- .github/workflows/ci.yml | 3 --- Makefile | 3 ++- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index caa2c847..d6d8bc9b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,9 +36,6 @@ jobs: - name: Bundle `runtime.js` run: pnpm bundle - - name: Copy `runtime.js` to RomFS - run: mkdir -p ./romfs && cp -v ./packages/runtime/runtime.js* ./romfs/ - - name: Build `nxjs.nro` run: make V=1 diff --git a/Makefile b/Makefile index bcd3f7af..67884caa 100644 --- a/Makefile +++ b/Makefile @@ -84,7 +84,7 @@ export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ export DEPSDIR := $(CURDIR)/$(BUILD) -CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) +CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) runtime.c CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) @@ -165,6 +165,7 @@ all: $(BUILD) source/runtime.c: packages/runtime/runtime.js @qjsc -o source/runtime.c packages/runtime/runtime.js + @echo "compiled 'source/runtime.c' with qjsc" romfs/runtime.js.map: packages/runtime/runtime.js.map @mkdir -p romfs From 9383b72a4966c6d2907bffb063693cba58446e39 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 3 Jul 2024 01:32:35 -0700 Subject: [PATCH 4/6] . --- Makefile | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 67884caa..5bee136b 100644 --- a/Makefile +++ b/Makefile @@ -84,11 +84,16 @@ export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ export DEPSDIR := $(CURDIR)/$(BUILD) -CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) runtime.c +CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) +# Add `runtime.c` to CFILES if necessary (i.e. on CI) +ifneq ($(filter runtime.c,$(CFILES)),runtime.c) + CFILES := $(CFILES) runtime.c +endif + #--------------------------------------------------------------------------------- # use CXX for linking C++ projects, CC for standard C #--------------------------------------------------------------------------------- @@ -163,13 +168,13 @@ endif #--------------------------------------------------------------------------------- all: $(BUILD) -source/runtime.c: packages/runtime/runtime.js - @qjsc -o source/runtime.c packages/runtime/runtime.js - @echo "compiled 'source/runtime.c' with qjsc" +$(SOURCES)/runtime.c: packages/runtime/runtime.js + @qjsc -o $(SOURCES)/runtime.c packages/runtime/runtime.js + @echo "compiled '$(SOURCES)/runtime.c' with qjsc" -romfs/runtime.js.map: packages/runtime/runtime.js.map - @mkdir -p romfs - @cp -v packages/runtime/runtime.js.map romfs +$(ROMFS)/runtime.js.map: packages/runtime/runtime.js.map + @mkdir -p $(ROMFS) + @cp -v packages/runtime/runtime.js.map $(ROMFS) $(BUILD): source/runtime.c romfs/runtime.js.map @[ -d $@ ] || mkdir -p $@ From 796da901d75128ba8b28fcd2d9658bbf1ee4b684 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 3 Jul 2024 02:37:34 -0700 Subject: [PATCH 5/6] . --- source/main.c | 1 - 1 file changed, 1 deletion(-) diff --git a/source/main.c b/source/main.c index c40ac9f3..d9c15423 100644 --- a/source/main.c +++ b/source/main.c @@ -694,7 +694,6 @@ int main(int argc, char *argv[]) runtime_init_func = JS_ReadObject(ctx, qjsc_runtime, qjsc_runtime_size, JS_READ_OBJ_BYTECODE); if (JS_IsException(runtime_init_func)) { - printf("Runtime initialization failed\n"); print_js_error(ctx); nx_ctx->had_error = 1; goto main_loop; From c032a8cd550177d5688c20f20c8513bb4a1a2ec3 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 3 Jul 2024 03:55:14 -0700 Subject: [PATCH 6/6] Fix test --- apps/tests/src/switch.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/tests/src/switch.test.ts b/apps/tests/src/switch.test.ts index bea5f74a..96d3b1f0 100644 --- a/apps/tests/src/switch.test.ts +++ b/apps/tests/src/switch.test.ts @@ -70,7 +70,7 @@ test('`Switch.resolveDns()` rejects with invalid hostname', async () => { }); test('`Switch.readFile()` works with string path', async () => { - const data = await Switch.readFile('romfs:/runtime.js'); + const data = await Switch.readFile('romfs:/runtime.js.map'); assert.ok(data instanceof ArrayBuffer); assert.ok(data.byteLength > 0); });