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/.github/workflows/ci.yml b/.github/workflows/ci.yml index c1225cb9..d6d8bc9b 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 @@ -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/.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..5bee136b 100644 --- a/Makefile +++ b/Makefile @@ -89,6 +89,11 @@ 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,7 +168,15 @@ endif #--------------------------------------------------------------------------------- all: $(BUILD) -$(BUILD): +$(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) + +$(BUILD): source/runtime.c romfs/runtime.js.map @[ -d $@ ] || mkdir -p $@ @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile 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); }); 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..d9c15423 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,24 @@ 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); + 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);