mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-21 08:35:40 +00:00
Refactor resource and error handling in wasm (#6074)
* Refactor resource and error handling in wasm * Fixes based on review
This commit is contained in:
@@ -237,21 +237,33 @@ impl<'a, FE: SandboxCapabilities + 'a> Externals for GuestExternals<'a, FE> {
|
|||||||
.supervisor_externals
|
.supervisor_externals
|
||||||
.allocate_memory(invoke_args_len)
|
.allocate_memory(invoke_args_len)
|
||||||
.map_err(|_| trap("Can't allocate memory in supervisor for the arguments"))?;
|
.map_err(|_| trap("Can't allocate memory in supervisor for the arguments"))?;
|
||||||
self
|
|
||||||
|
let deallocate = |this: &mut GuestExternals<FE>, ptr, fail_msg| {
|
||||||
|
this
|
||||||
|
.supervisor_externals
|
||||||
|
.deallocate_memory(ptr)
|
||||||
|
.map_err(|_| trap(fail_msg))
|
||||||
|
};
|
||||||
|
|
||||||
|
if self
|
||||||
.supervisor_externals
|
.supervisor_externals
|
||||||
.write_memory(invoke_args_ptr, &invoke_args_data)
|
.write_memory(invoke_args_ptr, &invoke_args_data)
|
||||||
.map_err(|_| trap("Can't write invoke args into memory"))?;
|
.is_err()
|
||||||
|
{
|
||||||
|
deallocate(self, invoke_args_ptr, "Failed dealloction after failed write of invoke arguments")?;
|
||||||
|
return Err(trap("Can't write invoke args into memory"));
|
||||||
|
}
|
||||||
|
|
||||||
let result = self.supervisor_externals.invoke(
|
let result = self.supervisor_externals.invoke(
|
||||||
&self.sandbox_instance.dispatch_thunk,
|
&self.sandbox_instance.dispatch_thunk,
|
||||||
invoke_args_ptr,
|
invoke_args_ptr,
|
||||||
invoke_args_len,
|
invoke_args_len,
|
||||||
state,
|
state,
|
||||||
func_idx,
|
func_idx,
|
||||||
)?;
|
);
|
||||||
self
|
|
||||||
.supervisor_externals
|
deallocate(self, invoke_args_ptr, "Can't deallocate memory for dispatch thunk's invoke arguments")?;
|
||||||
.deallocate_memory(invoke_args_ptr)
|
let result = result?;
|
||||||
.map_err(|_| trap("Can't deallocate memory for dispatch thunk's invoke arguments"))?;
|
|
||||||
|
|
||||||
// dispatch_thunk returns pointer to serialized arguments.
|
// dispatch_thunk returns pointer to serialized arguments.
|
||||||
// Unpack pointer and len of the serialized result data.
|
// Unpack pointer and len of the serialized result data.
|
||||||
@@ -265,12 +277,11 @@ impl<'a, FE: SandboxCapabilities + 'a> Externals for GuestExternals<'a, FE> {
|
|||||||
|
|
||||||
let serialized_result_val = self.supervisor_externals
|
let serialized_result_val = self.supervisor_externals
|
||||||
.read_memory(serialized_result_val_ptr, serialized_result_val_len)
|
.read_memory(serialized_result_val_ptr, serialized_result_val_len)
|
||||||
.map_err(|_| trap("Can't read the serialized result from dispatch thunk"))?;
|
.map_err(|_| trap("Can't read the serialized result from dispatch thunk"));
|
||||||
self.supervisor_externals
|
|
||||||
.deallocate_memory(serialized_result_val_ptr)
|
|
||||||
.map_err(|_| trap("Can't deallocate memory for dispatch thunk's result"))?;
|
|
||||||
|
|
||||||
deserialize_result(&serialized_result_val)
|
deallocate(self, serialized_result_val_ptr, "Can't deallocate memory for dispatch thunk's result")
|
||||||
|
.and_then(|_| serialized_result_val)
|
||||||
|
.and_then(|serialized_result_val| deserialize_result(&serialized_result_val))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user