haku2: add stats + fix crash w uninit vm

This commit is contained in:
りき萌 2025-06-16 16:39:54 +02:00
parent e667c6336a
commit 00a48527ca
12 changed files with 88 additions and 71 deletions

View file

@ -47,7 +47,6 @@ let [hakuWasm, haku2Wasm] = await Promise.all([
__haku2_log_info: makeLogFunction2("info"),
__haku2_log_debug: makeLogFunction2("debug"),
// TODO: Renderer
__haku2_canvas_begin: (c) => canvasBeginImpl(c),
__haku2_canvas_line: (c, x1, y1, x2, y2) => canvasLineImpl(c, x1, y1, x2, y2),
__haku2_canvas_rectangle: (c, x, y, width, height) =>
@ -205,6 +204,7 @@ export class Haku {
#pLimits2 = 0;
#pScratch2 = 0;
#pDefs2 = 0;
#pVm2 = 0;
#bytecode2 = null;
@ -242,11 +242,14 @@ export class Haku {
w2.haku2_scratch_destroy(this.#pScratch2);
w2.haku2_vm_destroy(this.#pVm2);
w2.haku2_defs_destroy(this.#pDefs2);
w2.haku2_limits_destroy(this.#pLimits2);
w2.haku_dealloc(this.#bytecode2.ptr);
}
setBrush(code) {
w.haku_reset(this.#pInstance);
let brushCode = allocString(code);
let statusCode = w.haku_compile_brush2(this.#pInstance, brushCode.length, brushCode.ptr);
freeString(brushCode);
@ -279,8 +282,11 @@ export class Haku {
}
if (this.#pVm2 != 0) w2.haku2_vm_destroy(this.#pVm2);
if (this.#pDefs2 != 0) w2.haku2_defs_destroy(this.#pDefs2);
if (this.#bytecode2 != null) freeString2(this.#bytecode2);
console.log(w.haku_defs2(this.#pInstance), w.haku_defs_len2(this.#pInstance));
let pDefsString = dup1to2(
w.haku_defs2(this.#pInstance),
w.haku_defs_len2(this.#pInstance),
@ -291,17 +297,17 @@ export class Haku {
w.haku_tags_len2(this.#pInstance),
1,
);
let defs = allocCheck(
this.#pDefs2 = allocCheck(
w2.haku2_defs_parse(
pDefsString,
pDefsString.ptr,
w.haku_defs_len2(this.#pInstance),
pTagsString,
pTagsString.ptr,
w.haku_tags_len2(this.#pInstance),
),
);
w2.haku2_scratch_reset(this.#pScratch2);
this.#pVm2 = allocCheck(w2.haku2_vm_new(this.#pScratch2, defs, this.#pLimits2));
this.#pVm2 = allocCheck(w2.haku2_vm_new());
this.#bytecode2 = dup1to2(
w.haku_bytecode2(this.#pInstance),
@ -310,7 +316,6 @@ export class Haku {
);
this.#localCount = w.haku_local_count2(this.#pInstance);
w2.haku2_defs_destroy(defs);
freeString2(pDefsString);
freeString2(pTagsString);
@ -341,7 +346,8 @@ export class Haku {
return;
}
w2.haku2_vm_reset(this.#pVm2, this.#fuel);
w2.haku2_scratch_reset(this.#pScratch2);
w2.haku2_vm_reset(this.#pVm2, this.#pScratch2, this.#pDefs2, this.#pLimits2, this.#fuel);
let ok = w2.haku2_vm_run_main(
this.#pVm2,
this.#pScratch2,
@ -405,18 +411,14 @@ export class Haku {
}
get astSize() {
return 0; // TODO
}
get numRefs() {
return 0; // TODO
return w.haku_stat_ast_size(this.#pInstance);
}
get remainingFuel() {
return 0; // TODO
return w2.haku2_vm_fuel(this.#pVm2);
}
get remainingMemory() {
return 0; // TODO
get usedMemory() {
return w2.haku2_scratch_used(this.#pScratch2);
}
}