summaryrefslogtreecommitdiff
path: root/src/script
diff options
context:
space:
mode:
Diffstat (limited to 'src/script')
-rw-r--r--src/script/wasm.rs77
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