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
impl ProtoJSSandbox
Sourcepub fn set_module_loader<Fs: FileSystem + Clone + 'static>(
self,
file_system: Fs,
) -> Result<Self>
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.
Sourcepub fn load_runtime(self) -> Result<JSSandbox>
pub fn load_runtime(self) -> Result<JSSandbox>
Load the JavaScript runtime into the sandbox.
Sourcepub fn host_module(
&mut self,
name: impl Into<String> + Debug,
) -> &mut HostModule
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()?;Sourcepub 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<()>
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.