pub struct WasmSandbox { /* private fields */ }
Expand description
Main sandbox controller
Implementations§
Source§impl WasmSandbox
impl WasmSandbox
Sourcepub fn with_config(config: SandboxConfig) -> Result<Self>
pub fn with_config(config: SandboxConfig) -> Result<Self>
Create a sandbox with custom configuration
Sourcepub fn load_module(&self, wasm_bytes: &[u8]) -> Result<ModuleId>
pub fn load_module(&self, wasm_bytes: &[u8]) -> Result<ModuleId>
Load a WASM module
Sourcepub fn create_instance(
&mut self,
module_id: ModuleId,
instance_config: Option<InstanceConfig>,
) -> Result<InstanceId>
pub fn create_instance( &mut self, module_id: ModuleId, instance_config: Option<InstanceConfig>, ) -> Result<InstanceId>
Create a new instance of a module
Sourcepub async fn call_function<P, R>(
&self,
instance_id: InstanceId,
function_name: &str,
params: P,
) -> Result<R>where
P: Serialize + 'static,
R: for<'de> Deserialize<'de> + 'static,
pub async fn call_function<P, R>(
&self,
instance_id: InstanceId,
function_name: &str,
params: P,
) -> Result<R>where
P: Serialize + 'static,
R: for<'de> Deserialize<'de> + 'static,
Run a function in the sandbox
Sourcepub async fn from_source(source_path: &str) -> Result<Self>
pub async fn from_source(source_path: &str) -> Result<Self>
Create a new sandbox from source code with automatic compilation and configuration.
This is the easiest way to get started - just point to a source file and the sandbox will automatically detect the language, compile it to WebAssembly, and set up safe defaults.
§Examples
use wasm_sandbox::WasmSandbox;
#[tokio::main]
async fn main() -> Result<(), wasm_sandbox::Error> {
// Rust source file
let sandbox = WasmSandbox::from_source("./calculator.rs").await?;
let result: i32 = sandbox.call("add", &(5, 3)).await?;
// Python source (when supported)
let sandbox = WasmSandbox::from_source("./processor.py").await?;
Ok(())
}
Sourcepub fn builder() -> WasmSandboxBuilder
pub fn builder() -> WasmSandboxBuilder
Create a new sandbox builder for more control over configuration.
§Examples
use wasm_sandbox::WasmSandbox;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), wasm_sandbox::Error> {
let sandbox = WasmSandbox::builder()
.source("./my_program.rs")
.timeout_duration(Duration::from_secs(30))
.memory_limit(64 * 1024 * 1024) // 64MB
.enable_file_access(false)
.build()
.await?;
Ok(())
}
Sourcepub async fn call<P, R>(&self, function_name: &str, params: &P) -> Result<R>
pub async fn call<P, R>(&self, function_name: &str, params: &P) -> Result<R>
Call a function in the sandbox with automatic instance management.
This method automatically creates an instance if needed and calls the function. For more control, use the explicit instance creation methods.
Sourcepub async fn execute_main(&self, args: &[&str]) -> Result<String>
pub async fn execute_main(&self, args: &[&str]) -> Result<String>
Execute a complete program with command-line arguments.
This calls the main function of the WebAssembly module with the provided arguments.
Sourcepub fn runtime(&self) -> &dyn WasmRuntime
pub fn runtime(&self) -> &dyn WasmRuntime
Get a reference to the runtime
Sourcepub fn runtime_mut(&mut self) -> &mut dyn WasmRuntime
pub fn runtime_mut(&mut self) -> &mut dyn WasmRuntime
Get a mutable reference to the runtime
Sourcepub fn get_instance(&self, instance_id: InstanceId) -> Option<&SandboxInstance>
pub fn get_instance(&self, instance_id: InstanceId) -> Option<&SandboxInstance>
Get a reference to an instance
Sourcepub fn get_instance_mut(
&mut self,
instance_id: InstanceId,
) -> Option<&mut SandboxInstance>
pub fn get_instance_mut( &mut self, instance_id: InstanceId, ) -> Option<&mut SandboxInstance>
Get a mutable reference to an instance
Sourcepub fn remove_instance(
&mut self,
instance_id: InstanceId,
) -> Option<SandboxInstance>
pub fn remove_instance( &mut self, instance_id: InstanceId, ) -> Option<SandboxInstance>
Remove an instance
Sourcepub fn instance_ids(&self) -> Vec<InstanceId>
pub fn instance_ids(&self) -> Vec<InstanceId>
Get all instance IDs
Sourcepub fn get_instance_resource_usage(
&self,
instance_id: InstanceId,
) -> Result<DetailedResourceUsage>
pub fn get_instance_resource_usage( &self, instance_id: InstanceId, ) -> Result<DetailedResourceUsage>
Get resource usage for a specific instance
Sourcepub fn reset_instance(&mut self, instance_id: InstanceId) -> Result<()>
pub fn reset_instance(&mut self, instance_id: InstanceId) -> Result<()>
Reset an instance (recreate it with the same configuration)
Auto Trait Implementations§
impl Freeze for WasmSandbox
impl !RefUnwindSafe for WasmSandbox
impl Send for WasmSandbox
impl Sync for WasmSandbox
impl Unpin for WasmSandbox
impl !UnwindSafe for WasmSandbox
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> GetSetFdFlags for T
impl<T> GetSetFdFlags for T
Source§fn get_fd_flags(&self) -> Result<FdFlags, Error>where
T: AsFilelike,
fn get_fd_flags(&self) -> Result<FdFlags, Error>where
T: AsFilelike,
self
file descriptor.Source§fn new_set_fd_flags(&self, fd_flags: FdFlags) -> Result<SetFdFlags<T>, Error>where
T: AsFilelike,
fn new_set_fd_flags(&self, fd_flags: FdFlags) -> Result<SetFdFlags<T>, Error>where
T: AsFilelike,
Source§fn set_fd_flags(&mut self, set_fd_flags: SetFdFlags<T>) -> Result<(), Error>where
T: AsFilelike,
fn set_fd_flags(&mut self, set_fd_flags: SetFdFlags<T>) -> Result<(), Error>where
T: AsFilelike,
self
file descriptor. Read moreSource§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more