Skip to main content

Sandbox

Struct Sandbox 

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

A JavaScript runtime that rule code executes in.

Not Sync: the underlying engine runtime is single-threaded, so each rayon worker owns one. They share a RunClock so the global budget is measured once for the run rather than once per worker.

Implementations§

Source§

impl Sandbox

Source

pub fn new(limits: Limits, clock: Arc<RunClock>) -> Result<Self, SandboxError>

Build a sandbox sharing a run clock.

§Errors

Returns SandboxError::Engine if the runtime cannot be created or the bootstrap fails, both of which indicate a broken build rather than anything about a rule.

Source

pub fn with_modules( limits: Limits, clock: Arc<RunClock>, root: RuleRoot, typescript: Arc<dyn Language>, javascript: Arc<dyn Language>, ) -> Result<Self, SandboxError>

Build a sandbox that can load rule modules from a rules root.

§Errors

As Sandbox::new.

Source

pub fn loaded_modules(&self) -> Option<&LoadedModules>

Every module loaded so far, when this sandbox was built with a module root.

The hash of the rule graph is derived from this, so it has to reflect what was actually read rather than what the config named.

Source

pub fn import_default<T>(&self, path: &Path) -> Result<T, SandboxError>
where T: for<'js> FromJs<'js>,

Import a rule module and return its default export.

§Errors

Returns SandboxError on a breached budget, a module that fails to resolve or load, or a module that throws while evaluating.

Source

pub fn with_limits(limits: Limits) -> Result<Self, SandboxError>

Build a sandbox with its own run clock, starting now.

For a single-threaded run or a test. A real run shares one clock across workers.

§Errors

As Sandbox::new.

Source

pub const fn limits(&self) -> &Limits

The budgets in force.

Source

pub fn eval<T>(&self, source: &str) -> Result<T, SandboxError>
where T: for<'js> FromJs<'js>,

Evaluate source, enforcing the per-invocation budget.

§Errors

Returns SandboxError on a breached budget or a thrown value. Every variant cancels the run.

Source

pub fn eval_with_timeout<T>( &self, source: &str, timeout: Duration, ) -> Result<T, SandboxError>
where T: for<'js> FromJs<'js>,

Evaluate source under an explicit per-invocation budget, for a rule that declared its own.

§Errors

As Sandbox::eval.

Source

pub fn eval_module(&self, name: &str, source: &str) -> Result<(), SandboxError>

Evaluate a synthetic module under a chosen name.

The name matters: the resolver treats it as the importing module’s path, so a synthetic entry has to sit inside the rules root for relative specifiers in its source to resolve. Naming it outside would make every import look like an escape.

§Errors

As Sandbox::eval.

Source

pub fn eval_with_host<T>( &self, host: &HostContext, source: &str, ) -> Result<T, SandboxError>
where T: for<'js> FromJs<'js>,

Evaluate source with a ctx object in scope, the way a rule handler runs.

§Errors

As Sandbox::eval.

Source

pub fn eval_with_host_timeout<T>( &self, host: &HostContext, source: &str, timeout: Duration, ) -> Result<T, SandboxError>
where T: for<'js> FromJs<'js>,

Evaluate with ctx in scope under an explicit budget, for a rule that declared one.

§Errors

As Sandbox::eval.

Source

pub fn eval_with_reduce_host<T>( &self, host: &ReduceContext, source: &str, timeout: Duration, ) -> Result<T, SandboxError>
where T: for<'js> FromJs<'js>,

Evaluate with a reduce-phase ctx in scope, under an explicit budget.

A separate entry point rather than a flag on the one above, because the two contexts expose different surfaces on purpose — facts and files here, emitFact and the tree there. A single builder that switched on a boolean would make it possible to get the wrong one, which is precisely what must not happen.

§Errors

As Sandbox::eval.

Trait Implementations§

Source§

impl Debug for Sandbox

Source§

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

Formats the value using the given formatter. 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, 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> ParallelSend for T

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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.