Skip to content

Commit

Permalink
Extract Intrinsics struct from Context and cleanup names
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Mar 3, 2022
1 parent a9a82a7 commit fd8e0c6
Show file tree
Hide file tree
Showing 56 changed files with 1,199 additions and 990 deletions.
7 changes: 7 additions & 0 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_engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fast-float = "0.2.0"
unicode-normalization = "0.1.19"
dyn-clone = "1.0.4"
once_cell = "1.9.0"

tap = "1.0.1"
[dev-dependencies]
criterion = "0.3.5"
float-cmp = "0.9.0"
Expand Down
6 changes: 5 additions & 1 deletion boa_engine/src/builtins/array/array_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ impl ArrayIterator {
context: &Context,
) -> JsValue {
let array_iterator = JsObject::from_proto_and_data(
context.iterator_prototypes().array_iterator(),
context
.intrinsics()
.objects()
.iterator_prototypes()
.array_iterator(),
ObjectData::array_iterator(Self::new(array, kind)),
);
array_iterator.into()
Expand Down
27 changes: 14 additions & 13 deletions boa_engine/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ pub mod array_iterator;
mod tests;

use boa_profiler::Profiler;
use tap::{Conv, Pipe};

use super::JsArgs;
use crate::{
builtins::array::array_iterator::ArrayIterator,
builtins::BuiltIn,
builtins::Number,
context::StandardObjects,
context::intrinsics::StandardConstructors,
object::{
internal_methods::get_prototype_from_constructor, ConstructorBuilder, FunctionBuilder,
JsObject, ObjectData,
Expand All @@ -39,11 +40,7 @@ pub(crate) struct Array;
impl BuiltIn for Array {
const NAME: &'static str = "Array";

const ATTRIBUTE: Attribute = Attribute::WRITABLE
.union(Attribute::NON_ENUMERABLE)
.union(Attribute::CONFIGURABLE);

fn init(context: &mut Context) -> JsValue {
fn init(context: &mut Context) -> Option<JsValue> {
let _timer = Profiler::global().start_event(Self::NAME, "init");

let symbol_iterator = WellKnownSymbols::iterator();
Expand All @@ -55,10 +52,10 @@ impl BuiltIn for Array {

let values_function = Self::values_intrinsic(context);

let array = ConstructorBuilder::with_standard_object(
ConstructorBuilder::with_standard_constructor(
context,
Self::constructor,
context.standard_objects().array_object().clone(),
context.intrinsics().standard_constructors().array().clone(),
)
.name(Self::NAME)
.length(Self::LENGTH)
Expand Down Expand Up @@ -118,9 +115,9 @@ impl BuiltIn for Array {
// Static Methods
.static_method(Self::is_array, "isArray", 1)
.static_method(Self::of, "of", 0)
.build();

array.into()
.build()
.conv::<JsValue>()
.pipe(Some)
}
}

Expand All @@ -135,7 +132,7 @@ impl Array {
// If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget.
// 2. Let proto be ? GetPrototypeFromConstructor(newTarget, "%Array.prototype%").
let prototype =
get_prototype_from_constructor(new_target, StandardObjects::array_object, context)?;
get_prototype_from_constructor(new_target, StandardConstructors::array, context)?;

// 3. Let numberOfArgs be the number of elements in values.
let number_of_args = args.len();
Expand Down Expand Up @@ -226,7 +223,11 @@ impl Array {
// 5. Set A.[[DefineOwnProperty]] as specified in 10.4.2.1.
let prototype = match prototype {
Some(prototype) => prototype,
None => context.standard_objects().array_object().prototype(),
None => context
.intrinsics()
.standard_constructors()
.array()
.prototype(),
};
let array = JsObject::from_proto_and_data(prototype, ObjectData::array());

Expand Down
24 changes: 13 additions & 11 deletions boa_engine/src/builtins/array_buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod tests;

use crate::{
builtins::{typed_array::TypedArrayKind, BuiltIn, JsArgs},
context::StandardObjects,
context::intrinsics::StandardConstructors,
object::{
internal_methods::get_prototype_from_constructor, ConstructorBuilder, FunctionBuilder,
JsObject, ObjectData,
Expand All @@ -16,6 +16,7 @@ use crate::{
use boa_gc::{Finalize, Trace};
use boa_profiler::Profiler;
use num_traits::{Signed, ToPrimitive};
use tap::{Conv, Pipe};

#[derive(Debug, Clone, Trace, Finalize)]
pub struct ArrayBuffer {
Expand All @@ -33,22 +34,22 @@ impl ArrayBuffer {
impl BuiltIn for ArrayBuffer {
const NAME: &'static str = "ArrayBuffer";

const ATTRIBUTE: Attribute = Attribute::WRITABLE
.union(Attribute::NON_ENUMERABLE)
.union(Attribute::CONFIGURABLE);

fn init(context: &mut Context) -> JsValue {
fn init(context: &mut Context) -> Option<JsValue> {
let _timer = Profiler::global().start_event(Self::NAME, "init");

let get_species = FunctionBuilder::native(context, Self::get_species)
.name("get [Symbol.species]")
.constructor(false)
.build();

ConstructorBuilder::with_standard_object(
ConstructorBuilder::with_standard_constructor(
context,
Self::constructor,
context.standard_objects().array_buffer_object().clone(),
context
.intrinsics()
.standard_constructors()
.array_buffer()
.clone(),
)
.name(Self::NAME)
.length(Self::LENGTH)
Expand All @@ -67,7 +68,8 @@ impl BuiltIn for ArrayBuffer {
Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.build()
.into()
.conv::<JsValue>()
.pipe(Some)
}
}

Expand Down Expand Up @@ -230,7 +232,7 @@ impl ArrayBuffer {
let new_len = std::cmp::max(r#final - first, 0) as usize;

// 15. Let ctor be ? SpeciesConstructor(O, %ArrayBuffer%).
let ctor = obj.species_constructor(StandardObjects::array_buffer_object, context)?;
let ctor = obj.species_constructor(StandardConstructors::array_buffer, context)?;

// 16. Let new be ? Construct(ctor, « 𝔽(newLen) »).
let new = ctor.construct(&[new_len.into()], &ctor.clone().into(), context)?;
Expand Down Expand Up @@ -310,7 +312,7 @@ impl ArrayBuffer {
// 1. Let obj be ? OrdinaryCreateFromConstructor(constructor, "%ArrayBuffer.prototype%", « [[ArrayBufferData]], [[ArrayBufferByteLength]], [[ArrayBufferDetachKey]] »).
let prototype = get_prototype_from_constructor(
constructor,
StandardObjects::array_buffer_object,
StandardConstructors::array_buffer,
context,
)?;
let obj = context.construct_object();
Expand Down
21 changes: 11 additions & 10 deletions boa_engine/src/builtins/bigint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::{
};
use boa_profiler::Profiler;
use num_bigint::ToBigInt;
use tap::{Conv, Pipe};

#[cfg(test)]
mod tests;
Expand All @@ -33,19 +34,19 @@ pub struct BigInt;
impl BuiltIn for BigInt {
const NAME: &'static str = "BigInt";

const ATTRIBUTE: Attribute = Attribute::WRITABLE
.union(Attribute::NON_ENUMERABLE)
.union(Attribute::CONFIGURABLE);

fn init(context: &mut Context) -> JsValue {
fn init(context: &mut Context) -> Option<JsValue> {
let _timer = Profiler::global().start_event(Self::NAME, "init");

let to_string_tag = WellKnownSymbols::to_string_tag();

let bigint_object = ConstructorBuilder::with_standard_object(
ConstructorBuilder::with_standard_constructor(
context,
Self::constructor,
context.standard_objects().bigint_object().clone(),
context
.intrinsics()
.standard_constructors()
.bigint_object()
.clone(),
)
.name(Self::NAME)
.length(Self::LENGTH)
Expand All @@ -60,9 +61,9 @@ impl BuiltIn for BigInt {
Self::NAME,
Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.build();

bigint_object.into()
.build()
.conv::<JsValue>()
.pipe(Some)
}
}

Expand Down
26 changes: 13 additions & 13 deletions boa_engine/src/builtins/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ mod tests;

use crate::{
builtins::BuiltIn,
context::StandardObjects,
context::intrinsics::StandardConstructors,
object::{
internal_methods::get_prototype_from_constructor, ConstructorBuilder, JsObject, ObjectData,
},
property::Attribute,
Context, JsResult, JsValue,
};
use boa_profiler::Profiler;
use tap::{Conv, Pipe};

/// Boolean implementation.
#[derive(Debug, Clone, Copy)]
Expand All @@ -31,25 +31,25 @@ impl BuiltIn for Boolean {
/// The name of the object.
const NAME: &'static str = "Boolean";

const ATTRIBUTE: Attribute = Attribute::WRITABLE
.union(Attribute::NON_ENUMERABLE)
.union(Attribute::CONFIGURABLE);

fn init(context: &mut Context) -> JsValue {
fn init(context: &mut Context) -> Option<JsValue> {
let _timer = Profiler::global().start_event(Self::NAME, "init");

let boolean_object = ConstructorBuilder::with_standard_object(
ConstructorBuilder::with_standard_constructor(
context,
Self::constructor,
context.standard_objects().boolean_object().clone(),
context
.intrinsics()
.standard_constructors()
.boolean()
.clone(),
)
.name(Self::NAME)
.length(Self::LENGTH)
.method(Self::to_string, "toString", 0)
.method(Self::value_of, "valueOf", 0)
.build();

boolean_object.into()
.build()
.conv::<JsValue>()
.pipe(Some)
}
}

Expand All @@ -71,7 +71,7 @@ impl Boolean {
return Ok(JsValue::new(data));
}
let prototype =
get_prototype_from_constructor(new_target, StandardObjects::boolean_object, context)?;
get_prototype_from_constructor(new_target, StandardConstructors::boolean, context)?;
let boolean = JsObject::from_proto_and_data(prototype, ObjectData::boolean(data));

Ok(boolean.into())
Expand Down
16 changes: 6 additions & 10 deletions boa_engine/src/builtins/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ mod tests;
use crate::{
builtins::{BuiltIn, JsArgs},
object::ObjectInitializer,
property::Attribute,
value::{display::display_obj, JsValue, Numeric},
Context, JsResult, JsString,
};
use boa_profiler::Profiler;
use rustc_hash::FxHashMap;
use std::time::SystemTime;
use tap::{Conv, Pipe};

/// This represents the different types of log messages.
#[derive(Debug)]
Expand Down Expand Up @@ -133,13 +133,9 @@ pub(crate) struct Console {
impl BuiltIn for Console {
const NAME: &'static str = "console";

const ATTRIBUTE: Attribute = Attribute::WRITABLE
.union(Attribute::NON_ENUMERABLE)
.union(Attribute::CONFIGURABLE);

fn init(context: &mut Context) -> JsValue {
fn init(context: &mut Context) -> Option<JsValue> {
let _timer = Profiler::global().start_event(Self::NAME, "init");
let console = ObjectInitializer::new(context)
ObjectInitializer::new(context)
.function(Self::assert, "assert", 0)
.function(Self::clear, "clear", 0)
.function(Self::debug, "debug", 0)
Expand All @@ -159,9 +155,9 @@ impl BuiltIn for Console {
.function(Self::time_end, "timeEnd", 0)
.function(Self::dir, "dir", 0)
.function(Self::dir, "dirxml", 0)
.build();

console.into()
.build()
.conv::<JsValue>()
.pipe(Some)
}
}

Expand Down
Loading

0 comments on commit fd8e0c6

Please sign in to comment.