Initial setup for results for wasm
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
use anyhow::anyhow;
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow_ext::Result;
|
||||
use parking_lot::RwLock;
|
||||
use paste::paste;
|
||||
|
||||
@@ -177,40 +179,50 @@ script_function_cache! {
|
||||
|
||||
impl ScriptFunctionCache {
|
||||
/// Get the name of a script.
|
||||
pub(crate) fn script_get_name(&self, env: &Arc<WebAssemblyEnvironmentData>) -> Option<TypedFunction<u32, u32>> {
|
||||
pub(crate) fn script_get_name(&self, env: &Arc<WebAssemblyEnvironmentData>) -> Result<TypedFunction<u32, u32>> {
|
||||
{
|
||||
let read_lock = self.script_get_name.read();
|
||||
if let Some(f) = read_lock.as_ref() {
|
||||
return Some(f.clone());
|
||||
return Ok(f.clone());
|
||||
}
|
||||
}
|
||||
{
|
||||
let exported = env.exported_functions();
|
||||
let f = exported.get::<StringKey>(&"script_get_name".into());
|
||||
if let Some(f) = f {
|
||||
let func: TypedFunction<u32, u32> = f.typed(&env.store_ref()).unwrap();
|
||||
let func: TypedFunction<u32, u32> = f.typed(&env.store_ref())?;
|
||||
let _ = self.script_get_name.write().insert(func);
|
||||
}
|
||||
}
|
||||
self.script_get_name.read().as_ref().cloned()
|
||||
Ok(self
|
||||
.script_get_name
|
||||
.read()
|
||||
.as_ref()
|
||||
.ok_or(anyhow!("Couldn't find script function `script_get_name`"))?
|
||||
.clone())
|
||||
}
|
||||
|
||||
/// Drop the memory of a CString inside the WASM memory.
|
||||
pub(crate) fn dealloc_cstring(&self, env: &Arc<WebAssemblyEnvironmentData>) -> Option<TypedFunction<u32, ()>> {
|
||||
pub(crate) fn dealloc_cstring(&self, env: &Arc<WebAssemblyEnvironmentData>, ptr: u32) -> Result<()> {
|
||||
{
|
||||
let read_lock = self.dealloc_cstring.read();
|
||||
if let Some(f) = read_lock.as_ref() {
|
||||
return Some(f.clone());
|
||||
if read_lock.is_none() {
|
||||
drop(read_lock);
|
||||
let exported = env.exported_functions();
|
||||
let f = exported.get::<StringKey>(&"dealloc_cstring".into());
|
||||
if let Some(f) = f {
|
||||
let func: TypedFunction<u32, ()> = f.typed(&env.store_ref())?;
|
||||
let _ = self.dealloc_cstring.write().insert(func);
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
let exported = env.exported_functions();
|
||||
let f = exported.get::<StringKey>(&"dealloc_cstring".into());
|
||||
if let Some(f) = f {
|
||||
let func: TypedFunction<u32, ()> = f.typed(&env.store_ref()).unwrap();
|
||||
let _ = self.dealloc_cstring.write().insert(func);
|
||||
}
|
||||
}
|
||||
self.dealloc_cstring.read().as_ref().cloned()
|
||||
};
|
||||
let func = self
|
||||
.dealloc_cstring
|
||||
.read()
|
||||
.as_ref()
|
||||
.ok_or(anyhow!("Couldn't find script function `dealloc_cstring`"))?
|
||||
.clone();
|
||||
func.call(&mut env.store_mut(), ptr)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user