Add console module

This commit is contained in:
2025-12-03 20:05:06 +01:00
parent 4b4c3022eb
commit 19e034a982
7 changed files with 91 additions and 2 deletions

View File

@@ -93,13 +93,20 @@ JSValue jsKCPrintln(JSContext *ctx, JSValueConst jsThis, int argc, JSValueConst
return JS_UNDEFINED; return JS_UNDEFINED;
} }
JSValue jsKCClearScreen(JSContext *ctx, JSValueConst jsThis, int argc, JSValueConst *argv) {
gST->ConOut->ClearScreen(gST->ConOut);
return JS_UNDEFINED;
}
void initKC(JSContext *ctx) { void initKC(JSContext *ctx) {
JSValue global = JS_GetGlobalObject(ctx); JSValue global = JS_GetGlobalObject(ctx);
JSValue kc = JS_NewObject(ctx); JSValue kc = JS_NewObject(ctx);
JS_SetPropertyStr(ctx, global, "kc", kc); JS_SetPropertyStr(ctx, global, "kc", kc);
JS_SetPropertyStr(ctx, kc, "println", JS_NewCFunction(ctx, jsKCPrintln, "println", 1)); JS_SetPropertyStr(ctx, kc, "println", JS_NewCFunction(ctx, jsKCPrintln, "println", 1));
JS_SetPropertyStr(ctx, kc, "clearScreen", JS_NewCFunction(ctx, jsKCClearScreen, "clearScreen", 0));
JS_FreeValue(ctx, global); JS_FreeValue(ctx, global);
} }

View File

@@ -1,3 +1,5 @@
import { KMain } from "./kernel";
export function KEntry() { export function KEntry() {
kc.println("Kernel entry point reached!"); KMain();
} }

View File

@@ -0,0 +1,9 @@
import { Logger } from "../lib/logger";
import { ConsoleKModule } from "./modules/console/console.kmod";
export function KMain() {
Logger.clear();
Logger.log("[Kernel] Initializing kernel...");
ConsoleKModule.init();
}

View File

@@ -0,0 +1,25 @@
import { KernelModule } from "../kmod";
export class ConsoleKModule extends KernelModule {
static instance: ConsoleKModule = new this();
constructor() {
super("console");
}
static init() {
ConsoleKModule.instance.init();
}
println(msg: string) {
if (!this.initialized) return;
kc.println(msg);
}
clear() {
if (!this.initialized) return;
kc.clearScreen();
}
}

View File

@@ -0,0 +1,20 @@
import { Logger } from "../../lib/logger";
export class KernelModule {
#name: string;
#initialized: boolean = false;
constructor(name: string) {
this.#name = name;
}
init() {
Logger.log(`[Kernel] Initializing ${this.#name} module...`);
this.#initialized = true;
}
get initialized() {
return this.#initialized;
}
}

View File

@@ -0,0 +1,25 @@
import { ConsoleKModule } from "../kernel/modules/console/console.kmod";
export class Logger {
static log(msg: string) {
if (!ConsoleKModule.instance.initialized)
return Logger.fallbackConsolePrintln(msg);
ConsoleKModule.instance.println(msg);
}
static clear() {
if (!ConsoleKModule.instance.initialized)
return Logger.fallbackConsoleClearScreen();
ConsoleKModule.instance.clear();
}
static fallbackConsolePrintln(msg: string) {
kc.println(msg);
}
static fallbackConsoleClearScreen() {
kc.clearScreen();
}
}

View File

@@ -1,3 +1,4 @@
declare namespace kc { declare namespace kc {
function println(msg: string): void; function println(msg: string): void;
function clearScreen(): void;
} }