Add console module
This commit is contained in:
@@ -93,6 +93,12 @@ JSValue jsKCPrintln(JSContext *ctx, JSValueConst jsThis, int argc, JSValueConst
|
||||
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) {
|
||||
JSValue global = JS_GetGlobalObject(ctx);
|
||||
JSValue kc = JS_NewObject(ctx);
|
||||
@@ -100,6 +106,7 @@ void initKC(JSContext *ctx) {
|
||||
JS_SetPropertyStr(ctx, global, "kc", kc);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { KMain } from "./kernel";
|
||||
|
||||
export function KEntry() {
|
||||
kc.println("Kernel entry point reached!");
|
||||
KMain();
|
||||
}
|
||||
|
||||
9
src/system/src/kernel/index.ts
Normal file
9
src/system/src/kernel/index.ts
Normal 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();
|
||||
}
|
||||
25
src/system/src/kernel/modules/console/console.kmod.ts
Normal file
25
src/system/src/kernel/modules/console/console.kmod.ts
Normal 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();
|
||||
}
|
||||
}
|
||||
20
src/system/src/kernel/modules/kmod.ts
Normal file
20
src/system/src/kernel/modules/kmod.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
25
src/system/src/lib/logger.ts
Normal file
25
src/system/src/lib/logger.ts
Normal 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();
|
||||
}
|
||||
}
|
||||
1
src/system/src/types/kc.d.ts
vendored
1
src/system/src/types/kc.d.ts
vendored
@@ -1,3 +1,4 @@
|
||||
declare namespace kc {
|
||||
function println(msg: string): void;
|
||||
function clearScreen(): void;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user