From c80cd1c7fe175753b287513219eb63e9f4223eba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=AA=E3=82=AD=E8=90=8C?= Date: Mon, 16 Jun 2025 18:22:18 +0200 Subject: [PATCH] haku1 API cleanups (removing 2 suffix) --- crates/haku-wasm/src/lib.rs | 52 +++++++++++++++++--------------- crates/haku2/src/bytecode.zig | 2 -- crates/rkgk/src/api/wall.rs | 18 +++++------ crates/rkgk/src/haku.rs | 28 ++++++++--------- static/haku.js | 57 +++++++++++++++++++---------------- static/index.js | 2 ++ 6 files changed, 82 insertions(+), 77 deletions(-) diff --git a/crates/haku-wasm/src/lib.rs b/crates/haku-wasm/src/lib.rs index 771117f..9c6a65e 100644 --- a/crates/haku-wasm/src/lib.rs +++ b/crates/haku-wasm/src/lib.rs @@ -370,10 +370,8 @@ unsafe extern "C" fn haku_pixmap_stroke( true } -// v2 compile-only support - #[unsafe(no_mangle)] -unsafe extern "C" fn haku_compile_brush2( +unsafe extern "C" fn haku_compile_brush( instance: *mut Instance, code_len: u32, code: *const u8, @@ -480,72 +478,78 @@ unsafe extern "C" fn haku_compile_brush2( StatusCode::Ok } -unsafe fn compile_result<'a>(instance: *const Instance) -> &'a CompileResult { +unsafe fn unwrap_compile_result<'a>(instance: *const Instance) -> &'a CompileResult { let cr = (*instance).compile_result2.as_ref().unwrap(); cr } +unsafe fn get_compile_result<'a>(instance: *const Instance) -> Option<&'a CompileResult> { + (*instance).compile_result2.as_ref() +} + #[unsafe(no_mangle)] -unsafe extern "C" fn haku_num_diagnostics2(instance: *const Instance) -> u32 { +unsafe extern "C" fn haku_num_diagnostics(instance: *const Instance) -> u32 { (*instance).diagnostics2.len() as u32 } #[unsafe(no_mangle)] -unsafe extern "C" fn haku_diagnostic_start2(instance: *const Instance, index: u32) -> u32 { +unsafe extern "C" fn haku_diagnostic_start(instance: *const Instance, index: u32) -> u32 { (*instance).diagnostics2[index as usize].span().start } #[unsafe(no_mangle)] -unsafe extern "C" fn haku_diagnostic_end2(instance: *const Instance, index: u32) -> u32 { +unsafe extern "C" fn haku_diagnostic_end(instance: *const Instance, index: u32) -> u32 { (*instance).diagnostics2[index as usize].span().end } #[unsafe(no_mangle)] -unsafe extern "C" fn haku_diagnostic_message2(instance: *const Instance, index: u32) -> *const u8 { +unsafe extern "C" fn haku_diagnostic_message(instance: *const Instance, index: u32) -> *const u8 { (*instance).diagnostics2[index as usize].message().as_ptr() } #[unsafe(no_mangle)] -unsafe extern "C" fn haku_diagnostic_message_len2(instance: *const Instance, index: u32) -> u32 { +unsafe extern "C" fn haku_diagnostic_message_len(instance: *const Instance, index: u32) -> u32 { (*instance).diagnostics2[index as usize].message().len() as u32 } #[unsafe(no_mangle)] -unsafe extern "C" fn haku_defs_len2(instance: *const Instance) -> usize { - compile_result(instance).defs_string.len() +unsafe extern "C" fn haku_defs_len(instance: *const Instance) -> usize { + unwrap_compile_result(instance).defs_string.len() } #[unsafe(no_mangle)] -unsafe extern "C" fn haku_defs2(instance: *const Instance) -> *const u8 { - compile_result(instance).defs_string.as_ptr() +unsafe extern "C" fn haku_defs(instance: *const Instance) -> *const u8 { + unwrap_compile_result(instance).defs_string.as_ptr() } #[unsafe(no_mangle)] -unsafe extern "C" fn haku_tags_len2(instance: *const Instance) -> usize { - compile_result(instance).tags_string.len() +unsafe extern "C" fn haku_tags_len(instance: *const Instance) -> usize { + unwrap_compile_result(instance).tags_string.len() } #[unsafe(no_mangle)] -unsafe extern "C" fn haku_tags2(instance: *const Instance) -> *const u8 { - compile_result(instance).tags_string.as_ptr() +unsafe extern "C" fn haku_tags(instance: *const Instance) -> *const u8 { + unwrap_compile_result(instance).tags_string.as_ptr() } #[unsafe(no_mangle)] -unsafe extern "C" fn haku_bytecode_len2(instance: *const Instance) -> usize { - compile_result(instance).chunk.bytecode.len() +unsafe extern "C" fn haku_bytecode_len(instance: *const Instance) -> usize { + unwrap_compile_result(instance).chunk.bytecode.len() } #[unsafe(no_mangle)] -unsafe extern "C" fn haku_bytecode2(instance: *const Instance) -> *const u8 { - compile_result(instance).chunk.bytecode.as_ptr() +unsafe extern "C" fn haku_bytecode(instance: *const Instance) -> *const u8 { + unwrap_compile_result(instance).chunk.bytecode.as_ptr() } #[unsafe(no_mangle)] -unsafe extern "C" fn haku_local_count2(instance: *const Instance) -> u8 { - compile_result(instance).closure_spec.local_count +unsafe extern "C" fn haku_local_count(instance: *const Instance) -> u8 { + unwrap_compile_result(instance).closure_spec.local_count } #[unsafe(no_mangle)] unsafe extern "C" fn haku_stat_ast_size(instance: *const Instance) -> usize { - compile_result(instance).stats.ast_size + get_compile_result(instance) + .map(|cr| cr.stats.ast_size) + .unwrap_or_default() } diff --git a/crates/haku2/src/bytecode.zig b/crates/haku2/src/bytecode.zig index 953c568..9d4005f 100644 --- a/crates/haku2/src/bytecode.zig +++ b/crates/haku2/src/bytecode.zig @@ -71,8 +71,6 @@ pub const Defs = struct { .num_tags = mem.count(u8, tags_string, "\n"), }; - log.debug("parsed defs {*} tags {s}: {}", .{ defs_string, tags_string, d }); - return d; } diff --git a/crates/rkgk/src/api/wall.rs b/crates/rkgk/src/api/wall.rs index 10d6c9c..22ad659 100644 --- a/crates/rkgk/src/api/wall.rs +++ b/crates/rkgk/src/api/wall.rs @@ -471,8 +471,8 @@ impl SessionLoop { Interaction::Dotter { from, to, num } => { if brush_ok { - jumpstart_trampoline2(&mut haku); - match haku.cont2() { + jumpstart_trampoline(&mut haku); + match haku.cont() { haku2::Cont::Dotter(dotter) => match dotter.run(&haku2::Dotter { from: (from.x, from.y), to: (to.x, to.y), @@ -487,9 +487,9 @@ impl SessionLoop { } Interaction::Scribble => { - match haku.cont2() { + match haku.cont() { haku2::Cont::None => { - draw_to_chunks2(&wall, current_render_area, &mut haku); + draw_to_chunks(&wall, current_render_area, &mut haku); } _ => error!("tried to draw a scribble with an active continuation"), } @@ -547,16 +547,16 @@ fn chunks_to_modify( chunks } -fn jumpstart_trampoline2(haku: &mut Haku) { - if !haku.has_cont2() { - if let Err(e) = haku.eval_brush2() { +fn jumpstart_trampoline(haku: &mut Haku) { + if !haku.has_cont() { + if let Err(e) = haku.eval_brush() { error!("eval_brush2 exception: {e:?}"); } } } #[instrument(skip(wall, vm))] -fn draw_to_chunks2(wall: &Wall, render_area: RenderArea, vm: &mut Haku) { +fn draw_to_chunks(wall: &Wall, render_area: RenderArea, vm: &mut Haku) { let settings = wall.settings(); let chunk_size = settings.chunk_size as f32; @@ -571,7 +571,7 @@ fn draw_to_chunks2(wall: &Wall, render_area: RenderArea, vm: &mut Haku) { let chunk_ref = wall.get_or_create_chunk(ChunkPosition::new(chunk_x, chunk_y)); let mut chunk = chunk_ref.blocking_lock(); let mut canvas = ChunkCanvas::new(&mut chunk).translated(x, y); - if let Err(e) = vm.render2(&mut canvas, 256) { + if let Err(e) = vm.render(&mut canvas, 256) { error!(chunk_x, chunk_y, "draw_to_chunks2 exception: {e:?}"); } } diff --git a/crates/rkgk/src/haku.rs b/crates/rkgk/src/haku.rs index e160afa..f5530bd 100644 --- a/crates/rkgk/src/haku.rs +++ b/crates/rkgk/src/haku.rs @@ -46,7 +46,7 @@ pub struct Haku { defs: Defs, defs_image: DefsImage, - vm2: Option, + vm: Option, brush: Option<(ChunkId, ClosureSpec)>, } @@ -68,7 +68,7 @@ impl Haku { system_image, defs, defs_image, - vm2: None, + vm: None, brush: None, } } @@ -128,7 +128,7 @@ impl Haku { self.brush = Some((chunk_id, closure_spec)); let scratch = self - .vm2 + .vm .take() .map(|vm| vm.into_scratch()) .unwrap_or_else(|| haku2::Scratch::new(self.limits.memory)); @@ -140,7 +140,7 @@ impl Haku { stack_capacity: self.limits.stack_capacity, call_stack_capacity: self.limits.call_stack_capacity, }); - self.vm2 = Some(haku2::Vm::new(scratch, code, limits)); + self.vm = Some(haku2::Vm::new(scratch, code, limits)); info!("brush set successfully"); @@ -148,9 +148,9 @@ impl Haku { } #[instrument(skip(self), err(level = Level::INFO))] - pub fn eval_brush2(&mut self) -> eyre::Result<()> { + pub fn eval_brush(&mut self) -> eyre::Result<()> { let vm = self - .vm2 + .vm .as_mut() .ok_or_eyre("brush is not compiled and ready to be used")?; vm.begin(self.limits.fuel as u32) @@ -159,13 +159,9 @@ impl Haku { } #[instrument(skip(self, canvas, max_depth), err(level = Level::INFO))] - pub fn render2( - &mut self, - canvas: &mut dyn haku2::Canvas, - max_depth: usize, - ) -> eyre::Result<()> { + pub fn render(&mut self, canvas: &mut dyn haku2::Canvas, max_depth: usize) -> eyre::Result<()> { let vm = self - .vm2 + .vm .as_mut() .ok_or_eyre("VM is not ready for rendering")?; vm.render(canvas, max_depth) @@ -173,11 +169,11 @@ impl Haku { Ok(()) } - pub fn has_cont2(&mut self) -> bool { - self.vm2.as_mut().expect("VM is not started").has_cont() + pub fn has_cont(&mut self) -> bool { + self.vm.as_mut().expect("VM is not started").has_cont() } - pub fn cont2(&mut self) -> haku2::Cont<'_> { - self.vm2.as_mut().expect("VM is not started").cont() + pub fn cont(&mut self) -> haku2::Cont<'_> { + self.vm.as_mut().expect("VM is not started").cont() } } diff --git a/static/haku.js b/static/haku.js index 9daf6c7..ad9e3c5 100644 --- a/static/haku.js +++ b/static/haku.js @@ -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; } } diff --git a/static/index.js b/static/index.js index 0329cb6..d287b0c 100644 --- a/static/index.js +++ b/static/index.js @@ -244,6 +244,8 @@ function readUrl(urlString) { setInterval(flushInteractionQueue, updateInterval); canvasRenderer.addEventListener(".interact", async (event) => { + if (!currentUser.haku.ok) return; + let result = await currentUser.haku.evalBrush( selfController(interactionQueue, wall, event), );