Skip to content

Commit

Permalink
Merge 05791d5 into 6ea4848
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat authored Mar 25, 2023
2 parents 6ea4848 + 05791d5 commit 7e63a54
Show file tree
Hide file tree
Showing 26 changed files with 1,185 additions and 309 deletions.
8 changes: 7 additions & 1 deletion boa_engine/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,13 @@ impl Array {
// 5. Set A.[[DefineOwnProperty]] as specified in 10.4.2.1.
let prototype =
prototype.unwrap_or_else(|| context.intrinsics().constructors().array().prototype());
let array = JsObject::from_proto_and_data(prototype, ObjectData::array());

// TODO: possible optimization: directly initialize with pre-initialized array instance shape.
let array = JsObject::from_proto_and_data_with_shared_shape(
context.root_shape.clone(),
prototype,
ObjectData::array(),
);

// 6. Perform ! OrdinaryDefineOwnProperty(A, "length", PropertyDescriptor { [[Value]]: 𝔽(length), [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
crate::object::internal_methods::ordinary_define_own_property(
Expand Down
24 changes: 19 additions & 5 deletions boa_engine/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
class::{Class, ClassBuilder},
job::{IdleJobQueue, JobQueue, NativeJob},
native_function::NativeFunction,
object::{FunctionObjectBuilder, GlobalPropertyMap, JsObject},
object::{shape::Shape, FunctionObjectBuilder, JsObject, PropertyMap},
property::{Attribute, PropertyDescriptor, PropertyKey},
realm::Realm,
vm::{CallFrame, CodeBlock, Vm},
Expand Down Expand Up @@ -101,6 +101,9 @@ pub struct Context<'host> {
host_hooks: &'host dyn HostHooks,

job_queue: &'host dyn JobQueue,

pub(crate) root_shape: Shape,
pub(crate) empty_object_shape: Shape,
}

impl std::fmt::Debug for Context<'_> {
Expand Down Expand Up @@ -323,7 +326,7 @@ impl Context<'_> {
.build();

self.global_bindings_mut().insert(
name.into(),
&PropertyKey::String(name.into()),
PropertyDescriptor::builder()
.value(function)
.writable(true)
Expand Down Expand Up @@ -355,7 +358,7 @@ impl Context<'_> {
.build();

self.global_bindings_mut().insert(
name.into(),
&PropertyKey::String(name.into()),
PropertyDescriptor::builder()
.value(function)
.writable(true)
Expand Down Expand Up @@ -393,7 +396,8 @@ impl Context<'_> {
.configurable(T::ATTRIBUTES.configurable())
.build();

self.global_bindings_mut().insert(T::NAME.into(), property);
self.global_bindings_mut()
.insert(&PropertyKey::String(T::NAME.into()), property);
Ok(())
}

Expand Down Expand Up @@ -460,7 +464,7 @@ impl Context<'_> {

impl Context<'_> {
/// Return a mutable reference to the global object string bindings.
pub(crate) fn global_bindings_mut(&mut self) -> &mut GlobalPropertyMap {
pub(crate) fn global_bindings_mut(&mut self) -> &mut PropertyMap {
self.realm.global_bindings_mut()
}

Expand Down Expand Up @@ -628,6 +632,9 @@ impl<'icu, 'hooks, 'queue> ContextBuilder<'icu, 'hooks, 'queue> {
{
let host_hooks = self.host_hooks.unwrap_or(&DefaultHooks);

let root_shape = Shape::root();
// let empty_object_shape = root_shape.change_prototype_transition(prototype);

let mut context = Context {
realm: Realm::create(host_hooks),
interner: self.interner.unwrap_or_default(),
Expand All @@ -643,9 +650,16 @@ impl<'icu, 'hooks, 'queue> ContextBuilder<'icu, 'hooks, 'queue> {
kept_alive: Vec::new(),
host_hooks,
job_queue: self.job_queue.unwrap_or(&IdleJobQueue),
// TODO: probably should be moved to realm, maybe into intrinsics.
root_shape: root_shape.clone(),
empty_object_shape: root_shape,
};

builtins::set_default_global_bindings(&mut context)?;
let object_prototype = context.intrinsics().constructors().object().prototype();
context.empty_object_shape = context
.root_shape
.change_prototype_transition(Some(object_prototype));

Ok(context)
}
Expand Down
14 changes: 7 additions & 7 deletions boa_engine/src/environments/compile.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{
environments::runtime::BindingLocator, property::PropertyDescriptor, Context, JsString, JsValue,
environments::runtime::BindingLocator,
property::{PropertyDescriptor, PropertyKey},
Context, JsString, JsValue,
};
use boa_ast::expression::Identifier;
use boa_gc::{Finalize, Gc, GcRefCell, Trace};
Expand Down Expand Up @@ -289,14 +291,12 @@ impl Context<'_> {
.interner()
.resolve_expect(name.sym())
.into_common::<JsString>(false);
let desc = self
.realm
.global_property_map
.string_property_map()
.get(&name_str);

let key = PropertyKey::String(name_str);
let desc = self.realm.global_property_map.get(&key);
if desc.is_none() {
self.global_bindings_mut().insert(
name_str,
&key,
PropertyDescriptor::builder()
.value(JsValue::Undefined)
.writable(true)
Expand Down
25 changes: 5 additions & 20 deletions boa_engine/src/object/internal_methods/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,30 +254,15 @@ pub(crate) fn global_own_property_keys(
};

// 2. For each own property key P of O such that P is an array index, in ascending numeric index order, do
// a. Add P as the last element of keys.
// a. Add P as the last element of keys.
keys.extend(ordered_indexes.into_iter().map(Into::into));

// 3. For each own property key P of O such that Type(P) is String and P is not an array index, in ascending chronological order of property creation, do
// a. Add P as the last element of keys.
keys.extend(
context
.realm
.global_property_map
.string_property_keys()
.cloned()
.map(Into::into),
);

// a. Add P as the last element of keys.
//
// 4. For each own property key P of O such that Type(P) is Symbol, in ascending chronological order of property creation, do
// a. Add P as the last element of keys.
keys.extend(
context
.realm
.global_property_map
.symbol_property_keys()
.cloned()
.map(Into::into),
);
// a. Add P as the last element of keys.
keys.extend(context.realm.global_property_map.shape.keys());

// 5. Return keys.
Ok(keys)
Expand Down
23 changes: 6 additions & 17 deletions boa_engine/src/object/internal_methods/integer_indexed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,30 +222,19 @@ pub(crate) fn integer_indexed_exotic_own_property_keys(
vec![]
} else {
// 2. If IsDetachedBuffer(O.[[ViewedArrayBuffer]]) is false, then
// a. For each integer i starting with 0 such that i < O.[[ArrayLength]], in ascending order, do
// i. Add ! ToString(𝔽(i)) as the last element of keys.
// a. For each integer i starting with 0 such that i < O.[[ArrayLength]], in ascending order, do
// i. Add ! ToString(𝔽(i)) as the last element of keys.
(0..inner.array_length())
.map(|index| PropertyKey::Index(index as u32))
.collect()
};

// 3. For each own property key P of O such that Type(P) is String and P is not an array index, in ascending chronological order of property creation, do
// a. Add P as the last element of keys.
keys.extend(
obj.properties
.string_property_keys()
.cloned()
.map(Into::into),
);

// a. Add P as the last element of keys.
//
// 4. For each own property key P of O such that Type(P) is Symbol, in ascending chronological order of property creation, do
// a. Add P as the last element of keys.
keys.extend(
obj.properties
.symbol_property_keys()
.cloned()
.map(Into::into),
);
// a. Add P as the last element of keys.
keys.extend(obj.properties.shape.keys());

// 5. Return keys.
Ok(keys)
Expand Down
35 changes: 10 additions & 25 deletions boa_engine/src/object/internal_methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,12 @@ pub(crate) fn ordinary_set_prototype_of(
_: &mut Context<'_>,
) -> JsResult<bool> {
// 1. Assert: Either Type(V) is Object or Type(V) is Null.
{
// 2. Let current be O.[[Prototype]].
let current = obj.prototype();
// 2. Let current be O.[[Prototype]].
let current = obj.prototype();

// 3. If SameValue(V, current) is true, return true.
if val == *current {
return Ok(true);
}
// 3. If SameValue(V, current) is true, return true.
if val == current {
return Ok(true);
}

// 4. Let extensible be O.[[Extensible]].
Expand Down Expand Up @@ -384,7 +382,7 @@ pub(crate) fn ordinary_set_prototype_of(
break;
}
// ii. Else, set p to p.[[Prototype]].
p = proto.prototype().clone();
p = proto.prototype();
}

// 9. Set O.[[Prototype]] to V.
Expand Down Expand Up @@ -714,24 +712,11 @@ pub(crate) fn ordinary_own_property_keys(
keys.extend(ordered_indexes.into_iter().map(Into::into));

// 3. For each own property key P of O such that Type(P) is String and P is not an array index, in ascending chronological order of property creation, do
// a. Add P as the last element of keys.
keys.extend(
obj.borrow()
.properties
.string_property_keys()
.cloned()
.map(Into::into),
);

// a. Add P as the last element of keys.
//
// 4. For each own property key P of O such that Type(P) is Symbol, in ascending chronological order of property creation, do
// a. Add P as the last element of keys.
keys.extend(
obj.borrow()
.properties
.symbol_property_keys()
.cloned()
.map(Into::into),
);
// a. Add P as the last element of keys.
keys.extend(obj.borrow().properties.shape.keys());

// 5. Return keys.
Ok(keys)
Expand Down
21 changes: 5 additions & 16 deletions boa_engine/src/object/internal_methods/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ pub(crate) fn string_exotic_own_property_keys(
let mut keys = Vec::with_capacity(len);

// 5. For each integer i starting with 0 such that i < len, in ascending order, do
// a. Add ! ToString(𝔽(i)) as the last element of keys.
// a. Add ! ToString(𝔽(i)) as the last element of keys.
keys.extend((0..len).map(Into::into));

// 6. For each own property key P of O such that P is an array index
// and ! ToIntegerOrInfinity(P) ≥ len, in ascending numeric index order, do
// a. Add P as the last element of keys.
// a. Add P as the last element of keys.
let mut remaining_indices: Vec<_> = obj
.properties
.index_property_keys()
Expand All @@ -117,23 +117,12 @@ pub(crate) fn string_exotic_own_property_keys(

// 7. For each own property key P of O such that Type(P) is String and P is not
// an array index, in ascending chronological order of property creation, do
// a. Add P as the last element of keys.
keys.extend(
obj.properties
.string_property_keys()
.cloned()
.map(Into::into),
);
// a. Add P as the last element of keys.

// 8. For each own property key P of O such that Type(P) is Symbol, in ascending
// chronological order of property creation, do
// a. Add P as the last element of keys.
keys.extend(
obj.properties
.symbol_property_keys()
.cloned()
.map(Into::into),
);
// a. Add P as the last element of keys.
keys.extend(obj.properties.shape.keys());

// 9. Return keys.
Ok(keys)
Expand Down
Loading

0 comments on commit 7e63a54

Please sign in to comment.