Skip to content

Commit e8eb116

Browse files
committed
Ref type
1 parent 33ff96c commit e8eb116

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

bindings/rust/src/lib.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,14 @@ impl ModuleInspector {
203203
}
204204

205205
/// An instance of a module.
206-
pub struct Instance(NonNull<sys::FizzyInstance>);
206+
pub struct Instance {
207+
instance: NonNull<sys::FizzyInstance>,
208+
module: Module,
209+
}
207210

208211
impl Drop for Instance {
209212
fn drop(&mut self) {
210-
unsafe { sys::fizzy_free_instance(self.0.as_ptr()) }
213+
unsafe { sys::fizzy_free_instance(self.instance.as_ptr()) }
211214
}
212215
}
213216

@@ -235,12 +238,18 @@ impl Module {
235238
// Forget Module (and avoid calling drop) because it has been consumed by instantiate (even if it failed).
236239
core::mem::forget(self);
237240
if ptr.is_null() {
238-
debug_assert!(err.code() != 0);
239-
Err(err.error().unwrap())
240-
} else {
241-
debug_assert!(err.code() == 0);
242-
Ok(Instance(unsafe { NonNull::new_unchecked(ptr) }))
241+
return Err("Invalid ptr".into());
243242
}
243+
let instance = unsafe { NonNull::new_unchecked(ptr) };
244+
Ok(Instance {
245+
instance: instance,
246+
module: Module {
247+
ptr: unsafe {
248+
ConstNonNull::new_unchecked(sys::fizzy_get_instance_module(instance.as_ptr()))
249+
},
250+
owned: false,
251+
},
252+
})
244253
}
245254

246255
/// Returns true if the module has a start function defined.
@@ -437,8 +446,8 @@ impl Instance {
437446
/// # Safety
438447
/// These slices turn invalid if the memory is resized (i.e. via the WebAssembly `memory.grow` instruction)
439448
pub unsafe fn checked_memory_slice(&self, offset: u32, size: usize) -> Result<&[u8], Error> {
440-
let memory_data = sys::fizzy_get_instance_memory_data(self.0.as_ptr());
441-
let memory_size = sys::fizzy_get_instance_memory_size(self.0.as_ptr());
449+
let memory_data = sys::fizzy_get_instance_memory_data(self.instance.as_ptr());
450+
let memory_size = sys::fizzy_get_instance_memory_size(self.instance.as_ptr());
442451
let range = Instance::checked_memory_range(memory_data, memory_size, offset, size)?;
443452
// Slices allow empty length, but data must be a valid pointer.
444453
debug_assert!(!memory_data.is_null());
@@ -455,8 +464,8 @@ impl Instance {
455464
offset: u32,
456465
size: usize,
457466
) -> Result<&mut [u8], Error> {
458-
let memory_data = sys::fizzy_get_instance_memory_data(self.0.as_ptr());
459-
let memory_size = sys::fizzy_get_instance_memory_size(self.0.as_ptr());
467+
let memory_data = sys::fizzy_get_instance_memory_data(self.instance.as_ptr());
468+
let memory_size = sys::fizzy_get_instance_memory_size(self.instance.as_ptr());
460469
let range = Instance::checked_memory_range(memory_data, memory_size, offset, size)?;
461470
// Slices allow empty length, but data must be a valid pointer.
462471
debug_assert!(!memory_data.is_null());
@@ -466,7 +475,7 @@ impl Instance {
466475

467476
/// Returns the current memory size, in bytes.
468477
pub fn memory_size(&self) -> usize {
469-
unsafe { sys::fizzy_get_instance_memory_size(self.0.as_ptr()) }
478+
unsafe { sys::fizzy_get_instance_memory_size(self.instance.as_ptr()) }
470479
}
471480

472481
/// Copies memory from `offset` to `target`, for the length of `target.len()`.
@@ -485,17 +494,14 @@ impl Instance {
485494

486495
/// Get a read-only pointer to the module.
487496
unsafe fn get_module_ptr(&self) -> *const sys::FizzyModule {
488-
let ptr = sys::fizzy_get_instance_module(self.0.as_ptr());
497+
let ptr = sys::fizzy_get_instance_module(self.instance.as_ptr());
489498
debug_assert!(!ptr.is_null());
490499
ptr
491500
}
492501

493502
/// Get a non-owned module instance.
494-
pub fn get_module(&self) -> Module {
495-
Module {
496-
ptr: unsafe { ConstNonNull::new_unchecked(self.get_module_ptr()) },
497-
owned: false,
498-
}
503+
pub fn get_module(&self) -> &Module {
504+
&self.module
499505
}
500506

501507
/// Find index of exported function by name.
@@ -521,7 +527,7 @@ impl Instance {
521527
/// This function expects a valid `func_idx` and appropriate number of `args`.
522528
pub unsafe fn unsafe_execute(&mut self, func_idx: u32, args: &[Value]) -> ExecutionResult {
523529
ExecutionResult(sys::fizzy_execute(
524-
self.0.as_ptr(),
530+
self.instance.as_ptr(),
525531
func_idx,
526532
args.as_ptr(),
527533
std::ptr::null_mut(),

0 commit comments

Comments
 (0)