Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lazy builtins #3973

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
27 changes: 20 additions & 7 deletions core/engine/src/builtins/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
native_function::{NativeFunctionObject, NativeFunctionPointer},
object::{
shape::{property_table::PropertyTableInner, slot::SlotAttributes},
FunctionBinding, JsFunction, JsPrototype, CONSTRUCTOR, PROTOTYPE,
FunctionBinding, JsFunction, JsPrototype, LazyBuiltIn, CONSTRUCTOR, PROTOTYPE,
},
property::{Attribute, PropertyDescriptor, PropertyKey},
realm::Realm,
Expand Down Expand Up @@ -391,12 +391,25 @@ impl BuiltInConstructorWithPrototype<'_> {
}

let mut object = self.object.borrow_mut();
let function = object
.downcast_mut::<NativeFunctionObject>()
.expect("Builtin must be a function object");
function.f = NativeFunction::from_fn_ptr(self.function);
function.constructor = Some(ConstructorKind::Base);
function.realm = Some(self.realm.clone());
Comment on lines -394 to -399
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I suggested to remove this, but I think this is still needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made some updates to this, take a look

if object.is::<NativeFunctionObject>() {
let function = object
.downcast_mut::<NativeFunctionObject>()
.expect("Builtin must be a function object");
function.f = NativeFunction::from_fn_ptr(self.function);
function.constructor = Some(ConstructorKind::Base);
function.realm = Some(self.realm.clone());
} else if object.is::<LazyBuiltIn>() {
let lazy = object
.downcast_mut::<LazyBuiltIn>()
.expect("Builtin must be a lazy object");
lazy.set_constructor(
NativeFunction::from_fn_ptr(self.function),
self.realm.clone(),
);
} else {
unreachable!("The object must be a function or a lazy object");
}

object
.properties_mut()
.shape
Expand Down
4 changes: 2 additions & 2 deletions core/engine/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
get_prototype_from_constructor, CallValue, InternalObjectMethods,
ORDINARY_INTERNAL_METHODS,
},
JsData, JsFunction, JsObject, PrivateElement, PrivateName,
JsData, JsFunction, JsObject, LazyBuiltIn, PrivateElement, PrivateName,
},
property::{Attribute, PropertyDescriptor, PropertyKey},
realm::Realm,
Expand Down Expand Up @@ -843,7 +843,7 @@ impl BuiltInFunctionObject {
};

