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
impl Script
sourcepub fn from_string(js_code: &str) -> Result<Self, JsError>
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.
sourcepub fn from_file(file: impl AsRef<Path>) -> Result<Self, JsError>
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.
pub fn new() -> Self
pub fn add_script( &mut self, namespace: &str, fn_name: &str, js_code: &str ) -> Result<(), JsError>
sourcepub fn with_timeout(self, timeout: Duration) -> Self
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.
sourcepub fn call<A, R>(&mut self, fn_name: &str, args_tuple: A) -> Result<R, JsError>where
A: CallArgs,
R: DeserializeOwned,
pub fn call<A, R>(&mut self, fn_name: &str, args_tuple: A) -> Result<R, JsError>where
A: CallArgs,
R: DeserializeOwned,
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.