Skip to content

Commit ccf7f85

Browse files
committed
refactor(runtime): move $262 to boa_runtime, add --test262-object to cli
Move `$262` implementation from `tests/tester/src/exec/js262.rs` to `core/runtime/src/test262.rs` behind a new `test262` feature gate (pulls `bus` as optional dep). Add `annex-b` feature to boa_runtime forwarding to `boa_engine/annex-b` for `$262.IsHTMLDDA`. Both cli and boa_tester now use `boa_runtime/test262`, removing the tester's direct `bus` and `boa_gc` deps. CLI registers `$262` when `--test262-object` flag is passed. This change makes CLI more suitable for running test262 via an external harness such as eshost + test262-harness. The exact same `$262` object as used by the internal boa_tester can now be directly accessed in CLI. Existing `$boa` object doesn't expose some of the methods (like IsHTMLDDA, detachArrayBuffer and agent helpers) needed for full test262 support, and would have required users to maintain a custom shim mapping it to `$262`. Fixes #5054
1 parent 66a1cd2 commit ccf7f85

11 files changed

Lines changed: 43 additions & 37 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ Options:
164164
--flowgraph [<FORMAT>] Generate instruction flowgraph. Default is Graphviz [possible values: graphviz, mermaid]
165165
--flowgraph-direction <FORMAT> Specifies the direction of the flowgraph. Default is top-top-bottom [possible values: top-to-bottom, bottom-to-top, left-to-right, right-to-left]
166166
--debug-object Inject debugging object `$boa`
167+
--test262-object Inject the test262 host object `$262`
167168
-m, --module Treats the input files as modules
168169
-r, --root <ROOT> Root path from where the module resolver will try to load the modules [default: .]
169170
-h, --help Print help (see more with '--help')

