add boa-test
This commit is contained in:
1352
__lang/boa-test/Cargo.lock
generated
Normal file
1352
__lang/boa-test/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
7
__lang/boa-test/Cargo.toml
Normal file
7
__lang/boa-test/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "boa-test"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
boa_engine = { version = "0.21.1", features = ["js"] }
|
||||
42
__lang/boa-test/src/main.rs
Normal file
42
__lang/boa-test/src/main.rs
Normal 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(())
|
||||
}
|
||||
Reference in New Issue
Block a user