Rust编写的JavaScript引擎,该项目是一个试验性质的项目。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

36 lines
941 B

#![allow(unused_crate_dependencies)]
use boa_engine::value::TryFromJs;
use boa_engine::{js_string, Context, JsResult, JsValue, Source};
use boa_string::JsString;
#[test]
fn try_from_js_derive() {
#[derive(Debug, TryFromJs, Eq, PartialEq)]
struct TryFromJsTest {
a: JsString,
#[boa(rename = "bBB")]
b: i32,
#[boa(from_js_with = "check_tfj_called")]
c: i32,
}
fn check_tfj_called(value: &JsValue, context: &mut Context) -> JsResult<i32> {
let v = value.to_i32(context)?;
Ok(v / 2)
}
let mut context = Context::default();
let obj = context
.eval(Source::from_bytes(br#"({ a: "hello", bBB: 42, c: 120 })"#))
.unwrap();
let result = TryFromJsTest::try_from_js(&obj, &mut context).unwrap();
assert_eq!(
result,
TryFromJsTest {
a: js_string!("hello"),
b: 42,
c: 60
}
);
}