|
| 1 | +use crate::{ |
| 2 | + object::{ |
| 3 | + internal_methods::{ |
| 4 | + non_existant_call, non_existant_construct, ordinary_define_own_property, |
| 5 | + ordinary_delete, ordinary_get, ordinary_get_own_property, ordinary_get_prototype_of, |
| 6 | + ordinary_has_property, ordinary_is_extensible, ordinary_own_property_keys, |
| 7 | + ordinary_prevent_extensions, ordinary_set, ordinary_set_prototype_of, ordinary_try_get, |
| 8 | + InternalMethodContext, InternalObjectMethods, |
| 9 | + }, |
| 10 | + JsPrototype, |
| 11 | + }, |
| 12 | + property::{PropertyDescriptor, PropertyKey}, |
| 13 | + Context, JsData, JsObject, JsResult, JsValue, |
| 14 | +}; |
| 15 | +use boa_gc::{Finalize, Trace}; |
| 16 | + |
| 17 | +use super::LazyBuiltIn; |
| 18 | + |
| 19 | +/// The `LazyArrayPrototype` struct is responsible for ensuring that the prototype |
| 20 | +/// of a constructor is lazily initialized. In JavaScript, constructors have a |
| 21 | +/// `prototype` property that points to an object which is used as the prototype |
| 22 | +/// for instances created by that constructor. |
| 23 | +/// |
| 24 | +/// The `LazyArrayPrototype` struct is used within `JsObject` (e.g., `JsObject<LazyPrototype>`) |
| 25 | +/// to defer the creation of the prototype object until it is actually needed. |
| 26 | +/// This lazy initialization helps improve performance by avoiding the creation |
| 27 | +/// of the prototype object until it is necessary. |
| 28 | +/// |
| 29 | +/// Each `LazyArrayPrototype` instance points to the constructor's `lazyBuiltin` (via its |
| 30 | +/// object) and triggers the initialization of the prototype if any methods on |
| 31 | +/// the prototype are called. |
| 32 | +
|
| 33 | +#[derive(Clone, Trace, Finalize, Debug)] |
| 34 | +#[allow(clippy::type_complexity)] |
| 35 | +pub struct LazyArrayPrototype { |
| 36 | + pub(crate) constructor: JsObject<LazyBuiltIn>, |
| 37 | +} |
| 38 | + |
| 39 | +// Implement the trait for JsData by overriding all internal_methods by calling init on the LazyBuiltIn associated with this prototype |
| 40 | +impl JsData for LazyArrayPrototype { |
| 41 | + fn internal_methods(&self) -> &'static InternalObjectMethods { |
| 42 | + &LAZY_INTERNAL_METHODS |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +pub(crate) static LAZY_INTERNAL_METHODS: InternalObjectMethods = InternalObjectMethods { |
| 47 | + __get_prototype_of__: lazy_get_prototype_of, |
| 48 | + __set_prototype_of__: lazy_set_prototype_of, |
| 49 | + __is_extensible__: lazy_is_extensible, |
| 50 | + __prevent_extensions__: lazy_prevent_extensions, |
| 51 | + __get_own_property__: lazy_get_own_property, |
| 52 | + __define_own_property__: lazy_define_own_property, |
| 53 | + __has_property__: lazy_has_property, |
| 54 | + __try_get__: lazy_try_get, |
| 55 | + __get__: lazy_get, |
| 56 | + __set__: lazy_set, |
| 57 | + __delete__: lazy_delete, |
| 58 | + __own_property_keys__: lazy_own_property_keys, |
| 59 | + __call__: non_existant_call, |
| 60 | + __construct__: non_existant_construct, |
| 61 | +}; |
| 62 | + |
| 63 | +pub(crate) fn lazy_get_prototype_of( |
| 64 | + obj: &JsObject, |
| 65 | + context: &mut Context, |
| 66 | +) -> JsResult<JsPrototype> { |
| 67 | + let lazy_prototype: JsObject<LazyArrayPrototype> = obj |
| 68 | + .clone() |
| 69 | + .downcast::<LazyArrayPrototype>() |
| 70 | + .expect("obj is not a Builtin"); |
| 71 | + let lazy_built_in = &lazy_prototype.borrow_mut().data.constructor.clone(); |
| 72 | + LazyBuiltIn::ensure_init(lazy_built_in); |
| 73 | + ordinary_get_prototype_of(obj, context) |
| 74 | +} |
| 75 | + |
| 76 | +pub(crate) fn lazy_set_prototype_of( |
| 77 | + obj: &JsObject, |
| 78 | + prototype: JsPrototype, |
| 79 | + context: &mut Context, |
| 80 | +) -> JsResult<bool> { |
| 81 | + let lazy_prototype: JsObject<LazyArrayPrototype> = obj |
| 82 | + .clone() |
| 83 | + .downcast::<LazyArrayPrototype>() |
| 84 | + .expect("obj is not a Builtin"); |
| 85 | + let lazy_built_in = &lazy_prototype.borrow_mut().data.constructor.clone(); |
| 86 | + LazyBuiltIn::ensure_init(lazy_built_in); |
| 87 | + ordinary_set_prototype_of(obj, prototype, context) |
| 88 | +} |
| 89 | +pub(crate) fn lazy_is_extensible(obj: &JsObject, context: &mut Context) -> JsResult<bool> { |
| 90 | + let lazy_prototype: JsObject<LazyArrayPrototype> = obj |
| 91 | + .clone() |
| 92 | + .downcast::<LazyArrayPrototype>() |
| 93 | + .expect("obj is not a Builtin"); |
| 94 | + let lazy_built_in = &lazy_prototype.borrow_mut().data.constructor.clone(); |
| 95 | + LazyBuiltIn::ensure_init(lazy_built_in); |
| 96 | + ordinary_is_extensible(obj, context) |
| 97 | +} |
| 98 | + |
| 99 | +pub(crate) fn lazy_prevent_extensions(obj: &JsObject, context: &mut Context) -> JsResult<bool> { |
| 100 | + let lazy_prototype: JsObject<LazyArrayPrototype> = obj |
| 101 | + .clone() |
| 102 | + .downcast::<LazyArrayPrototype>() |
| 103 | + .expect("obj is not a Builtin"); |
| 104 | + let lazy_built_in = &lazy_prototype.borrow_mut().data.constructor.clone(); |
| 105 | + LazyBuiltIn::ensure_init(lazy_built_in); |
| 106 | + ordinary_prevent_extensions(obj, context) |
| 107 | +} |
| 108 | + |
| 109 | +pub(crate) fn lazy_get_own_property( |
| 110 | + obj: &JsObject, |
| 111 | + key: &PropertyKey, |
| 112 | + context: &mut InternalMethodContext<'_>, |
| 113 | +) -> JsResult<Option<PropertyDescriptor>> { |
| 114 | + let lazy_prototype: JsObject<LazyArrayPrototype> = obj |
| 115 | + .clone() |
| 116 | + .downcast::<LazyArrayPrototype>() |
| 117 | + .expect("obj is not a Builtin"); |
| 118 | + let lazy_built_in = &lazy_prototype.borrow_mut().data.constructor.clone(); |
| 119 | + LazyBuiltIn::ensure_init(lazy_built_in); |
| 120 | + ordinary_get_own_property(obj, key, context) |
| 121 | +} |
| 122 | + |
| 123 | +pub(crate) fn lazy_define_own_property( |
| 124 | + obj: &JsObject, |
| 125 | + key: &PropertyKey, |
| 126 | + desc: PropertyDescriptor, |
| 127 | + context: &mut InternalMethodContext<'_>, |
| 128 | +) -> JsResult<bool> { |
| 129 | + let lazy_prototype: JsObject<LazyArrayPrototype> = obj |
| 130 | + .clone() |
| 131 | + .downcast::<LazyArrayPrototype>() |
| 132 | + .expect("obj is not a Builtin"); |
| 133 | + let lazy_built_in = &lazy_prototype.borrow_mut().data.constructor.clone(); |
| 134 | + LazyBuiltIn::ensure_init(lazy_built_in); |
| 135 | + |
| 136 | + ordinary_define_own_property(obj, key, desc, context) |
| 137 | +} |
| 138 | + |
| 139 | +pub(crate) fn lazy_has_property( |
| 140 | + obj: &JsObject, |
| 141 | + key: &PropertyKey, |
| 142 | + context: &mut InternalMethodContext<'_>, |
| 143 | +) -> JsResult<bool> { |
| 144 | + let lazy_prototype: JsObject<LazyArrayPrototype> = obj |
| 145 | + .clone() |
| 146 | + .downcast::<LazyArrayPrototype>() |
| 147 | + .expect("obj is not a Builtin"); |
| 148 | + let lazy_built_in = &lazy_prototype.borrow_mut().data.constructor.clone(); |
| 149 | + LazyBuiltIn::ensure_init(lazy_built_in); |
| 150 | + ordinary_has_property(obj, key, context) |
| 151 | +} |
| 152 | + |
| 153 | +pub(crate) fn lazy_try_get( |
| 154 | + obj: &JsObject, |
| 155 | + key: &PropertyKey, |
| 156 | + receiver: JsValue, |
| 157 | + context: &mut InternalMethodContext<'_>, |
| 158 | +) -> JsResult<Option<JsValue>> { |
| 159 | + let lazy_prototype: JsObject<LazyArrayPrototype> = obj |
| 160 | + .clone() |
| 161 | + .downcast::<LazyArrayPrototype>() |
| 162 | + .expect("obj is not a Builtin"); |
| 163 | + let lazy_built_in = &lazy_prototype.borrow_mut().data.constructor.clone(); |
| 164 | + LazyBuiltIn::ensure_init(lazy_built_in); |
| 165 | + |
| 166 | + ordinary_try_get(obj, key, receiver, context) |
| 167 | +} |
| 168 | + |
| 169 | +pub(crate) fn lazy_get( |
| 170 | + obj: &JsObject, |
| 171 | + key: &PropertyKey, |
| 172 | + receiver: JsValue, |
| 173 | + context: &mut InternalMethodContext<'_>, |
| 174 | +) -> JsResult<JsValue> { |
| 175 | + let lazy_prototype: JsObject<LazyArrayPrototype> = obj |
| 176 | + .clone() |
| 177 | + .downcast::<LazyArrayPrototype>() |
| 178 | + .expect("obj is not a Builtin"); |
| 179 | + let lazy_built_in = &lazy_prototype.borrow_mut().data.constructor.clone(); |
| 180 | + LazyBuiltIn::ensure_init(lazy_built_in); |
| 181 | + ordinary_get(obj, key, receiver, context) |
| 182 | +} |
| 183 | + |
| 184 | +pub(crate) fn lazy_set( |
| 185 | + obj: &JsObject, |
| 186 | + key: PropertyKey, |
| 187 | + value: JsValue, |
| 188 | + receiver: JsValue, |
| 189 | + context: &mut InternalMethodContext<'_>, |
| 190 | +) -> JsResult<bool> { |
| 191 | + let lazy_prototype: JsObject<LazyArrayPrototype> = obj |
| 192 | + .clone() |
| 193 | + .downcast::<LazyArrayPrototype>() |
| 194 | + .expect("obj is not a Builtin"); |
| 195 | + let lazy_built_in = &lazy_prototype.borrow_mut().data.constructor.clone(); |
| 196 | + LazyBuiltIn::ensure_init(lazy_built_in); |
| 197 | + ordinary_set(obj, key, value, receiver, context) |
| 198 | +} |
| 199 | + |
| 200 | +pub(crate) fn lazy_delete( |
| 201 | + obj: &JsObject, |
| 202 | + key: &PropertyKey, |
| 203 | + context: &mut InternalMethodContext<'_>, |
| 204 | +) -> JsResult<bool> { |
| 205 | + let lazy_prototype: JsObject<LazyArrayPrototype> = obj |
| 206 | + .clone() |
| 207 | + .downcast::<LazyArrayPrototype>() |
| 208 | + .expect("obj is not a Builtin"); |
| 209 | + let lazy_built_in = &lazy_prototype.borrow_mut().data.constructor.clone(); |
| 210 | + LazyBuiltIn::ensure_init(lazy_built_in); |
| 211 | + ordinary_delete(obj, key, context) |
| 212 | +} |
| 213 | + |
| 214 | +pub(crate) fn lazy_own_property_keys( |
| 215 | + obj: &JsObject, |
| 216 | + context: &mut Context, |
| 217 | +) -> JsResult<Vec<PropertyKey>> { |
| 218 | + let lazy_prototype: JsObject<LazyArrayPrototype> = obj |
| 219 | + .clone() |
| 220 | + .downcast::<LazyArrayPrototype>() |
| 221 | + .expect("obj is not a Builtin"); |
| 222 | + let lazy_built_in = &lazy_prototype.borrow_mut().data.constructor.clone(); |
| 223 | + LazyBuiltIn::ensure_init(lazy_built_in); |
| 224 | + |
| 225 | + ordinary_own_property_keys(obj, context) |
| 226 | +} |
0 commit comments