pub struct MiniV8 { /* private fields */ }
Implementations§
Source§impl MiniV8
impl MiniV8
pub fn new() -> MiniV8
Sourcepub fn eval<S, R>(&self, script: S) -> Result<R>
pub fn eval<S, R>(&self, script: S) -> Result<R>
Executes a JavaScript script and returns its result.
Sourcepub fn set_user_data<K, T>(&self, key: K, data: T) -> Option<Box<dyn Any>>
pub fn set_user_data<K, T>(&self, key: K, data: T) -> Option<Box<dyn Any>>
Inserts any sort of keyed value of type T
into the MiniV8
, typically for later retrieval
from within Rust functions called from within JavaScript. If a value already exists with the
key, it is returned.
Sourcepub fn use_user_data<F, T: Any, U>(&self, key: &str, func: F) -> U
pub fn use_user_data<F, T: Any, U>(&self, key: &str, func: F) -> U
Calls a function with a user data value by its key, or None
if no value exists with the
key. If a value exists but it is not of the type T
, None
is returned. This is typically
used by a Rust function called from within JavaScript.
Sourcepub fn remove_user_data(&self, key: &str) -> Option<Box<dyn Any>>
pub fn remove_user_data(&self, key: &str) -> Option<Box<dyn Any>>
Removes and returns a user data value by its key. Returns None
if no value exists with the
key.
Sourcepub fn create_string(&self, value: &str) -> String
pub fn create_string(&self, value: &str) -> String
Creates and returns a string managed by V8.
§Panics
Panics if source value is longer than (1 << 28) - 16
bytes.
Sourcepub fn create_array(&self) -> Array
pub fn create_array(&self) -> Array
Creates and returns an empty Array
managed by V8.
Sourcepub fn create_object(&self) -> Object
pub fn create_object(&self) -> Object
Creates and returns an empty Object
managed by V8.
Sourcepub fn create_object_from<K, V, I>(&self, iter: I) -> Result<Object>
pub fn create_object_from<K, V, I>(&self, iter: I) -> Result<Object>
Creates and returns an Object
managed by V8 filled with the keys and values from an
iterator. Keys are coerced to object properties.
This is a thin wrapper around MiniV8::create_object
and Object::set
. See Object::set
for how this method might return an error.
Sourcepub fn create_function<F, R>(&self, func: F) -> Function
pub fn create_function<F, R>(&self, func: F) -> Function
Wraps a Rust function or closure, creating a callable JavaScript function handle to it.
The function’s return value is always a Result
: If the function returns Err
, the error
is raised as a JavaScript exception, which can be caught within JavaScript or bubbled up
back into Rust by not catching it. This allows using the ?
operator to propagate errors
through intermediate JavaScript code.
If the function returns Ok
, the contained value will be converted to a JavaScript value.
For details on Rust-to-JavaScript conversions, refer to the ToValue
and ToValues
traits.
If the provided function panics, the executable will be aborted.
Sourcepub fn create_function_mut<F, R>(&self, func: F) -> Function
pub fn create_function_mut<F, R>(&self, func: F) -> Function
Wraps a mutable Rust closure, creating a callable JavaScript function handle to it.
This is a version of create_function
that accepts a FnMut argument. Refer to
create_function
for more information about the implementation.