Browse Source

ArrayIteratorPrototype static shape

optimization/static-shapes
Haled Odat 1 year ago
parent
commit
a9693942c4
  1. 5
      boa_builtins/build.rs
  2. 32
      boa_engine/src/builtins/array/array_iterator.rs
  3. 20
      boa_engine/src/builtins/iterable/mod.rs
  4. 16
      boa_engine/src/builtins/mod.rs

5
boa_builtins/build.rs

@ -1038,6 +1038,11 @@ fn main() -> io::Result<()> {
.property(WellKnown::ToStringTag, Attribute::CONFIGURABLE) .property(WellKnown::ToStringTag, Attribute::CONFIGURABLE)
.build(file)?; .build(file)?;
BuiltInBuilder::new(&context, "ARRAY_ITERATOR_PROTOTYPE")
.method(utf16!("next"))
.property(WellKnown::ToStringTag, Attribute::CONFIGURABLE)
.build(file)?;
context.build(file)?; context.build(file)?;
Ok(()) Ok(())

32
boa_engine/src/builtins/array/array_iterator.rs

@ -12,9 +12,8 @@ use crate::{
context::intrinsics::Intrinsics, context::intrinsics::Intrinsics,
error::JsNativeError, error::JsNativeError,
object::{JsObject, ObjectData}, object::{JsObject, ObjectData},
property::{Attribute, PropertyNameKind}, property::PropertyNameKind,
realm::Realm, realm::Realm,
symbol::JsSymbol,
Context, JsResult, Context, JsResult,
}; };
use boa_gc::{Finalize, Trace}; use boa_gc::{Finalize, Trace};
@ -39,21 +38,20 @@ impl IntrinsicObject for ArrayIterator {
fn init(realm: &Realm) { fn init(realm: &Realm) {
let _timer = Profiler::global().start_event("ArrayIterator", "init"); let _timer = Profiler::global().start_event("ArrayIterator", "init");
BuiltInBuilder::with_intrinsic::<Self>(realm) BuiltInBuilder::with_intrinsic_static_shape::<Self>(
.prototype( realm,
realm &boa_builtins::ARRAY_ITERATOR_PROTOTYPE_STATIC_SHAPE,
.intrinsics() )
.objects() .prototype(
.iterator_prototypes() realm
.iterator(), .intrinsics()
) .objects()
.static_method(Self::next, "next", 0) .iterator_prototypes()
.static_property( .iterator(),
JsSymbol::to_string_tag(), )
"Array Iterator", .static_method(Self::next, 0)
Attribute::CONFIGURABLE, .static_property("Array Iterator")
) .build();
.build();
} }
fn get(intrinsics: &Intrinsics) -> JsObject { fn get(intrinsics: &Intrinsics) -> JsObject {

20
boa_engine/src/builtins/iterable/mod.rs

@ -40,7 +40,7 @@ macro_rules! if_abrupt_close_iterator {
pub(crate) use if_abrupt_close_iterator; pub(crate) use if_abrupt_close_iterator;
/// The built-in iterator prototypes. /// The built-in iterator prototypes.
#[derive(Debug, Default, Trace, Finalize)] #[derive(Debug, Trace, Finalize)]
pub struct IteratorPrototypes { pub struct IteratorPrototypes {
/// The `IteratorPrototype` object. /// The `IteratorPrototype` object.
iterator: JsObject, iterator: JsObject,
@ -74,6 +74,24 @@ pub struct IteratorPrototypes {
segment: JsObject, segment: JsObject,
} }
impl Default for IteratorPrototypes {
fn default() -> Self {
Self {
array: JsObject::default_with_static_shape(),
iterator: JsObject::default(),
async_iterator: JsObject::default(),
async_from_sync_iterator: JsObject::default(),
set: JsObject::default(),
string: JsObject::default(),
regexp_string: JsObject::default(),
map: JsObject::default(),
for_in: JsObject::default(),
#[cfg(feature = "intl")]
segment: JsObject::default(),
}
}
}
impl IteratorPrototypes { impl IteratorPrototypes {
/// Returns the `ArrayIteratorPrototype` object. /// Returns the `ArrayIteratorPrototype` object.
#[inline] #[inline]

16
boa_engine/src/builtins/mod.rs

@ -855,9 +855,15 @@ struct BuiltInBuilderStaticShape<'ctx> {
object: JsObject, object: JsObject,
property_index: usize, property_index: usize,
storage: Vec<JsValue>, storage: Vec<JsValue>,
prototype: JsObject,
} }
impl BuiltInBuilderStaticShape<'_> { impl BuiltInBuilderStaticShape<'_> {
fn prototype(mut self, prototype: JsObject) -> Self {
self.prototype = prototype;
self
}
/// Adds a new static method to the builtin object. /// Adds a new static method to the builtin object.
fn static_method(mut self, function: NativeFunctionPointer, length: usize) -> Self { fn static_method(mut self, function: NativeFunctionPointer, length: usize) -> Self {
let name = self.shape.get_string_key_expect(self.property_index); let name = self.shape.get_string_key_expect(self.property_index);
@ -890,14 +896,7 @@ impl BuiltInBuilderStaticShape<'_> {
let mut object = self.object.borrow_mut(); let mut object = self.object.borrow_mut();
object.properties_mut().shape = StaticShape::new(self.shape).into(); object.properties_mut().shape = StaticShape::new(self.shape).into();
self.storage.push( self.storage.push(self.prototype.into());
self.realm
.intrinsics()
.constructors()
.object()
.prototype()
.into(),
);
object.properties_mut().storage = self.storage; object.properties_mut().storage = self.storage;
} }
} }
@ -924,6 +923,7 @@ impl<'ctx> BuiltInBuilder<'ctx, OrdinaryObject> {
object: I::get(realm.intrinsics()), object: I::get(realm.intrinsics()),
storage: Vec::with_capacity(shape.storage_len), storage: Vec::with_capacity(shape.storage_len),
property_index: 0, property_index: 0,
prototype: realm.intrinsics().constructors().object().prototype(),
} }
} }
} }

Loading…
Cancel
Save