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

[Merged by Bors] - Interner support in the parser #1765

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion boa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ boa_unicode = { path = "../boa_unicode", version = "0.13.0" }
boa_interner = { path = "../boa_interner", version = "0.13.0" }
gc = { version = "0.4.1", features = ["derive"] }
serde = { version = "1.0.134", features = ["derive", "rc"] }
serde_json = "1.0.75"
serde_json = "1.0.76"
rand = "0.8.4"
num-traits = "0.2.14"
regress = "0.4.1"
Expand Down
22 changes: 10 additions & 12 deletions boa/benches/full.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Benchmarks of the whole execution engine in Boa.

use boa::{realm::Realm, syntax::Parser, Context};
use boa_interner::Interner;
use boa::{realm::Realm, Context};
use criterion::{black_box, criterion_group, criterion_main, Criterion};

#[cfg(all(target_arch = "x86_64", target_os = "linux", target_env = "gnu"))]
Expand All @@ -21,9 +20,9 @@ macro_rules! full_benchmarks {
$(
{
static CODE: &str = include_str!(concat!("bench_scripts/", stringify!($name), ".js"));
let mut interner = Interner::new();
let mut context = Context::default();
c.bench_function(concat!($id, " (Parser)"), move |b| {
b.iter(|| Parser::new(black_box(CODE.as_bytes()), false).parse_all(&mut interner))
b.iter(|| context.parse(black_box(CODE)))
});
}
)*
Expand All @@ -32,11 +31,11 @@ macro_rules! full_benchmarks {
$(
{
static CODE: &str = include_str!(concat!("bench_scripts/", stringify!($name), ".js"));
let mut interner = Interner::new();
let statement_list = Parser::new(CODE.as_bytes(), false).parse_all( &mut interner).expect("parsing failed");
let mut context = Context::default();
let statement_list = context.parse(CODE).expect("parsing failed");
c.bench_function(concat!($id, " (Compiler)"), move |b| {
b.iter(|| {
Context::compile(black_box(&statement_list));
context.compile(black_box(&statement_list))
})
});
}
Expand All @@ -46,13 +45,12 @@ macro_rules! full_benchmarks {
$(
{
static CODE: &str = include_str!(concat!("bench_scripts/", stringify!($name), ".js"));
let mut interner = Interner::new();
let statement_list = Parser::new(CODE.as_bytes(), false).parse_all(&mut interner).expect("parsing failed");
let mut context = Context::new(interner);
let code_block = Context::compile(&statement_list);
let mut context = Context::default();
let statement_list = context.parse(CODE).expect("parsing failed");
let code_block = context.compile(&statement_list);
c.bench_function(concat!($id, " (Execution)"), move |b| {
b.iter(|| {
context.execute(black_box(code_block.clone())).unwrap();
context.execute(black_box(code_block.clone())).unwrap()
})
});
}
Expand Down
1 change: 0 additions & 1 deletion boa/src/builtins/array/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ fn of() {
assert_eq!(context.eval("a.length").unwrap(), JsValue::new(3));
}

#[ignore]
#[test]
fn concat() {
let mut context = Context::default();
Expand Down
7 changes: 6 additions & 1 deletion boa/src/builtins/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,12 @@ impl Console {
let mut prev_frame = context.vm.frame.as_ref();

while let Some(frame) = prev_frame {
stack_trace.push(frame.code.name.to_string());
stack_trace.push(
context
.interner()
.resolve_expect(frame.code.name)
.to_owned(),
);
prev_frame = frame.prev.as_ref();
}

Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/dataview/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
builtins::{array_buffer::SharedMemoryOrder, typed_array::TypedArrayName, BuiltIn, JsArgs},
context::StandardObjects,
gc::{Finalize, Trace},
object::{
internal_methods::get_prototype_from_constructor, ConstructorBuilder, FunctionBuilder,
JsObject, ObjectData,
Expand All @@ -10,7 +11,6 @@ use crate::{
value::JsValue,
Context, JsResult,
};
use gc::{Finalize, Trace};

#[derive(Debug, Clone, Trace, Finalize)]
pub struct DataView {
Expand Down
11 changes: 5 additions & 6 deletions boa/src/builtins/function/arguments.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::{
builtins::Array,
environment::lexical_environment::Environment,
gc::{Finalize, Trace},
object::{FunctionBuilder, JsObject, ObjectData},
property::PropertyDescriptor,
symbol::{self, WellKnownSymbols},
syntax::ast::node::FormalParameter,
Context, JsValue,
};

use gc::{Finalize, Trace};
use rustc_hash::FxHashSet;

#[derive(Debug, Clone, Trace, Finalize)]
Expand Down Expand Up @@ -171,9 +170,9 @@ impl Arguments {
// a. Let name be parameterNames[index].

for (index, parameter_name_vec) in formals.iter().map(|fp| fp.names()).enumerate().rev() {
for parameter_name in parameter_name_vec.iter().cloned() {
for parameter_name in parameter_name_vec.iter().copied() {
// b. If name is not an element of mappedNames, then
if !mapped_names.contains(parameter_name) {
if !mapped_names.contains(&parameter_name) {
// i. Add name as an element of the list mappedNames.
mapped_names.insert(parameter_name);
// ii. If index < len, then
Expand All @@ -189,7 +188,7 @@ impl Arguments {
// 1. Let getterClosure be a new Abstract Closure with no parameters that captures
// name and env and performs the following steps when called:
|_, _, captures, context| {
captures.0.get_binding_value(&captures.1, false, context)
captures.0.get_binding_value(captures.1, false, context)
},
(env.clone(), parameter_name.to_owned()),
)
Expand All @@ -212,7 +211,7 @@ impl Arguments {
// a. Return env.SetMutableBinding(name, value, false).
captures
.0
.set_mutable_binding(&captures.1, value, false, context)
.set_mutable_binding(captures.1, value, false, context)
.map(|_| JsValue::Undefined)
// Ok(JsValue::Undefined)
},
Expand Down
52 changes: 21 additions & 31 deletions boa/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,29 @@
//! [spec]: https://tc39.es/ecma262/#sec-function-objects
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function

use std::{
any::Any,
borrow::Cow,
fmt,
ops::{Deref, DerefMut},
};

use dyn_clone::DynClone;
use gc::{Gc, GcCell};

use super::JsArgs;
use crate::{
builtins::BuiltIn,
context::StandardObjects,
environment::lexical_environment::Environment,
gc::{Finalize, Trace},
object::JsObject,
object::{internal_methods::get_prototype_from_constructor, NativeObject, ObjectData},
property::Attribute,
property::PropertyDescriptor,
BoaProfiler, Context, JsResult, JsValue,
};
use crate::{object::Object, symbol::WellKnownSymbols};
use crate::{
object::{ConstructorBuilder, FunctionBuilder},
property::PropertyKey,
JsString,
};
use crate::{
object::{Ref, RefMut},
gc::{self, Finalize, Gc, Trace},
object::{
internal_methods::get_prototype_from_constructor, JsObject, NativeObject, Object,
ObjectData,
},
object::{ConstructorBuilder, FunctionBuilder, Ref, RefMut},
property::{Attribute, PropertyDescriptor, PropertyKey},
symbol::WellKnownSymbols,
value::IntegerOrInfinity,
BoaProfiler, Context, JsResult, JsString, JsValue,
};
use dyn_clone::DynClone;
use std::{
any::Any,
borrow::Cow,
fmt,
ops::{Deref, DerefMut},
};

use super::JsArgs;

pub(crate) mod arguments;
#[cfg(test)]
Expand Down Expand Up @@ -136,23 +126,23 @@ impl ConstructorKind {
/// with `Any::downcast_ref` and `Any::downcast_mut` to recover the original
/// type.
#[derive(Clone, Debug, Trace, Finalize)]
pub struct Captures(Gc<GcCell<Box<dyn NativeObject>>>);
pub struct Captures(Gc<gc::Cell<Box<dyn NativeObject>>>);

impl Captures {
/// Creates a new capture context.
pub(crate) fn new<T>(captures: T) -> Self
where
T: NativeObject,
{
Self(Gc::new(GcCell::new(Box::new(captures))))
Self(Gc::new(gc::Cell::new(Box::new(captures))))
}

/// Casts `Captures` to `Any`
///
/// # Panics
///
/// Panics if it's already borrowed as `&mut Any`
pub fn as_any(&self) -> gc::GcCellRef<'_, dyn Any> {
pub fn as_any(&self) -> gc::Ref<'_, dyn Any> {
Ref::map(self.0.borrow(), |data| data.deref().as_any())
}

Expand All @@ -161,7 +151,7 @@ impl Captures {
/// # Panics
///
/// Panics if it's already borrowed as `&mut Any`
pub fn as_mut_any(&self) -> gc::GcCellRefMut<'_, Box<dyn NativeObject>, dyn Any> {
pub fn as_mut_any(&self) -> gc::RefMut<'_, Box<dyn NativeObject>, dyn Any> {
RefMut::map(self.0.borrow_mut(), |data| data.deref_mut().as_mut_any())
}
}
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/map/map_iterator.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use super::ordered_map::MapLock;
use crate::{
builtins::{function::make_builtin_fn, iterable::create_iter_result_object, Array, JsValue},
gc::{Finalize, Trace},
object::{JsObject, ObjectData},
property::{PropertyDescriptor, PropertyNameKind},
symbol::WellKnownSymbols,
BoaProfiler, Context, JsResult,
};
use gc::{Finalize, Trace};

use super::ordered_map::MapLock;
/// The Map Iterator object represents an iteration over a map. It implements the iterator protocol.
///
/// More information:
Expand Down
6 changes: 2 additions & 4 deletions boa/src/builtins/set/set_iterator.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::{
builtins::Array,
builtins::JsValue,
builtins::{function::make_builtin_fn, iterable::create_iter_result_object},
builtins::{function::make_builtin_fn, iterable::create_iter_result_object, Array, JsValue},
gc::{Finalize, Trace},
object::{JsObject, ObjectData},
property::{PropertyDescriptor, PropertyNameKind},
symbol::WellKnownSymbols,
BoaProfiler, Context, JsResult,
};
use gc::{Finalize, Trace};

/// The Set Iterator object represents an iteration over a set. It implements the iterator protocol.
///
Expand Down