From 953db56a0d8a8292e8259ea4da34b1f23cb4ee3f Mon Sep 17 00:00:00 2001 From: Elena Torro Date: Wed, 9 Apr 2025 12:19:47 +0200 Subject: [PATCH] :bug: Remove children correctly --- frontend/src/app/render_wasm/api.cljs | 5 ++--- render-wasm/src/main.rs | 12 +++++------- render-wasm/src/mem.rs | 6 ++++++ render-wasm/src/shapes.rs | 4 ---- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/frontend/src/app/render_wasm/api.cljs b/frontend/src/app/render_wasm/api.cljs index 6254c156b..d42e20b63 100644 --- a/frontend/src/app/render_wasm/api.cljs +++ b/frontend/src/app/render_wasm/api.cljs @@ -198,9 +198,8 @@ (when-not (empty? entries) (let [id (first entries)] (sr/heapu32-set-uuid id heap (mem/ptr8->ptr32 current-offset)) - (recur (rest entries) (+ current-offset CHILD-ENTRY-SIZE))))) - - (h/call wasm/internal-module "_set_children"))))) + (recur (rest entries) (+ current-offset CHILD-ENTRY-SIZE))))))) + (h/call wasm/internal-module "_set_children"))) (defn- get-string-length [string] (+ (count string) 1)) diff --git a/render-wasm/src/main.rs b/render-wasm/src/main.rs index 6e4619622..17b23eef5 100644 --- a/render-wasm/src/main.rs +++ b/render-wasm/src/main.rs @@ -219,7 +219,8 @@ pub extern "C" fn add_shape_child(a: u32, b: u32, c: u32, d: u32) { #[no_mangle] pub extern "C" fn set_children() { - let bytes = mem::bytes(); + let bytes = mem::bytes_or_empty(); + let entries: IndexSet = bytes .chunks(size_of::<::BytesType>()) .map(|data| Uuid::from_bytes(data.try_into().unwrap())) @@ -237,13 +238,10 @@ pub extern "C" fn set_children() { state.delete_shape(id); } }); -} -#[no_mangle] -pub extern "C" fn clear_shape_children() { - with_current_shape!(state, |shape: &mut Shape| { - shape.clear_children(); - }); + if !bytes.is_empty() { + mem::free_bytes(); + } } #[no_mangle] diff --git a/render-wasm/src/mem.rs b/render-wasm/src/mem.rs index cfba1757d..b70bd42f4 100644 --- a/render-wasm/src/mem.rs +++ b/render-wasm/src/mem.rs @@ -56,6 +56,12 @@ pub fn bytes() -> Vec { .map_or_else(|| panic!("Buffer is not initialized"), |buffer| *buffer) } +pub fn bytes_or_empty() -> Vec { + let mut guard = BUFFERU8.lock().unwrap(); + + guard.take().map_or_else(|| Vec::new(), |buffer| *buffer) +} + pub trait SerializableResult { type BytesType; fn from_bytes(bytes: Self::BytesType) -> Self; diff --git a/render-wasm/src/shapes.rs b/render-wasm/src/shapes.rs index b2fc5b167..8cf1f09bf 100644 --- a/render-wasm/src/shapes.rs +++ b/render-wasm/src/shapes.rs @@ -444,10 +444,6 @@ impl Shape { (added, removed) } - pub fn clear_children(&mut self) { - self.children.clear(); - } - pub fn fills(&self) -> std::slice::Iter { self.fills.iter() }