cli/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ rust-version.workspace = true
1515
boa_engine = { workspace = true, features = ["deser", "float16", "flowgraph", "temporal", "trace", "xsum"] }
1616
boa_parser.workspace = true
1717
boa_gc.workspace = true
18-
boa_runtime.workspace = true
18+
boa_runtime = { workspace = true, features = ["test262"] }
1919
rustyline = { workspace = true, features = ["derive", "with-file-history"] }
2020
clap = { workspace = true, features = ["derive"] }
2121
serde_json.workspace = true
@@ -33,6 +33,7 @@ rustls.workspace = true
3333
[features]
3434
default = [
3535
"boa_engine/annex-b",
36+
"boa_runtime/annex-b",
3637
"boa_engine/experimental",
3738
"boa_engine/intl_bundled",
3839
"boa_engine/native-backtrace",

cli/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Options:
5858
--flowgraph [<FORMAT>] Generate instruction flowgraph. Default is Graphviz [possible values: graphviz, mermaid]
5959
--flowgraph-direction <FORMAT> Specifies the direction of the flowgraph. Default is top-top-bottom [possible values: top-to-bottom, bottom-to-top, left-to-right, right-to-left]
6060
--debug-object Inject debugging object `$boa`
61+
--test262-object Inject the test262 host object `$262`
6162
-m, --module Treats the input files as modules
6263
-r, --root <ROOT> Root path from where the module resolver will try to load the modules [default: .]
6364
-e, --expression <EXPR> Execute a JavaScript expression then exit

cli/src/main.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ struct Opt {
153153
#[arg(long)]
154154
debug_object: bool,
155155

156+
/// Inject the test262 host object `$262`.
157+
#[arg(long)]
158+
test262_object: bool,
159+
156160
/// Treats the input files as modules.
157161
#[arg(long, short = 'm', group = "mod")]
158162
module: bool,
@@ -562,6 +566,14 @@ fn main() -> Result<()> {
562566
init_boa_debug_object(context);
563567
}
564568

569+
if args.test262_object {
570+
boa_runtime::test262::register_js262(
571+
boa_runtime::test262::WorkerHandles::new(),
572+
true, // register `console` in $262.agent worker threads
573+
context,
574+
);
575+
}
576+
565577
// Configure optimizer options
566578
let mut optimizer_options = OptimizerOptions::empty();
567579
optimizer_options.set(OptimizerOptions::STATISTICS, args.optimizer_statistics);

core/runtime/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ rust-version.workspace = true
1414
boa_engine.workspace = true
1515
base64.workspace = true
1616
boa_gc.workspace = true
17+
bus = { workspace = true, optional = true }
1718
bytemuck.workspace = true
1819
either = { workspace = true, optional = true }
1920
futures = "0.3.32"
@@ -57,3 +58,5 @@ fetch = [
5758
]
5859
reqwest-blocking = ["dep:reqwest", "reqwest/blocking"]
5960
process = []
61+
annex-b = ["boa_engine/annex-b"]
62+
test262 = ["dep:bus"]

core/runtime/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ pub mod microtask;
123123
#[cfg(feature = "process")]
124124
pub mod process;
125125
pub mod store;
126+
#[cfg(feature = "test262")]
127+
pub mod test262;
126128
pub mod text;
127129
#[cfg(feature = "url")]
128130
pub mod url;
Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use std::{
22
cell::RefCell,
33
rc::Rc,
4-
sync::mpsc::{self, Sender},
4+
sync::{
5+
OnceLock,
6+
mpsc::{self, Sender},
7+
},
58
thread::JoinHandle,
6-
time::Duration,
9+
time::{Duration, Instant},
710
};
811

912
#[cfg(feature = "annex-b")]
@@ -18,25 +21,27 @@ use boa_engine::{
1821
};
1922
use bus::BusReader;
2023

21-
use crate::START;
24+
/// Monotonic clock for `$262.agent.monotonicNow()`, initialized by `register_js262()`.
25+
static START: OnceLock<Instant> = OnceLock::new();
2226

23-
pub(super) enum WorkerResult {
27+
pub enum WorkerResult {
2428
Ok,
2529
Err(String),
2630
Panic(String),
2731
}
2832

29-
pub(super) type WorkerHandle = JoinHandle<Result<(), String>>;
33+
type WorkerHandle = JoinHandle<Result<(), String>>;
3034

3135
#[derive(Debug, Clone)]
32-
pub(super) struct WorkerHandles(Rc<RefCell<Vec<WorkerHandle>>>);
36+
pub struct WorkerHandles(Rc<RefCell<Vec<WorkerHandle>>>);
3337

3438
impl WorkerHandles {
35-
pub(super) fn new() -> Self {
39+
#[must_use]
40+
pub fn new() -> Self {
3641
Self(Rc::default())
3742
}
3843

39-
pub(super) fn join_all(&mut self) -> Vec<WorkerResult> {
44+
pub fn join_all(&mut self) -> Vec<WorkerResult> {
4045
let handles = std::mem::take(&mut *self.0.borrow_mut());
4146

4247
handles
@@ -71,11 +76,8 @@ impl Drop for WorkerHandles {
7176
}
7277

7378
/// Creates the object $262 in the context.
74-
pub(super) fn register_js262(
75-
handles: WorkerHandles,
76-
console: bool,
77-
context: &mut Context,
78-
) -> JsObject {
79+
pub fn register_js262(handles: WorkerHandles, console: bool, context: &mut Context) -> JsObject {
80+
START.get_or_init(Instant::now);
7981
let global_obj = context.global_object();
8082

8183
let agent = agent_obj(handles, console, context);
@@ -235,14 +237,10 @@ fn agent_obj(handles: WorkerHandles, console: bool, context: &mut Context) -> Js
235237
register_js262_worker(rx, tx, context);
236238

237239
if console {
238-
let console = boa_runtime::Console::init(context);
240+
let console = crate::Console::init(context);
239241

240242
context
241-
.register_global_property(
242-
boa_runtime::Console::NAME,
243-
console,
244-
Attribute::all(),
245-
)
243+
.register_global_property(crate::Console::NAME, console, Attribute::all())
246244
.expect("the console builtin shouldn't exist");
247245
}
248246

tests/tester/Cargo.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ rust-version.workspace = true
1313

1414
[dependencies]
1515
boa_engine = { workspace = true, features = ["float16"] }
16-
boa_runtime.workspace = true
17-
boa_gc.workspace = true
16+
boa_runtime = { workspace = true, features = ["test262"] }
1817
clap = { workspace = true, features = ["derive"] }
1918
serde = { workspace = true, features = ["derive"] }
2019
serde_yaml = "0.9.34" # TODO: Track https://github.com/saphyr-rs/saphyr.
@@ -28,11 +27,10 @@ color-eyre.workspace = true
2827
phf = { workspace = true, features = ["macros"] }
2928
comfy-table.workspace = true
3029
serde_repr.workspace = true
31-
bus.workspace = true
3230
cow-utils.workspace = true
3331

3432
[features]
35-
annex-b = ["boa_engine/annex-b"]
33+
annex-b = ["boa_engine/annex-b", "boa_runtime/annex-b"]
3634
default = ["boa_engine/intl_bundled", "boa_engine/experimental", "annex-b"]
3735

3836
[lints]

tests/tester/src/exec/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Execution module for the test runner.
22
3-
mod js262;
3+
use boa_runtime::test262 as js262;
44

55
use crate::{
66
Harness, Outcome, Phase, SpecEdition, Statistics, SuiteResult, Test, TestFlags,
@@ -23,7 +23,7 @@ use rayon::prelude::*;
2323
use rustc_hash::FxHashSet;
2424
use std::{cell::RefCell, eprintln, path::Path, rc::Rc};
2525

26-
use self::js262::WorkerHandles;
26+
use js262::WorkerHandles;
2727

2828
impl TestSuite {
2929
/// Runs the test suite.

0 commit comments

Comments
 (0)