Skip to content

Commit

Permalink
Refactor lifetimes in Function, Instance, Module, and Runtime structs…
Browse files Browse the repository at this point in the history
… for improved clarity and consistency (#83)
  • Loading branch information
AlixANNERAUD authored Dec 23, 2024
1 parent 9d11c0d commit 9a910a1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
15 changes: 9 additions & 6 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ use crate::{
helper::exception_to_string, instance::Instance, value::WasmValue, ExecError, RuntimeError,
};

pub struct Function<'a> {
pub struct Function<'instance> {
function: wasm_function_inst_t,
_phantom: PhantomData<Instance<'a>>,
_phantom: PhantomData<Instance<'instance>>,
}

impl<'a> Function<'a> {
impl<'instance> Function<'instance> {
/// find a function by name
///
/// # Error
///
/// Return `RuntimeError::FunctionNotFound` if failed.
pub fn find_export_func(instance: &'a Instance<'a>, name: &str) -> Result<Self, RuntimeError> {
pub fn find_export_func(
instance: &'instance Instance<'instance>,
name: &str,
) -> Result<Self, RuntimeError> {
let name = CString::new(name).expect("CString::new failed");
let function =
unsafe { wasm_runtime_lookup_function(instance.get_inner_instance(), name.as_ptr()) };
Expand All @@ -46,7 +49,7 @@ impl<'a> Function<'a> {
#[allow(non_upper_case_globals)]
fn parse_result(
&self,
instance: &'a Instance<'a>,
instance: &Instance<'instance>,
result: Vec<u32>,
) -> Result<WasmValue, RuntimeError> {
let result_count =
Expand Down Expand Up @@ -81,7 +84,7 @@ impl<'a> Function<'a> {
/// Return `RuntimeError::ExecutionError` if failed.
pub fn call(
&self,
instance: &'a Instance<'a>,
instance: &'instance Instance<'instance>,
params: &Vec<WasmValue>,
) -> Result<WasmValue, RuntimeError> {
// params -> Vec<u32>
Expand Down
14 changes: 7 additions & 7 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ use crate::{
};

#[derive(Debug)]
pub struct Instance<'a> {
pub struct Instance<'module> {
instance: wasm_module_inst_t,
_phantom: PhantomData<Module<'a>>,
_phantom: PhantomData<Module<'module>>,
}

impl<'a> Instance<'a> {
impl<'module> Instance<'module> {
/// instantiate a module with stack size
///
/// # Error
///
/// Return `RuntimeError::CompilationError` if failed.
pub fn new(
runtime: &'a Runtime<'a>,
module: &'a Module<'a>,
runtime: &Runtime,
module: &'module Module<'module>,
stack_size: u32,
) -> Result<Self, RuntimeError> {
Self::new_with_args(runtime, module, stack_size, 0)
Expand All @@ -48,8 +48,8 @@ impl<'a> Instance<'a> {
///
/// Return `RuntimeError::CompilationError` if failed.
pub fn new_with_args(
_runtime: &'a Runtime<'a>,
module: &'a Module<'a>,
_runtime: &Runtime,
module: &'module Module<'module>,
stack_size: u32,
heap_size: u32,
) -> Result<Self, RuntimeError> {
Expand Down
10 changes: 5 additions & 5 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ use wamr_sys::{

#[allow(dead_code)]
#[derive(Debug)]
pub struct Module<'a> {
pub struct Module<'runtime> {
name: String,
module: wasm_module_t,
// to keep the module content in memory
content: Vec<u8>,
wasi_ctx: WasiCtx,
_phantom: PhantomData<Runtime<'a>>,
_phantom: PhantomData<&'runtime Runtime>,
}

impl<'a> Module<'a> {
impl<'runtime> Module<'runtime> {
/// compile a module with the given wasm file path, use the file name as the module name
///
/// # Error
///
/// If the file does not exist or the file cannot be read, an `RuntimeError::WasmFileFSError` will be returned.
/// If the wasm file is not a valid wasm file, an `RuntimeError::CompilationError` will be returned.
pub fn from_file(runtime: &'a Runtime<'a>, wasm_file: &Path) -> Result<Self, RuntimeError> {
pub fn from_file(runtime: &'runtime Runtime, wasm_file: &Path) -> Result<Self, RuntimeError> {
let name = wasm_file.file_name().unwrap().to_str().unwrap();
let mut wasm_file = File::open(wasm_file)?;

Expand All @@ -61,7 +61,7 @@ impl<'a> Module<'a> {
/// If the file does not exist or the file cannot be read, an `RuntimeError::WasmFileFSError` will be returned.
/// If the wasm file is not a valid wasm file, an `RuntimeError::CompilationError` will be returned.
pub fn from_vec(
_runtime: &'a Runtime<'a>,
_runtime: &'runtime Runtime,
mut content: Vec<u8>,
name: &str,
) -> Result<Self, RuntimeError> {
Expand Down
13 changes: 5 additions & 8 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! Every process should have only one instance of this runtime by call
//! `Runtime::new()` or `Runtime::builder().build()` once.
use std::{ffi::c_void, marker::PhantomData};
use std::ffi::c_void;

use wamr_sys::{
mem_alloc_type_t_Alloc_With_Pool, mem_alloc_type_t_Alloc_With_System_Allocator,
Expand All @@ -19,12 +19,11 @@ use crate::{host_function::HostFunctionList, RuntimeError};

#[allow(dead_code)]
#[derive(Debug)]
pub struct Runtime<'a> {
pub struct Runtime {
host_functions: HostFunctionList,
_phantom: PhantomData<&'a u8>,
}

impl<'a> Runtime<'a> {
impl Runtime {
/// return a `RuntimeBuilder` instance
///
/// has to
Expand All @@ -45,14 +44,13 @@ impl<'a> Runtime<'a> {
match unsafe { wasm_runtime_init() } {
true => Ok(Runtime {
host_functions: HostFunctionList::new("empty"),
_phantom: PhantomData,
}),
false => Err(RuntimeError::InitializationFailure),
}
}
}

impl Drop for Runtime<'_> {
impl Drop for Runtime {
fn drop(&mut self) {
unsafe {
wasm_runtime_destroy();
Expand Down Expand Up @@ -125,7 +123,7 @@ impl RuntimeBuilder {
/// # Errors
///
/// if the runtime initialization failed, it will return `RuntimeError::InitializationFailure`
pub fn build<'a>(mut self) -> Result<Runtime<'a>, RuntimeError> {
pub fn build(mut self) -> Result<Runtime, RuntimeError> {
match unsafe {
let module_name = &(self.host_functions).get_module_name();
self.args.native_module_name = module_name.as_ptr();
Expand All @@ -138,7 +136,6 @@ impl RuntimeBuilder {
} {
true => Ok(Runtime {
host_functions: self.host_functions,
_phantom: PhantomData,
}),
false => Err(RuntimeError::InitializationFailure),
}
Expand Down

0 comments on commit 9a910a1

Please sign in to comment.