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, AnyError>
pub fn from_string(js_code: &str) -> Result<Self, AnyError>
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, AnyError>
pub fn from_file(file: impl AsRef<Path>) -> Result<Self, AnyError>
Initialize a script by loading it from a .js file
Returns a new object on success. Fails if the file cannot be opened or in case of syntax or initialization error with the code.
Sourcepub fn call<P, R>(&mut self, fn_name: &str, args: &P) -> Result<R, AnyError>where
P: Serialize,
R: DeserializeOwned,
pub fn call<P, R>(&mut self, fn_name: &str, args: &P) -> Result<R, AnyError>where
P: Serialize,
R: DeserializeOwned,
Invokes a JavaScript function.
Passes a single argument args
to JS by serializing it to JSON (using serde_json).
Multiple arguments are currently not supported, but can easily be emulated using a Vec
to work as a JSON array.