Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WPT as optional tests for boa_runtime #4008

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
532 changes: 343 additions & 189 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 = "0.23.0"
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
15 changes: 15 additions & 0 deletions core/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,28 @@ boa_engine.workspace = true
boa_gc.workspace = true
boa_interop.workspace = true
rustc-hash = { workspace = true, features = ["std"] }
# We need a new release to fix the URL bug.
url = { git = "https://github.com/servo/rust-url.git", rev = "7eccac9a0b763145ab1bb67a50576a34cc750567", 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 = []
# This feature is used to disable compiling the WPT tests in the CI
# when using `--all-features`. If this feature is enabled, we disable
# the compilation of the WPT tests (since they will panic).
# Clippy will be disabled for those methods as well, but they're one
# line long.
wpt-tests-do-not-use = []
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
57 changes: 56 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,15 +56,67 @@
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.
#[must_use]
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.
///
/// # Errors
/// This will error is any of the built-in objects or functions cannot be registered.
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 crate::{register, RegisterOptions};
use boa_engine::{builtins, Context, JsResult, JsValue, Source};
use std::borrow::Cow;

Expand Down Expand Up @@ -126,6 +180,7 @@ pub(crate) mod test {
#[track_caller]
pub(crate) fn run_test_actions(actions: impl IntoIterator<Item = TestAction>) {
let context = &mut Context::default();
register(context, RegisterOptions::default()).expect("failed to register WebAPI objects");
run_test_actions_with(actions, context);
}

Expand Down
Loading
Loading