evaluate_script

Function evaluate_script 

Source
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 as this, or None to use the global object as this.
  • source_url: An optional string containing a URL for the script’s source file. This is used by debuggers and when reporting exceptions. Pass None if 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 at source_url. This is only used when reporting exceptions. The value is one-based, so the first line is line 1 and invalid values are clamped to 1.

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);