From 4b4c3022eba5ea9c6e1213bac9fea380b9b71f05 Mon Sep 17 00:00:00 2001 From: Martin Petr Date: Wed, 3 Dec 2025 18:56:16 +0100 Subject: [PATCH] Change entrypoint function --- src/core/main.c | 48 +++++++++++++++++++++++-------------- src/scripts/build_system.sh | 2 +- src/scripts/embed_system.sh | 18 +++++++++++++- src/system/README.md | 15 ++++++++++++ src/system/src/__.ts | 10 ++++++++ src/system/src/entry.ts | 3 +++ src/system/src/index.ts | 1 - 7 files changed, 76 insertions(+), 21 deletions(-) create mode 100644 src/system/README.md create mode 100644 src/system/src/__.ts create mode 100644 src/system/src/entry.ts delete mode 100644 src/system/src/index.ts diff --git a/src/core/main.c b/src/core/main.c index 0d0e3ea..dbbf0eb 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -128,28 +128,40 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) { initKC(ctx); - JSValue val = JS_Eval(ctx, SYSTEM_PROG_JS, strlen(SYSTEM_PROG_JS), "", JS_EVAL_TYPE_GLOBAL); + JSValue val; + JSValue eval_result = JS_Eval(ctx, SYSTEM_PROG_JS, strlen(SYSTEM_PROG_JS), "", JS_EVAL_TYPE_GLOBAL); + if (JS_IsException(eval_result)) val = eval_result; + else { + JS_FreeValue(ctx, eval_result); + + JSValue global_obj = JS_GetGlobalObject(ctx); + JSValue kentry_func = JS_GetPropertyStr(ctx, global_obj, "KEntry"); + + if (JS_IsFunction(ctx, kentry_func)) val = JS_Call(ctx, kentry_func, JS_UNDEFINED, 0, NULL); + else val = JS_ThrowReferenceError(ctx, "KEntry function not found or not callable"); + + JS_FreeValue(ctx, kentry_func); + JS_FreeValue(ctx, global_obj); + } if (JS_IsException(val)) { - JSValue ex = JS_GetException(ctx); - const char *str = JS_ToCString(ctx, ex); + JSValue ex = JS_GetException(ctx); + const char *str = JS_ToCString(ctx, ex); - if (str) { - printf("!!! Kernel panic: %s !!!\n", str); - JS_FreeCString(ctx, str); - } else { - printf("!!! Kernel panic: [unknown] !!!\n"); - } - JS_FreeValue(ctx, ex); + if (str) { + printf("!!! Kernel panic: %s !!!\n", str); + JS_FreeCString(ctx, str); + } else printf("!!! Kernel panic: [unknown] !!!\n"); + + JS_FreeValue(ctx, ex); } else { - const char *str = JS_ToCString(ctx, val); - if (str) { - printf("!!! Kernel panic: %s !!!\n", str); - JS_FreeCString(ctx, str); - } else { - printf("!!! Kernel panic: [unknown] !!!\n"); - } - JS_FreeValue(ctx, val); + const char *str = JS_ToCString(ctx, val); + if (str) { + printf("!!! Kernel panic: %s !!!\n", str); + JS_FreeCString(ctx, str); + } else printf("!!! Kernel panic: [unknown] !!!\n"); + + JS_FreeValue(ctx, val); } JS_FreeContext(ctx); diff --git a/src/scripts/build_system.sh b/src/scripts/build_system.sh index 6d758bc..0575555 100755 --- a/src/scripts/build_system.sh +++ b/src/scripts/build_system.sh @@ -4,4 +4,4 @@ set -e mkdir -p out mkdir -p out/system -bun build --outdir out/system src/system/src/index.ts \ No newline at end of file +bun build --outdir out/system src/system/src/__.ts \ No newline at end of file diff --git a/src/scripts/embed_system.sh b/src/scripts/embed_system.sh index 5da5622..46321c3 100755 --- a/src/scripts/embed_system.sh +++ b/src/scripts/embed_system.sh @@ -4,9 +4,25 @@ set -e echo "#ifndef SYSTEM_PROG_H" > out/system/system_prog.h echo "#define SYSTEM_PROG_H" >> out/system/system_prog.h echo "const char* SYSTEM_PROG_JS = \\" >> out/system/system_prog.h + +remove_mode=false while IFS= read -r line; do + if [ "$remove_mode" = true ]; then + remove_mode=false + continue + fi + ESCAPED_LINE=$(echo "$line" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g') + + if echo "$line" | grep -q "___remove_next_line"; then + remove_mode=true + continue + fi + echo "\"${ESCAPED_LINE}\\n\"" >> out/system/system_prog.h -done < out/system/index.js + +done < out/system/__.js + echo ";" >> out/system/system_prog.h + echo "#endif" >> out/system/system_prog.h \ No newline at end of file diff --git a/src/system/README.md b/src/system/README.md new file mode 100644 index 0000000..6f844be --- /dev/null +++ b/src/system/README.md @@ -0,0 +1,15 @@ +# lints-system + +To install dependencies: + +```bash +bun install +``` + +To run: + +```bash +bun run src/index.ts +``` + +This project was created using `bun init` in bun v1.2.20. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime. diff --git a/src/system/src/__.ts b/src/system/src/__.ts new file mode 100644 index 0000000..c9fb983 --- /dev/null +++ b/src/system/src/__.ts @@ -0,0 +1,10 @@ +// DO NOT EDIT, REMOVE OR IMPORT THIS FILE +// This is used for bundler to not remove the KEntry function (called by C code) +// The function will be removed after bundling + +import { KEntry } from "./entry"; +declare function ___remove_next_line(): void; + +// Mark the KEntry for removal +___remove_next_line(); +KEntry(); diff --git a/src/system/src/entry.ts b/src/system/src/entry.ts new file mode 100644 index 0000000..9773f7b --- /dev/null +++ b/src/system/src/entry.ts @@ -0,0 +1,3 @@ +export function KEntry() { + kc.println("Kernel entry point reached!"); +} diff --git a/src/system/src/index.ts b/src/system/src/index.ts deleted file mode 100644 index df2480f..0000000 --- a/src/system/src/index.ts +++ /dev/null @@ -1 +0,0 @@ -kc.println("Kernel works!");