add boa-test

This commit is contained in:
2026-04-03 00:41:42 +08:00
parent 949b99c356
commit 093cfe5c9b
3 changed files with 1401 additions and 0 deletions

1352
__lang/boa-test/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,7 @@
[package]
name = "boa-test"
version = "0.1.0"
edition = "2024"
[dependencies]
boa_engine = { version = "0.21.1", features = ["js"] }

View File

@@ -0,0 +1,42 @@
use boa_engine::{Context, JsResult, JsString, JsValue, NativeFunction, Source};
use std::str::FromStr;
fn main() -> JsResult<()> {
let js_code = r#"
let two = 1 + 1;
let definitely_not_four = two + "2";
println(definitely_not_four);
println(0.1 + 0.2);
println(Date.now(), 1, new Date()+'');
"#;
// Instantiate the execution context
let mut context = Context::default();
let println =
|_this: &JsValue, args: &[JsValue], _context: &mut Context| -> JsResult<JsValue> {
let mut vals = vec![];
args.iter()
.for_each(|arg| vals.push(arg.display().to_string()));
println!("{}", vals.join(" "));
Ok(JsValue::undefined())
};
_ = context.register_global_callable(
JsString::from_str("println").unwrap(),
0,
NativeFunction::from_copy_closure(println),
);
// Parse the source code
let result = context.eval(Source::from_bytes(js_code))?;
if !result.is_undefined() {
println!("{}", result.display());
}
Ok(())
}