pub fn evaluate_script<S: Into<JSString>, U: Into<JSString>>(
ctx: &JSContext,
script: S,
this_object: Option<&JSObject>,
source_url: U,
starting_line_number: i32,
) -> Result<JSValue, JSException>Expand description
Evaluates a string of JavaScript.
ctx: The execution context to use.script: A string containing the script to evaluate.this_object: The optional object to use asthis, orNoneto use the global object asthis.source_url: An optional string containing a URL for the script’s source file. This is used by debuggers and when reporting exceptions. PassNoneif you do not care to include source file information.starting_line_number: An integer value specifying the script’s starting line number in the file located atsource_url. This is only used when reporting exceptions. The value is one-based, so the first line is line1and invalid values are clamped to1.
Returns either the JSValue that results from evaluating the script or
the exception that occurred.
use javascriptcore::*;
let ctx = JSContext::default();
let r = evaluate_script(&ctx, "2 + 2", None, "test.js", 1);
assert_eq!(r.unwrap().as_number().unwrap(), 4.0);