Struct js_sandbox_ios::Script

source ·
pub struct Script { /* private fields */ }
Expand description

Represents a single JavaScript file that can be executed.

The code can be loaded from a file or from a string in memory. A typical usage pattern is to load a file with one or more JS function definitions, and then call those functions from Rust.

Implementations§

source§

impl Script

source

pub fn from_string(js_code: &str) -> Result<Self, JsError>

Initialize a script with the given JavaScript source code.

Returns a new object on success, and an error in case of syntax or initialization error with the code.

source

pub fn from_file(file: impl AsRef<Path>) -> Result<Self, JsError>

Initialize a script by loading it from a .js file.

To load a file at compile time, you can use Self::from_string() in combination with the include_str! macro. At the moment, a script is limited to a single file, and you will need to do bundling yourself (e.g. with esbuild).

Returns a new object on success. Fails if the file cannot be opened or in case of syntax or initialization error with the code.

source

pub fn new() -> Self

source

pub fn add_script( &mut self, namespace: &str, fn_name: &str, js_code: &str ) -> Result<(), JsError>

source

pub fn with_timeout(self, timeout: Duration) -> Self

Equips this script with a timeout, meaning that any function call is aborted after the specified duration.

This requires creating a separate thread for each function call, which tracks time and pulls the plug if the JS function does not return in time. Use this for untrusted 3rd-party code, not if you know that your functions always return.

Panics with invalid timeouts or if this script already has a timeout set.

source

pub fn call<A, R>(&mut self, fn_name: &str, args_tuple: A) -> Result<R, JsError>

Invokes a JavaScript function.

Blocks on asynchronous functions until completion.

args_tuple needs to be a tuple.

Each tuple element is converted to JSON (using serde_json) and passed as a distinct argument to the JS function.

source

pub fn call_namespace<A, R>( &mut self, namespace: &str, arg: A ) -> Result<R, JsError>

source

pub fn bind_api<'a, A>(&'a mut self) -> A
where A: JsApi<'a>,

Trait Implementations§

source§

impl Debug for Script

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Script

§

impl !RefUnwindSafe for Script

§

impl !Send for Script

§

impl !Sync for Script

§

impl Unpin for Script

§

impl !UnwindSafe for Script

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

§

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>,

§

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.