Skip to content

Commit

Permalink
Add WPT as optional tests for boa_runtime
Browse files Browse the repository at this point in the history
To run the tests, you require adding the `wpt-tests` feature
and setting the `WPT_ROOT` environment variable on the
command line. This is currently depending on a pending rstest
PR: la10736/rstest#277

The current set of curated tests are all working.
  • Loading branch information
hansl committed Sep 26, 2024
1 parent 4ed2bad commit c445a06
Show file tree
Hide file tree
Showing 11 changed files with 1,260 additions and 194 deletions.
626 changes: 445 additions & 181 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ criterion = "0.5.1"
float-cmp = "0.9.0"
futures-lite = "2.3.0"
test-case = "3.3.1"
rstest = { path = "../rstest/rstest" }
winapi = { version = "0.3.9", default-features = false }

# ICU4X
Expand Down
11 changes: 3 additions & 8 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ use boa_engine::{
builtins::promise::PromiseState,
context::ContextBuilder,
job::{FutureJob, JobQueue, NativeJob},
js_string,
module::{Module, SimpleModuleLoader},
optimizer::OptimizerOptions,
property::Attribute,
script::Script,
vm::flowgraph::{Direction, Graph},
Context, JsError, JsNativeError, JsResult, Source,
};
use boa_runtime::Console;
use clap::{Parser, ValueEnum, ValueHint};
use colored::Colorize;
use debug::init_boa_debug_object;
Expand Down Expand Up @@ -442,12 +439,10 @@ fn main() -> Result<(), io::Error> {
Ok(())
}

/// Adds the CLI runtime to the context.
/// Adds the CLI runtime to the context with default options.
fn add_runtime(context: &mut Context) {
let console = Console::init(context);
context
.register_global_property(js_string!(Console::NAME), console, Attribute::all())
.expect("the console object shouldn't exist");
boa_runtime::register(context, boa_runtime::RegisterOptions::new())
.expect("should not fail while registering the runtime");
}

#[derive(Default)]
Expand Down
8 changes: 8 additions & 0 deletions core/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ boa_engine.workspace = true
boa_gc.workspace = true
boa_interop.workspace = true
rustc-hash = { workspace = true, features = ["std"] }
url = { version = "2.5.2", optional = true }

[dev-dependencies]
boa_interop.workspace = true
indoc.workspace = true
rstest.workspace = true
textwrap.workspace = true

[lints]
workspace = true

[package.metadata.docs.rs]
all-features = true

[features]
default = ["all"]
all = ["url"]
wpt-tests = []
30 changes: 28 additions & 2 deletions core/runtime/src/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ pub trait Logger: Trace + Sized {
/// Implements the [`Logger`] trait and output errors to stderr and all
/// the others to stdout. Will add indentation based on the number of
/// groups.
#[derive(Trace, Finalize)]
struct DefaultLogger;
#[derive(Debug, Trace, Finalize)]
pub struct DefaultLogger;

impl Logger for DefaultLogger {
#[inline]
Expand All @@ -85,6 +85,32 @@ impl Logger for DefaultLogger {
}
}

/// A logger that drops all logging. Useful for testing.
#[derive(Debug, Trace, Finalize)]
pub struct NullLogger;

impl Logger for NullLogger {
#[inline]
fn log(&self, _: String, _: &ConsoleState, _: &mut Context) -> JsResult<()> {
Ok(())
}

#[inline]
fn info(&self, _: String, _: &ConsoleState, _: &mut Context) -> JsResult<()> {
Ok(())
}

#[inline]
fn warn(&self, _: String, _: &ConsoleState, _: &mut Context) -> JsResult<()> {
Ok(())
}

#[inline]
fn error(&self, _: String, _: &ConsoleState, _: &mut Context) -> JsResult<()> {
Ok(())
}
}

/// This represents the `console` formatter.
fn formatter(data: &[JsValue], context: &mut Context) -> JsResult<String> {
fn to_string(value: &JsValue, context: &mut Context) -> JsResult<String> {
Expand Down
4 changes: 2 additions & 2 deletions core/runtime/src/console/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{formatter, Console, ConsoleState};
use crate::test::{run_test_actions, run_test_actions_with, TestAction};
use crate::Logger;
use crate::{Logger, NullLogger};
use boa_engine::{js_string, property::Attribute, Context, JsError, JsResult, JsValue};
use boa_gc::{Gc, GcRefCell};
use indoc::indoc;
Expand Down Expand Up @@ -97,7 +97,7 @@ fn formatter_float_format_works() {
#[test]
fn console_log_cyclic() {
let mut context = Context::default();
let console = Console::init(&mut context);
let console = Console::init_with_logger(&mut context, NullLogger);
context
.register_global_property(js_string!(Console::NAME), console, Attribute::all())
.unwrap();
Expand Down
51 changes: 50 additions & 1 deletion core/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
)]
#![cfg_attr(test, allow(clippy::needless_raw_string_hashes))] // Makes strings a bit more copy-pastable
#![cfg_attr(not(test), forbid(clippy::unwrap_used))]
// Currently throws a false positive regarding dependencies that are only used in tests.
#![allow(unused_crate_dependencies)]
#![allow(
clippy::module_name_repetitions,
clippy::redundant_pub_crate,
Expand All @@ -54,13 +56,60 @@
mod console;

#[doc(inline)]
pub use console::{Console, ConsoleState, Logger};
pub use console::{Console, ConsoleState, DefaultLogger, Logger, NullLogger};

mod text;

#[doc(inline)]
pub use text::{TextDecoder, TextEncoder};

pub mod url;

/// Options used when registering all built-in objects and functions of the WebAPI runtime.
#[derive(Debug)]
pub struct RegisterOptions<L: Logger> {
console_logger: L,
}

impl Default for RegisterOptions<DefaultLogger> {
fn default() -> Self {
Self {
console_logger: DefaultLogger,
}
}
}

impl RegisterOptions<DefaultLogger> {
/// Create a new `RegisterOptions` with the default options.
pub fn new() -> Self {
Self::default()
}
}

impl<L: Logger> RegisterOptions<L> {
/// Set the logger for the console object.
pub fn with_console_logger<L2: Logger>(self, logger: L2) -> RegisterOptions<L2> {
RegisterOptions::<L2> {
console_logger: logger,
}
}
}

/// Register all the built-in objects and functions of the WebAPI runtime.
pub fn register(
ctx: &mut boa_engine::Context,
options: RegisterOptions<impl Logger + 'static>,
) -> boa_engine::JsResult<()> {
Console::register_with_logger(ctx, options.console_logger)?;
TextDecoder::register(ctx)?;
TextEncoder::register(ctx)?;

#[cfg(feature = "url")]
url::Url::register(ctx)?;

Ok(())
}

#[cfg(test)]
pub(crate) mod test {
use boa_engine::{builtins, Context, JsResult, JsValue, Source};
Expand Down
Loading

0 comments on commit c445a06

Please # to comment.