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 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions core/engine/src/builtins/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,6 @@ 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 -390 to -395
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.

object
.properties_mut()
.shape
Expand Down
9 changes: 8 additions & 1 deletion core/engine/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,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 @@ -850,6 +850,13 @@ impl BuiltInFunctionObject {
);
} else if object_borrow.is::<Proxy>() || object_borrow.is::<BoundFunction>() {
return Ok(js_string!("function () { [native code] }").into());
} else if object_borrow.is::<LazyBuiltIn>() {
let name = object_borrow
.downcast_ref::<LazyBuiltIn>()
.map_or_else(|| js_string!(), |built_in| built_in.name.clone());
return Ok(
js_string!(js_str!("function "), &name, js_str!("() { [native code] }")).into(),
);
Comment on lines +853 to +859
Copy link
Member

Choose a reason for hiding this comment

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

This is not needed if you move the check for LazyBuiltIn to the same branch as the check for NativeFunctionObject. The reason is that we call JsObject::get inside it, which will already initialize the name of the builtin, printing the correct result.

}

let function = object_borrow
Expand Down
3 changes: 1 addition & 2 deletions core/engine/src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,13 @@ impl Realm {
ForInIterator::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
29 changes: 21 additions & 8 deletions core/engine/src/context/intrinsics.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
//! 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 boa_string::JsString;

use crate::{
builtins::{iterable::IteratorPrototypes, uri::UriFunctions, Array, OrdinaryObject},
builtins::{
iterable::IteratorPrototypes, uri::UriFunctions, Array, BuiltInObject, Date,
IntrinsicObject, 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,8 +45,8 @@ 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 {
Expand Down Expand Up @@ -95,6 +100,14 @@ impl StandardConstructor {
}
}

/// Similar to `with_prototype`, but the prototype is lazily initialized.
fn lazy(init: fn(&Realm) -> (), name: JsString, realm_inner: WeakGc<RealmInner>) -> Self {
Self {
constructor: JsFunction::lazy_intrinsic_function(true, init, name, 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 +216,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, Date::NAME, 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, Array::NAME, 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
35 changes: 34 additions & 1 deletion core/engine/src/object/builtins/jsfunction.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
//! 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 boa_string::JsString;
use std::cell::Cell;
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 @@ -99,6 +104,34 @@ 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),
name: JsString,
realm_inner: WeakGc<RealmInner>,
) -> Self {
let kind = if constructor {
BuiltinKind::Function(Self::empty_intrinsic_function(constructor))
} else {
BuiltinKind::Ordinary
};

Self {
inner: JsObject::from_proto_and_data(
None,
LazyBuiltIn {
init,
is_initialized: Cell::new(false),
kind,
name,
realm_inner: Some(realm_inner),
},
),
}
}

/// 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