[−][src]Struct quick_js::Context
Methods
impl Context
[src]
pub fn new() -> Result<Self, ContextError>
[src]
pub fn eval(&self, code: &str) -> Result<JsValue, ExecutionError>
[src]
Evaluates Javascript code and returns the value of the final expression.
use quick_js::{Context, JsValue}; let context = Context::new().unwrap(); let value = context.eval(" 1 + 2 + 3 "); assert_eq!( value, Ok(JsValue::Int(6)), ); let value = context.eval(r#" function f() { return 55 * 3; } let y = f(); var x = y.toString() + "!" x "#); assert_eq!( value, Ok(JsValue::String("165!".to_string())), );
pub fn eval_as<R>(&self, code: &str) -> Result<R, ExecutionError> where
R: TryFrom<JsValue>,
R::Error: Into<ValueError>,
[src]
R: TryFrom<JsValue>,
R::Error: Into<ValueError>,
Evaluates Javascript code and returns the value of the final expression as a Rust type.
use quick_js::{Context}; let context = Context::new().unwrap(); let res = context.eval_as::<bool>(" 100 > 10 "); assert_eq!( res, Ok(true), ); let value: i32 = context.eval_as(" 10 + 10 ").unwrap(); assert_eq!( value, 20, );
pub fn call_function(
&self,
function_name: &str,
args: impl IntoIterator<Item = impl Into<JsValue>>
) -> Result<JsValue, ExecutionError>
[src]
&self,
function_name: &str,
args: impl IntoIterator<Item = impl Into<JsValue>>
) -> Result<JsValue, ExecutionError>
Call a global function in the Javascript namespace.
use quick_js::{Context, JsValue}; let context = Context::new().unwrap(); let res = context.call_function("encodeURIComponent", vec!["a=b"]); assert_eq!( res, Ok(JsValue::String("a%3Db".to_string())), );
pub fn add_callback<F>(
&self,
name: &str,
callback: impl Callback<F> + 'static
) -> Result<(), ExecutionError>
[src]
&self,
name: &str,
callback: impl Callback<F> + 'static
) -> Result<(), ExecutionError>
Add a global JS function that is backed by a Rust function or closure.
use quick_js::{Context, JsValue}; let context = Context::new().unwrap(); // Register a closue as a callback under the "add" name. // The 'add' function can now be called from Javascript code. context.add_callback("add", |a: i32, b: i32| { a + b }).unwrap(); // Now we try out the 'add' function via eval. let output = context.eval_as::<i32>(" add( 3 , 4 ) ").unwrap(); assert_eq!( output, 7, );
Auto Trait Implementations
impl Unpin for Context
impl !Sync for Context
impl !Send for Context
impl UnwindSafe for Context
impl RefUnwindSafe for Context
Blanket Implementations
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,