[][src]Function qjs::eval

pub fn eval<T: Source, V: ExtractValue>(source: T) -> Result<Option<V>, Error>

Evaluate a script or module source.

The eval function accept the source code &str, filename &Path or precompiled bytecode &[u8], and returns the primitive value as you special, including bool, i32, i64, u64, f64 or String.

  • The Javascript undefined and null value will be returned as None.
  • The Javascript exception will be convert to a ErrorKind error.

Examples

The eval function accept the source code &str and returns the primitive value.

let v: Option<i32> = qjs::eval("1+2").unwrap();

assert_eq!(v, Some(3));

The Javascript exception will be convert to a ErrorKind error.

assert_eq!(
    qjs::eval::<_, ()>("throw new Error('Whoops!')")
        .unwrap_err()
        .downcast::<qjs::ErrorKind>()
        .unwrap(),
    qjs::ErrorKind::Error(
        "Whoops!".into(),
        Some("    at <eval> (<evalScript>)\n".into())
    )
);