Skip to content

Commit

Permalink
Load runtime.js from bytecode (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Jul 3, 2024
1 parent d707087 commit 21b6a07
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-cars-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nxjs-runtime": patch
---

Load `runtime.js` from bytecode
5 changes: 1 addition & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ node_modules
/build
/romfs

# Generated by `qjsc`
/source/runtime.c

/?
/?.*
.vercel
15 changes: 14 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
#---------------------------------------------------------------------------------
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion apps/tests/src/switch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
2 changes: 0 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 12 additions & 12 deletions source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 21b6a07

Please sign in to comment.