Skip to content

Commit

Permalink
Proposal of new Object design (#1354)
Browse files Browse the repository at this point in the history
* Implement `InternalObjectMethods` struct for `Object`

* Implement remaining string internal methods

* Split object internal methods and operations in modules

* Store internal object methods as static struct

* Document `ObjectData` and order indices on `[[OwnPropertyKeys]]`

* Document and rearrange internal object methods
  • Loading branch information
jedel1043 authored Aug 27, 2021
1 parent 93f15ee commit 8afd50f
Show file tree
Hide file tree
Showing 45 changed files with 2,609 additions and 1,381 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ tests/js/test.js
*.string_index
*.events
chrome_profiler.json
*.mm_profdata

# Logs
*.log
2 changes: 1 addition & 1 deletion boa/src/builtins/array/array_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl ArrayIterator {
kind: PropertyNameKind,
) -> JsValue {
let array_iterator = JsValue::new_object(context);
array_iterator.set_data(ObjectData::ArrayIterator(Self::new(array, kind)));
array_iterator.set_data(ObjectData::array_iterator(Self::new(array, kind)));
array_iterator
.as_object()
.expect("array iterator object")
Expand Down
10 changes: 6 additions & 4 deletions boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,19 +227,21 @@ impl Array {
array.set_prototype_instance(prototype.into());
// This value is used by console.log and other routines to match Object type
// to its Javascript Identifier (global constructor method name)
array.borrow_mut().data = ObjectData::Array;
array.borrow_mut().data = ObjectData::array();

// 6. Perform ! OrdinaryDefineOwnProperty(A, "length", PropertyDescriptor { [[Value]]: 𝔽(length), [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).

array.ordinary_define_own_property(
crate::object::internal_methods::ordinary_define_own_property(
&array,
"length".into(),
PropertyDescriptor::builder()
.value(length)
.writable(true)
.enumerable(false)
.configurable(false)
.build(),
);
context,
)?;

Ok(array)
}
Expand Down Expand Up @@ -274,7 +276,7 @@ impl Array {
/// Creates a new `Array` instance.
pub(crate) fn new_array(context: &Context) -> JsValue {
let array = JsValue::new_object(context);
array.set_data(ObjectData::Array);
array.set_data(ObjectData::array());
array
.as_object()
.expect("'array' should be an object")
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/array/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1542,9 +1542,9 @@ fn get_relative_end() {

#[test]
fn array_length_is_not_enumerable() {
let context = Context::new();
let mut context = Context::new();

let array = Array::new_array(&context);
let array = Array::new_array(&mut context);
let desc = array.get_property("length").unwrap();
assert!(!desc.expect_enumerable());
}
Expand Down
10 changes: 3 additions & 7 deletions boa/src/builtins/bigint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

use crate::{
builtins::BuiltIn,
object::{ConstructorBuilder, ObjectData},
property::Attribute,
symbol::WellKnownSymbols,
value::IntegerOrInfinity,
BoaProfiler, Context, JsBigInt, JsResult, JsValue,
builtins::BuiltIn, object::ConstructorBuilder, property::Attribute, symbol::WellKnownSymbols,
value::IntegerOrInfinity, BoaProfiler, Context, JsBigInt, JsResult, JsValue,
};
#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -105,7 +101,7 @@ impl BigInt {
// a. Assert: Type(value.[[BigIntData]]) is BigInt.
// b. Return value.[[BigIntData]].
JsValue::Object(ref object) => {
if let ObjectData::BigInt(ref bigint) = object.borrow().data {
if let Some(bigint) = object.borrow().as_bigint() {
return Ok(bigint.clone());
}
}
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Boolean {
.as_object()
.expect("this should be an object")
.set_prototype_instance(prototype.into());
boolean.set_data(ObjectData::Boolean(data));
boolean.set_data(ObjectData::boolean(data));

Ok(boolean)
}
Expand Down
44 changes: 22 additions & 22 deletions boa/src/builtins/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ impl Date {
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date
pub(crate) fn make_date_now(this: &JsValue) -> JsValue {
let date = Date::default();
this.set_data(ObjectData::Date(date));
this.set_data(ObjectData::date(date));
this.clone()
}

Expand Down Expand Up @@ -428,7 +428,7 @@ impl Date {

let tv = tv.filter(|time| Self::time_clip(time.timestamp_millis() as f64).is_some());
let date = Date(tv);
this.set_data(ObjectData::Date(date));
this.set_data(ObjectData::date(date));
Ok(this.clone())
}

Expand Down Expand Up @@ -468,7 +468,7 @@ impl Date {
// If any of the args are infinity or NaN, return an invalid date.
if !check_normal_opt!(year, month, day, hour, min, sec, milli) {
let date = Date(None);
this.set_data(ObjectData::Date(date));
this.set_data(ObjectData::date(date));
return Ok(this.clone());
}

Expand All @@ -494,7 +494,7 @@ impl Date {
Some(milli),
);

this.set_data(ObjectData::Date(date));
this.set_data(ObjectData::date(date));

Ok(this.clone())
}
Expand Down Expand Up @@ -872,7 +872,7 @@ impl Date {
let u = t.get_time();

// 5. Set the [[DateValue]] internal slot of this Date object to u.
this.set_data(ObjectData::Date(t));
this.set_data(ObjectData::date(t));

// 6. Return u.
Ok(u.into())
Expand Down Expand Up @@ -933,7 +933,7 @@ impl Date {
let u = t.get_time();

// 8. Set the [[DateValue]] internal slot of this Date object to u.
this.set_data(ObjectData::Date(t));
this.set_data(ObjectData::date(t));

// 9. Return u.
Ok(u.into())
Expand Down Expand Up @@ -990,7 +990,7 @@ impl Date {
let u = t.get_time();

// 8. Set the [[DateValue]] internal slot of this Date object to u.
this.set_data(ObjectData::Date(t));
this.set_data(ObjectData::date(t));

// 9. Return u.
Ok(u.into())
Expand Down Expand Up @@ -1028,7 +1028,7 @@ impl Date {
let u = t.get_time();

// 5. Set the [[DateValue]] internal slot of this Date object to u.
this.set_data(ObjectData::Date(t));
this.set_data(ObjectData::date(t));

// 6. Return u.
Ok(u.into())
Expand Down Expand Up @@ -1080,7 +1080,7 @@ impl Date {
let u = t.get_time();

// 7. Set the [[DateValue]] internal slot of this Date object to u.
this.set_data(ObjectData::Date(t));
this.set_data(ObjectData::date(t));

// 8. Return u.
Ok(u.into())
Expand Down Expand Up @@ -1121,7 +1121,7 @@ impl Date {
let u = t.get_time();

// 6. Set the [[DateValue]] internal slot of this Date object to u.
this.set_data(ObjectData::Date(t));
this.set_data(ObjectData::date(t));

// 7. Return u.
Ok(u.into())
Expand Down Expand Up @@ -1166,7 +1166,7 @@ impl Date {
let u = t.get_time();

// 6. Set the [[DateValue]] internal slot of this Date object to u.
this.set_data(ObjectData::Date(t));
this.set_data(ObjectData::date(t));

// 7. Return u.
Ok(u.into())
Expand Down Expand Up @@ -1204,7 +1204,7 @@ impl Date {
// 4. If y is NaN, then
if y.is_nan() {
// a. Set the [[DateValue]] internal slot of this Date object to NaN.
this.set_data(ObjectData::Date(Date(None)));
this.set_data(ObjectData::date(Date(None)));

// b. Return NaN.
return Ok(JsValue::nan());
Expand All @@ -1222,7 +1222,7 @@ impl Date {
t.set_components(false, Some(y), None, None, None, None, None, None);

// 10. Set the [[DateValue]] internal slot of this Date object to TimeClip(date).
this.set_data(ObjectData::Date(t));
this.set_data(ObjectData::date(t));

// 11. Return the value of the [[DateValue]] internal slot of this Date object.
Ok(t.get_time().into())
Expand Down Expand Up @@ -1260,7 +1260,7 @@ impl Date {
let v = t.get_time();

// 4. Set the [[DateValue]] internal slot of this Date object to v.
this.set_data(ObjectData::Date(t));
this.set_data(ObjectData::date(t));

// 5. Return v.
Ok(v.into())
Expand Down Expand Up @@ -1298,7 +1298,7 @@ impl Date {
let v = t.get_time();

// 5. Set the [[DateValue]] internal slot of this Date object to v.
this.set_data(ObjectData::Date(t));
this.set_data(ObjectData::date(t));

// 6. Return v.
Ok(v.into())
Expand Down Expand Up @@ -1359,7 +1359,7 @@ impl Date {
let v = t.get_time();

// 8. Set the [[DateValue]] internal slot of this Date object to v.
this.set_data(ObjectData::Date(t));
this.set_data(ObjectData::date(t));

// 9. Return v.
Ok(v.into())
Expand Down Expand Up @@ -1420,7 +1420,7 @@ impl Date {
let v = t.get_time();

// 8. Set the [[DateValue]] internal slot of this Date object to v.
this.set_data(ObjectData::Date(t));
this.set_data(ObjectData::date(t));

// 9. Return v.
Ok(v.into())
Expand Down Expand Up @@ -1458,7 +1458,7 @@ impl Date {
let v = t.get_time();

// 5. Set the [[DateValue]] internal slot of this Date object to v.
this.set_data(ObjectData::Date(t));
this.set_data(ObjectData::date(t));

// 6. Return v.
Ok(v.into())
Expand Down Expand Up @@ -1514,7 +1514,7 @@ impl Date {
let v = t.get_time();

// 9. Set the [[DateValue]] internal slot of this Date object to v.
this.set_data(ObjectData::Date(t));
this.set_data(ObjectData::date(t));

// 10. Return v.
Ok(v.into())
Expand Down Expand Up @@ -1561,7 +1561,7 @@ impl Date {
let v = t.get_time();

// 7. Set the [[DateValue]] internal slot of this Date object to v.
this.set_data(ObjectData::Date(t));
this.set_data(ObjectData::date(t));

// 8. Return v.
Ok(v.into())
Expand Down Expand Up @@ -1608,7 +1608,7 @@ impl Date {
let v = t.get_time();

// 7. Set the [[DateValue]] internal slot of this Date object to v.
this.set_data(ObjectData::Date(t));
this.set_data(ObjectData::date(t));

// 8. Return v.
Ok(v.into())
Expand Down Expand Up @@ -1928,7 +1928,7 @@ impl Date {
#[inline]
pub fn this_time_value(value: &JsValue, context: &mut Context) -> JsResult<Date> {
if let JsValue::Object(ref object) = value {
if let ObjectData::Date(ref date) = object.borrow().data {
if let Some(date) = object.borrow().as_date() {
return Ok(*date);
}
}
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/date/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::zero_prefixed_literal)]

use crate::{forward, forward_val, object::ObjectData, Context, JsValue};
use crate::{forward, forward_val, Context, JsValue};
use chrono::prelude::*;

// NOTE: Javascript Uses 0-based months, where chrono uses 1-based months. Many of the assertions look wrong because of
Expand All @@ -14,7 +14,7 @@ fn forward_dt_utc(context: &mut Context, src: &str) -> Option<NaiveDateTime> {
};

if let JsValue::Object(ref date_time) = date_time {
if let ObjectData::Date(ref date_time) = date_time.borrow().data {
if let Some(date_time) = date_time.borrow().as_date() {
date_time.0
} else {
panic!("expected date")
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/error/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl EvalError {

// This value is used by console.log and other routines to match Object type
// to its Javascript Identifier (global constructor method name)
this.set_data(ObjectData::Error);
this.set_data(ObjectData::error());
Ok(this)
}
}
2 changes: 1 addition & 1 deletion boa/src/builtins/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Error {

// This value is used by console.log and other routines to match Object type
// to its Javascript Identifier (global constructor method name)
this.set_data(ObjectData::Error);
this.set_data(ObjectData::error());
Ok(this)
}

Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/error/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl RangeError {

// This value is used by console.log and other routines to match Object type
// to its Javascript Identifier (global constructor method name)
this.set_data(ObjectData::Error);
this.set_data(ObjectData::error());
Ok(this)
}
}
2 changes: 1 addition & 1 deletion boa/src/builtins/error/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl ReferenceError {

// This value is used by console.log and other routines to match Object type
// to its Javascript Identifier (global constructor method name)
this.set_data(ObjectData::Error);
this.set_data(ObjectData::error());
Ok(this)
}
}
2 changes: 1 addition & 1 deletion boa/src/builtins/error/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl SyntaxError {

// This value is used by console.log and other routines to match Object type
// to its Javascript Identifier (global constructor method name)
this.set_data(ObjectData::Error);
this.set_data(ObjectData::error());
Ok(this)
}
}
2 changes: 1 addition & 1 deletion boa/src/builtins/error/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl TypeError {

// This value is used by console.log and other routines to match Object type
// to its Javascript Identifier (global constructor method name)
this.set_data(ObjectData::Error);
this.set_data(ObjectData::error());
Ok(this)
}
}
2 changes: 1 addition & 1 deletion boa/src/builtins/error/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl UriError {

// This value is used by console.log and other routines to match Object type
// to its Javascript Identifier (global constructor method name)
this.set_data(ObjectData::Error);
this.set_data(ObjectData::error());
Ok(this)
}
}
Loading

0 comments on commit 8afd50f

Please sign in to comment.