Skip to main content

ProtoJSSandbox

Struct ProtoJSSandbox 

Source
pub struct ProtoJSSandbox { /* private fields */ }
Expand description

A Hyperlight Sandbox with no JavaScript run time loaded and no guest code. This is used to register new host functions prior to loading the JavaScript runtime. A Hyperlight Sandbox with no JavaScript run time loaded and no guest code. This is used to register new host functions prior to loading the JavaScript run time.

Implementations§

Source§

impl ProtoJSSandbox

Source

pub fn set_module_loader<Fs: FileSystem + Clone + 'static>( self, file_system: Fs, ) -> Result<Self>

Install a custom file system for module resolution and loading.

Enables JavaScript module imports using the provided FileSystem implementation.

Source

pub fn load_runtime(self) -> Result<JSSandbox>

Load the JavaScript runtime into the sandbox.

Source

pub fn host_module( &mut self, name: impl Into<String> + Debug, ) -> &mut HostModule

Register a host module that can be called from the guest JavaScript code.

This method should be called before ProtoJSSandbox::load_runtime, while the sandbox is still in its “proto” (uninitialized) state. After load_runtime is called, the set of host modules and functions is fixed for the resulting JSSandbox.

Calling this method multiple times with the same name refers to the same module; additional calls will reuse the existing module instance and allow you to register more functions on it. The first call creates the module and subsequent calls return the previously created module.

Module names are matched by exact string equality from the guest JavaScript environment. They should be valid UTF‑8 strings and while there is no explicit restriction on special characters, using simple, ASCII identifiers (e.g. "fs", "net", "my_module") is recommended for portability and clarity.

§Example
use hyperlight_js::SandboxBuilder;

// Create a proto sandbox and register a host function.
let mut sbox = SandboxBuilder::new().build()?;

// Register a module and a function on it before loading the runtime.
sbox.host_module("math").register("add", |a: i32, b: i32| a + b);

// Once all host modules/functions are registered, load the JS runtime.
let js_sandbox = sbox.load_runtime()?;
Source

pub fn register<Output: Serialize, Args: DeserializeOwned>( &mut self, module: impl Into<String> + Debug, name: impl Into<String> + Debug, func: impl Function<Output, Args> + Send + Sync + 'static, ) -> Result<()>

Register a host function that can be called from the guest JavaScript code. This is equivalent to calling sbox.host_module(module).register(name, func).

Registering a function with the same module and name as an existing function overwrites the previous registration.

Trait Implementations§

Source§

impl Debug for ProtoJSSandbox

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ProtoJSSandbox

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more