Add console module
This commit is contained in:
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { KMain } from "./kernel";
|
||||||
|
|
||||||
export function KEntry() {
|
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 {
|
declare namespace kc {
|
||||||
function println(msg: string): void;
|
function println(msg: string): void;
|
||||||
|
function clearScreen(): void;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user