Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add execution environment #35

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/execution_environment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use wamr_sys::{
wasm_exec_env_t, wasm_runtime_get_call_stack_buf_size, wasm_runtime_get_exec_env_singleton,
wasm_runtime_get_user_data, wasm_runtime_set_user_data,
};

use crate::instance::Instance;

pub struct ExecutionEnvironment(wasm_exec_env_t);

impl ExecutionEnvironment {
pub fn from_instance(instance: &Instance) -> Self {
Self(unsafe { wasm_runtime_get_exec_env_singleton(instance.get_inner_instance()) })
}

/// Set user data for the current execution environment.
/// This is useful when you want to pass some data to the host native functions.
/// The user data can be retrieved by calling `get_user_data()`.
///
/// # Safety
/// Be careful when using this function, as it can lead to undefined behavior if misused.
pub unsafe fn set_user_data<T>(&self, user_data: &mut T) {
wasm_runtime_set_user_data(
self.get_inner_execution_environment(),
user_data as *mut T as *mut std::ffi::c_void,
);
}

/// Get user data for the current execution environment.
/// This is useful when you want to pass some data to the host native functions.
/// The user data can be set by calling `set_user_data()`.
///
/// # Safety
/// Be careful when using this function, as it can lead to undefined behavior if misused.
#[allow(clippy::mut_from_ref)]
pub unsafe fn get_user_data<T>(&self) -> Option<&mut T> {
let pointer = wasm_runtime_get_user_data(self.get_inner_execution_environment()) as *mut T;
pointer.as_mut()
}

pub fn get_call_stack_buffer_size(&self) -> usize {
unsafe {
wasm_runtime_get_call_stack_buf_size(self.get_inner_execution_environment()) as usize
}
}

pub(crate) fn get_inner_execution_environment(&self) -> wasm_exec_env_t {
self.0
}
}
17 changes: 14 additions & 3 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ use core::ffi::c_char;

use wamr_sys::{
wasm_module_inst_t, wasm_runtime_deinstantiate, wasm_runtime_destroy_thread_env,
wasm_runtime_init_thread_env, wasm_runtime_instantiate,
wasm_runtime_get_module_inst, wasm_runtime_init_thread_env, wasm_runtime_instantiate,
};

use crate::{
helper::error_buf_to_string, helper::DEFAULT_ERROR_BUF_SIZE, module::Module, runtime::Runtime,
RuntimeError,
execution_environment::ExecutionEnvironment, helper::error_buf_to_string,
helper::DEFAULT_ERROR_BUF_SIZE, module::Module, runtime::Runtime, RuntimeError,
};

#[derive(Debug)]
Expand All @@ -26,6 +26,17 @@ pub struct Instance {
}

impl Instance {
/// This function get the execution environment from the instance.
pub fn from_execution_environment(execution_environment: &ExecutionEnvironment) -> Self {
Self {
instance: unsafe {
wasm_runtime_get_module_inst(
execution_environment.get_inner_execution_environment(),
)
},
}
}

/// instantiate a module with stack size
///
/// # Error
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ use std::error;
use std::fmt;
use std::io;

pub mod execution_environment;
pub mod function;
mod helper;
pub mod host_function;
Expand Down