diff options
| author | Mica White <botahamec@outlook.com> | 2026-07-10 19:42:23 -0400 |
|---|---|---|
| committer | Mica White <botahamec@outlook.com> | 2026-07-10 19:42:23 -0400 |
| commit | 7aa6566796e1632ae36277648c162fc77af7052b (patch) | |
| tree | 1f8362c44329acd519eb1c19cc65a3729e50313a /src/script/wasm.rs | |
| parent | a9977e7fabeb06a6f21824cc4a41b36ea39f6799 (diff) | |
State utilities
Diffstat (limited to 'src/script/wasm.rs')
| -rw-r--r-- | src/script/wasm.rs | 77 |
1 files changed, 76 insertions, 1 deletions
diff --git a/src/script/wasm.rs b/src/script/wasm.rs index a1b923b..5a9332e 100644 --- a/src/script/wasm.rs +++ b/src/script/wasm.rs @@ -328,7 +328,76 @@ fn save_state(mut caller: Caller<FullScriptContext<'_>>, offset: u32, length: u3 context.operation_queue.push(Operation::UpdateState { script_id: context.script_id, new_state, - }) + }); +} + +fn entity_size(caller: Caller<FullScriptContext<'_>>, name: u32, name_len: u32) -> u32 { + let context = caller.data(); + let name = unsafe { str_from_memory(&caller, name, name_len) }; + context + .scripts + .entities + .get(name) + .map(|entity| entity.len()) + .unwrap_or_default() as u32 +} + +fn load_entity(mut caller: Caller<FullScriptContext<'_>>, name: u32, name_len: u32, offset: u32) { + let memory = caller.get_export("memory").unwrap().into_memory().unwrap(); + let name = unsafe { str_from_memory(&caller, name, name_len) }; + let context = caller.data(); + let entity = context + .scripts + .entities + .get(name) + .map(|entity| &**entity) + .unwrap_or(&[]); + memory.write(&mut caller, offset as usize, entity).unwrap(); +} + +fn save_entity( + mut caller: Caller<FullScriptContext<'_>>, + name: u32, + name_len: u32, + offset: u32, + length: u32, +) { + let offset = offset as usize; + let length = length as usize; + let memory = caller.get_export("memory").unwrap().into_memory().unwrap(); + let new_state = Box::from(&memory.data(&caller)[offset..(offset + length)]); + let name = Box::from(unsafe { str_from_memory(&caller, name, name_len) }); + let context = caller.data_mut(); + context + .operation_queue + .push(Operation::UpdateEntity { name, new_state }); +} + +fn storage_size(caller: Caller<FullScriptContext<'_>>) -> u32 { + caller + .data() + .storage + .get() + .map(|data| data.len()) + .unwrap_or_default() as u32 +} + +fn load_storage(mut caller: Caller<FullScriptContext<'_>>, offset: u32) { + let memory = caller.get_export("memory").unwrap().into_memory().unwrap(); + let context = caller.data(); + let state = context.storage.get().unwrap_or(&[]); + memory.write(&mut caller, offset as usize, state).unwrap(); +} + +fn save_storage(mut caller: Caller<FullScriptContext<'_>>, offset: u32, length: u32) { + let offset = offset as usize; + let length = length as usize; + let memory = caller.get_export("memory").unwrap().into_memory().unwrap(); + let new_save = Box::from(&memory.data(&caller)[offset..(offset + length)]); + let context = caller.data_mut(); + context + .operation_queue + .push(Operation::UpdateStorage { new_save }); } fn system_now(caller: Caller<FullScriptContext<'_>>) -> u64 { @@ -381,6 +450,12 @@ pub fn linker<'ctx>( state_size, load_state, save_state, + entity_size, + load_entity, + save_entity, + storage_size, + load_storage, + save_storage, system_now, monotonic_now, delta_time |
