haku1 API cleanups (removing 2 suffix)

This commit is contained in:
りき萌 2025-06-16 18:22:18 +02:00
parent 00a48527ca
commit c80cd1c7fe
6 changed files with 82 additions and 77 deletions

View file

@ -250,20 +250,27 @@ export class Haku {
setBrush(code) {
w.haku_reset(this.#pInstance);
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);
this.#pVm2 = 0;
this.#pDefs2 = 0;
this.#bytecode2 = null;
let brushCode = allocString(code);
let statusCode = w.haku_compile_brush2(this.#pInstance, brushCode.length, brushCode.ptr);
let statusCode = w.haku_compile_brush(this.#pInstance, brushCode.length, brushCode.ptr);
freeString(brushCode);
if (!w.haku_is_ok(statusCode)) {
if (w.haku_is_diagnostics_emitted(statusCode)) {
let diagnostics = [];
for (let i = 0; i < w.haku_num_diagnostics2(this.#pInstance); ++i) {
for (let i = 0; i < w.haku_num_diagnostics(this.#pInstance); ++i) {
diagnostics.push({
start: w.haku_diagnostic_start2(this.#pInstance, i),
end: w.haku_diagnostic_end2(this.#pInstance, i),
start: w.haku_diagnostic_start(this.#pInstance, i),
end: w.haku_diagnostic_end(this.#pInstance, i),
message: readString(
memory,
w.haku_diagnostic_message_len2(this.#pInstance, i),
w.haku_diagnostic_message2(this.#pInstance, i),
w.haku_diagnostic_message_len(this.#pInstance, i),
w.haku_diagnostic_message(this.#pInstance, i),
),
});
}
@ -281,28 +288,22 @@ 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),
w.haku_defs(this.#pInstance),
w.haku_defs_len(this.#pInstance),
1,
);
let pTagsString = dup1to2(
w.haku_tags2(this.#pInstance),
w.haku_tags_len2(this.#pInstance),
w.haku_tags(this.#pInstance),
w.haku_tags_len(this.#pInstance),
1,
);
this.#pDefs2 = allocCheck(
w2.haku2_defs_parse(
pDefsString.ptr,
w.haku_defs_len2(this.#pInstance),
w.haku_defs_len(this.#pInstance),
pTagsString.ptr,
w.haku_tags_len2(this.#pInstance),
w.haku_tags_len(this.#pInstance),
),
);
@ -310,11 +311,11 @@ export class Haku {
this.#pVm2 = allocCheck(w2.haku2_vm_new());
this.#bytecode2 = dup1to2(
w.haku_bytecode2(this.#pInstance),
w.haku_bytecode_len2(this.#pInstance),
w.haku_bytecode(this.#pInstance),
w.haku_bytecode_len(this.#pInstance),
1,
);
this.#localCount = w.haku_local_count2(this.#pInstance);
this.#localCount = w.haku_local_count(this.#pInstance);
freeString2(pDefsString);
freeString2(pTagsString);
@ -340,10 +341,14 @@ export class Haku {
};
}
get ok() {
return this.#pVm2 != 0;
}
beginBrush() {
if (this.#pVm2 == 0) {
console.warn("VM instance is not available for drawing");
return;
if (!this.ok) {
console.debug("VM instance is not available for drawing");
return { status: "ok" };
}
w2.haku2_scratch_reset(this.#pScratch2);
@ -415,10 +420,10 @@ export class Haku {
}
get remainingFuel() {
return w2.haku2_vm_fuel(this.#pVm2);
return this.ok ? w2.haku2_vm_fuel(this.#pVm2) : this.#fuel;
}
get usedMemory() {
return w2.haku2_scratch_used(this.#pScratch2);
return this.ok ? w2.haku2_scratch_used(this.#pScratch2) : 0;
}
}