From a802a6c638b5b8dc3ca4c2b76a090988d3e6d557 Mon Sep 17 00:00:00 2001 From: Mica White Date: Sat, 31 Oct 2026 12:32:45 -0400 Subject: Fix state for WASM --- platforms/tortuise-script-wasm/src/state.rs | 99 +++++++++++++++++------------ 1 file changed, 59 insertions(+), 40 deletions(-) diff --git a/platforms/tortuise-script-wasm/src/state.rs b/platforms/tortuise-script-wasm/src/state.rs index ffcdb3e..22367af 100644 --- a/platforms/tortuise-script-wasm/src/state.rs +++ b/platforms/tortuise-script-wasm/src/state.rs @@ -5,6 +5,7 @@ use core::{ use alloc::alloc::{alloc, dealloc}; use serde::{Serialize, de::DeserializeOwned}; +use spin::lock_api::{MappedMutexGuard, Mutex, MutexGuard}; use crate::ScriptContext; @@ -18,21 +19,19 @@ mod tortuise { } } -impl ScriptContext<'_> { - pub fn load_state(&self) -> Option { - unsafe { - let size = tortuise::state_size(self.0); - let layout = Layout::array::(size).unwrap(); - if layout.size() == 0 { - return None; - } - let ptr = alloc(layout); - tortuise::load_state(self.0, ptr); - let data = core::slice::from_raw_parts(ptr, size); - let data = messagepack_serde::from_reader(data).ok()?; - dealloc(ptr, layout); - Some(data) +fn load_state(context: u64) -> Option { + unsafe { + let size = tortuise::state_size(context); + let layout = Layout::array::(size).unwrap(); + if layout.size() == 0 { + return None; } + let ptr = alloc(layout); + tortuise::load_state(context, ptr); + let data = core::slice::from_raw_parts(ptr, size); + let data = messagepack_serde::from_reader(data).ok()?; + dealloc(ptr, layout); + Some(data) } } @@ -43,45 +42,65 @@ fn save_state(context: u64, state: &T) { } } -pub struct StateMut { - context: u64, - data: T, +pub struct ScriptState { + data: Mutex>, + default: fn(&ScriptContext) -> T, } -impl StateMut { - pub fn load(context: &ScriptContext) -> Option { - Some(Self { - context: context.0, - data: context.load_state()?, - }) - } +pub struct StateRef<'state, T> { + data: MappedMutexGuard<'state, T>, +} - pub fn load_or(context: &ScriptContext, default: T) -> Self { +pub struct StateMut<'state, T: Serialize> { + context: u64, + data: MappedMutexGuard<'state, T>, +} + +impl ScriptState { + pub const fn new(default: fn(&ScriptContext) -> T) -> Self { Self { - context: context.0, - data: context.load_state().unwrap_or(default), + data: Mutex::new(None), + default, } } - pub fn load_or_else(context: &ScriptContext, default: impl FnOnce() -> T) -> Self { - Self { - context: context.0, - data: context.load_state().unwrap_or_else(default), - } + pub fn get(&self, context: &ScriptContext) -> StateRef<'_, T> + where + T: DeserializeOwned, + { + let data = MutexGuard::map(self.data.lock(), |data| { + data.get_or_insert_with(|| { + load_state(context.0).unwrap_or_else(|| (self.default)(context)) + }) + }); + StateRef { data } } - pub fn load_or_default(context: &ScriptContext) -> Self + pub fn get_mut(&self, context: &ScriptContext) -> StateMut<'_, T> where - T: Default, + T: DeserializeOwned + Serialize, { - Self { + let data = MutexGuard::map(self.data.lock(), |data| { + data.get_or_insert_with(|| { + load_state(context.0).unwrap_or_else(|| (self.default)(context)) + }) + }); + StateMut { context: context.0, - data: context.load_state().unwrap_or_default(), + data, } } } -impl Deref for StateMut { +impl Deref for StateRef<'_, T> { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.data + } +} + +impl Deref for StateMut<'_, T> { type Target = T; fn deref(&self) -> &Self::Target { @@ -89,14 +108,14 @@ impl Deref for StateMut { } } -impl DerefMut for StateMut { +impl DerefMut for StateMut<'_, T> { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.data } } -impl Drop for StateMut { +impl Drop for StateMut<'_, T> { fn drop(&mut self) { - save_state(self.context, &self.data); + save_state(self.context, &*self.data); } } -- cgit v1.3.1