Skip to main content

CompiledModule

Struct CompiledModule 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn engine(&self) -> &Engine

Get a reference to the wasmtime Engine.

Source

pub fn module_ref(&self) -> &Module

Get a reference to the compiled WASM Module.

Source

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.

Source

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.

Source

pub fn snapshot_memory(store: &Store<HostState>, memory: &Memory) -> Vec<u8>

Snapshot the WASM linear memory for state save/restore.

Returns a byte vector that can be restored later. Cost: ~1μs for a 4MB instance (just memcpy).

Source

pub fn restore_memory( store: &mut Store<HostState>, memory: &Memory, snapshot: &[u8], )

Restore WASM linear memory from a snapshot.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.