pub struct CompiledModule { /* private fields */ }Expand description
The pre-compiled QuickJS WASM module.
Created once, reused across executions. Thread-safe. Each execution instantiates a fresh WASM instance from this module.
Implementations§
Source§impl CompiledModule
impl CompiledModule
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Compile the embedded QuickJS WASM binary.
This is the expensive operation (~10-50ms). Do it once at startup.
For repeated runs, use load_cached() with a serialized module.
§Errors
Returns an error if the WASM fails to compile or instantiate.
Sourcepub fn serialize(&self) -> Result<Vec<u8>>
pub fn serialize(&self) -> Result<Vec<u8>>
Serialize the compiled module to bytes for caching.
Save the result to disk. On next startup, use load_cached()
to skip compilation (~50ms → ~1ms).
§Errors
Returns an error if the underlying module serialization fails.
Sourcepub fn load_cached(bytes: &[u8]) -> Result<Self>
pub fn load_cached(bytes: &[u8]) -> Result<Self>
Load a pre-compiled module from serialized bytes.
§Safety
The serialized bytes must have been produced by serialize()
from the same wasmtime version. Loading tampered bytes is
memory-safe (wasmtime validates) but may produce unexpected behavior.
§Errors
Returns an error if the engine or module deserialization fails.
Sourcepub fn new_cached(cache_path: &Path) -> Result<Self>
pub fn new_cached(cache_path: &Path) -> Result<Self>
Compile and cache to disk. On next call, loads from cache.
The cache file carries an 8-byte integrity tag so that trivially
tampered files are rejected before reaching the unsafe deserialize
path. On Unix, the file is created with mode 0o600.
§Errors
Returns an error if compilation fails.
Sourcepub fn module_ref(&self) -> &Module
pub fn module_ref(&self) -> &Module
Get a reference to the compiled WASM Module.
Sourcepub fn execute(
&self,
scripts: &[String],
bridge: Arc<dyn Bridge>,
config: &SandboxConfig,
) -> Result<ExecutionResult>
pub fn execute( &self, scripts: &[String], bridge: Arc<dyn Bridge>, config: &SandboxConfig, ) -> Result<ExecutionResult>
Execute JavaScript in a fresh sandboxed instance.
Each call creates a new WASM instance with isolated memory. No state leaks between executions.
§Errors
Returns an error if WASM execution traps or resources are exhausted.
Sourcepub fn execute_in_context(
&self,
scripts: &[String],
bridge: Arc<dyn Bridge>,
config: &SandboxConfig,
context_id: &ContextId,
message_bus: Option<&Mutex<MessageBus>>,
) -> Result<ExecutionResult>
pub fn execute_in_context( &self, scripts: &[String], bridge: Arc<dyn Bridge>, config: &SandboxConfig, context_id: &ContextId, message_bus: Option<&Mutex<MessageBus>>, ) -> Result<ExecutionResult>
Execute in a named context with access to the message bus.
§Errors
Returns an error if WASM execution traps or resources are exhausted.
Auto Trait Implementations§
impl Freeze for CompiledModule
impl !RefUnwindSafe for CompiledModule
impl Send for CompiledModule
impl Sync for CompiledModule
impl Unpin for CompiledModule
impl UnsafeUnpin for CompiledModule
impl !UnwindSafe for CompiledModule
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> 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