Skip to content

Commit e1ed8a8

Browse files
committed
add laziness for math
1 parent 0725691 commit e1ed8a8

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

core/engine/src/builtins/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ impl Realm {
206206
AsyncIterator::init(self);
207207
AsyncFromSyncIterator::init(self);
208208
ForInIterator::init(self);
209-
Math::init(self);
210209
Json::init(self);
211210
ArrayIterator::init(self);
212211
Proxy::init(self);

core/engine/src/context/intrinsics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use boa_macros::js_str;
66
use crate::{
77
builtins::{
88
function::ConstructorKind, iterable::IteratorPrototypes, uri::UriFunctions, Array, Date,
9-
IntrinsicObject, OrdinaryObject,
9+
IntrinsicObject, Math, OrdinaryObject,
1010
},
1111
js_string,
1212
native_function::NativeFunctionObject,
@@ -1168,7 +1168,7 @@ impl IntrinsicObjects {
11681168
pub(crate) fn uninit(realm_inner: &WeakGc<RealmInner>) -> Option<Self> {
11691169
Some(Self {
11701170
reflect: JsObject::default(),
1171-
math: JsObject::default(),
1171+
math: JsObject::lazy(Math::init, realm_inner),
11721172
json: JsObject::default(),
11731173
throw_type_error: JsFunction::empty_intrinsic_function(false),
11741174
array_prototype_values: JsFunction::empty_intrinsic_function(false),

core/engine/src/object/jsobject.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
use super::{
66
internal_methods::{InternalMethodContext, InternalObjectMethods, ORDINARY_INTERNAL_METHODS},
77
shape::RootShape,
8-
JsPrototype, LazyBuiltIn, LazyPrototype, NativeObject, Object, PrivateName, PropertyMap,
8+
BuiltinKind, JsPrototype, LazyBuiltIn, LazyPrototype, NativeObject, Object, PrivateName,
9+
PropertyMap,
910
};
1011
use crate::{
1112
builtins::{
@@ -17,10 +18,11 @@ use crate::{
1718
error::JsNativeError,
1819
js_string,
1920
property::{PropertyDescriptor, PropertyKey},
21+
realm::{Realm, RealmInner},
2022
value::PreferredType,
2123
Context, JsResult, JsString, JsValue,
2224
};
23-
use boa_gc::{self, Finalize, Gc, GcBox, GcRefCell, Trace};
25+
use boa_gc::{self, Finalize, Gc, GcBox, GcRefCell, Trace, WeakGc};
2426
use boa_macros::js_str;
2527
use std::{
2628
cell::RefCell,
@@ -92,8 +94,20 @@ impl JsObject {
9294
inner: coerce_gc(gc),
9395
}
9496
}
97+
98+
/// Creates a new lazy `JsObject` from its inner object and its vtable.
99+
/// This object will call init([realm]) when it's first accessed
100+
pub(crate) fn lazy(init: fn(&Realm) -> (), realm_inner: &WeakGc<RealmInner>) -> Self {
101+
let data = LazyBuiltIn {
102+
init_and_realm: Some((init, realm_inner.clone())),
103+
kind: BuiltinKind::Ordinary,
104+
};
105+
106+
Self::from_proto_and_data(None, data)
107+
}
108+
95109
/// Creates a new lazy `JsObject` from its inner object and its vtable.
96-
/// This is used for built-in objects that are lazily initialized.
110+
/// This is used for built-in objects that are prototypes of Constructors.
97111
pub(crate) fn lazy_prototype(constructor: JsObject<LazyBuiltIn>) -> Self {
98112
Self::from_proto_and_data(None, LazyPrototype { constructor })
99113
}

0 commit comments

Comments
 (0)