let object_borrow = object.borrow();
if object_borrow.is::<NativeFunctionObject>() {
if object_borrow.is::<NativeFunctionObject>() || object_borrow.is::<LazyBuiltIn>() {
let name = {
// Is there a case here where if there is no name field on a value
// name should default to None? Do all functions have names set?
Expand Down
5 changes: 2 additions & 3 deletions core/engine/src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,15 @@ impl Realm {
AsyncIterator::init(self);
AsyncFromSyncIterator::init(self);
ForInIterator::init(self);
Math::init(self);
// Math::init(self);
Json::init(self);
Array::init(self);
ArrayIterator::init(self);
Proxy::init(self);
ArrayBuffer::init(self);
SharedArrayBuffer::init(self);
BigInt::init(self);
Boolean::init(self);
Date::init(self);
// Date::init(self);
DataView::init(self);
Map::init(self);
MapIterator::init(self);
Expand Down
34 changes: 23 additions & 11 deletions core/engine/src/context/intrinsics.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
//! Data structures that contain intrinsic objects and constructors.

use boa_gc::{Finalize, Trace};
use boa_gc::{Finalize, Trace, WeakGc};
use boa_macros::js_str;

use crate::{
builtins::{iterable::IteratorPrototypes, uri::UriFunctions, Array, OrdinaryObject},
builtins::{
iterable::IteratorPrototypes, uri::UriFunctions, Array, Date, IntrinsicObject, Math,
OrdinaryObject,
},
js_string,
object::{
internal_methods::immutable_prototype::IMMUTABLE_PROTOTYPE_EXOTIC_INTERNAL_METHODS,
shape::{shared_shape::template::ObjectTemplate, RootShape},
JsFunction, JsObject, Object, CONSTRUCTOR, PROTOTYPE,
},
property::{Attribute, PropertyKey},
realm::{Realm, RealmInner},
JsSymbol,
};

Expand Down Expand Up @@ -40,13 +44,13 @@ impl Intrinsics {
/// To initialize all the intrinsics with their spec properties, see [`Realm::initialize`].
///
/// [`Realm::initialize`]: crate::realm::Realm::initialize
pub(crate) fn uninit(root_shape: &RootShape) -> Option<Self> {
let constructors = StandardConstructors::default();
pub(crate) fn uninit(root_shape: &RootShape, realm_inner: &WeakGc<RealmInner>) -> Option<Self> {
let constructors = StandardConstructors::new(realm_inner);
let templates = ObjectTemplates::new(root_shape, &constructors);

Some(Self {
constructors,
objects: IntrinsicObjects::uninit()?,
objects: IntrinsicObjects::uninit(realm_inner)?,
templates,
})
}
Expand Down Expand Up @@ -95,6 +99,14 @@ impl StandardConstructor {
}
}

/// Similar to `with_prototype`, but the prototype is lazily initialized.
fn lazy(init: fn(&Realm) -> (), realm_inner: WeakGc<RealmInner>) -> Self {
Self {
constructor: JsFunction::lazy_intrinsic_function(true, init, realm_inner),
prototype: JsObject::default(),
}
}

/// Build a constructor with a defined prototype.
fn with_prototype(prototype: JsObject) -> Self {
Self {
Expand Down Expand Up @@ -203,23 +215,23 @@ pub struct StandardConstructors {
calendar: StandardConstructor,
}

impl Default for StandardConstructors {
fn default() -> Self {
impl StandardConstructors {
fn new(realm_inner: &WeakGc<RealmInner>) -> Self {
Self {
object: StandardConstructor::with_prototype(JsObject::from_object_and_vtable(
Object::<OrdinaryObject>::default(),
&IMMUTABLE_PROTOTYPE_EXOTIC_INTERNAL_METHODS,
)),
async_generator_function: StandardConstructor::default(),
proxy: StandardConstructor::default(),
date: StandardConstructor::default(),
date: StandardConstructor::lazy(Date::init, realm_inner.clone()),
function: StandardConstructor {
constructor: JsFunction::empty_intrinsic_function(true),
prototype: JsFunction::empty_intrinsic_function(false).into(),
},
async_function: StandardConstructor::default(),
generator_function: StandardConstructor::default(),
array: StandardConstructor::with_prototype(JsObject::from_proto_and_data(None, Array)),
array: StandardConstructor::lazy(Array::init, realm_inner.clone()),
bigint: StandardConstructor::default(),
number: StandardConstructor::with_prototype(JsObject::from_proto_and_data(None, 0.0)),
boolean: StandardConstructor::with_prototype(JsObject::from_proto_and_data(
Expand Down Expand Up @@ -1120,10 +1132,10 @@ impl IntrinsicObjects {
///
/// [`Realm::initialize`]: crate::realm::Realm::initialize
#[allow(clippy::unnecessary_wraps)]
pub(crate) fn uninit() -> Option<Self> {
pub(crate) fn uninit(realm_inner: &WeakGc<RealmInner>) -> Option<Self> {
Some(Self {
reflect: JsObject::default(),
math: JsObject::default(),
math: JsObject::lazy(Math::init, realm_inner),
json: JsObject::default(),
throw_type_error: JsFunction::empty_intrinsic_function(false),
array_prototype_values: JsFunction::empty_intrinsic_function(false),
Expand Down
27 changes: 26 additions & 1 deletion core/engine/src/object/builtins/jsfunction.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
//! A Rust API wrapper for Boa's `Function` Builtin ECMAScript Object
use crate::realm::{Realm, RealmInner};
use crate::{
builtins::function::ConstructorKind, native_function::NativeFunctionObject, object::JsObject,
value::TryFromJs, Context, JsNativeError, JsResult, JsValue, NativeFunction, TryIntoJsResult,
};
use boa_gc::{Finalize, Trace};
use boa_gc::{Finalize, Trace, WeakGc};
use std::marker::PhantomData;
use std::ops::Deref;

use super::lazy_builtin::{BuiltinKind, LazyBuiltIn};

/// A trait for converting a tuple of Rust values into a vector of `JsValue`,
/// to be used as arguments for a JavaScript function.
pub trait TryIntoJsArguments {
Expand Down Expand Up @@ -135,6 +138,28 @@ impl JsFunction {
}
}

/// Creates a new, lazy intrinsic functionobject with only its function internal methods set.
/// When the function is accessed it will call init from the procided init function
pub(crate) fn lazy_intrinsic_function(
constructor: bool,
init: fn(&Realm),
realm_inner: WeakGc<RealmInner>,
) -> Self {
Self {
inner: JsObject::from_proto_and_data(
None,
LazyBuiltIn {
init_and_realm: Some((init, realm_inner)),
kind: BuiltinKind::Function(NativeFunctionObject {
f: NativeFunction::from_fn_ptr(|_, _, _| Ok(JsValue::undefined())),
constructor: constructor.then_some(ConstructorKind::Base),
realm: None,
}),
},
),
}
}

/// Creates a [`JsFunction`] from a [`JsObject`], or returns `None` if the object is not a function.
///
/// This does not clone the fields of the function, it only does a shallow clone of the object.
Expand Down
Loading
Loading