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

lazy builtins #3973

wants to merge 5 commits into from

Conversation

jasonwilliams
Copy link
Member

Lazy Builtins

@jasonwilliams jasonwilliams mentioned this pull request Aug 29, 2024
Handle toString for lazyBuiltIn objects
Comment on lines -390 to -395
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());
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.

Comment on lines +853 to +859
} 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(),
);
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.

Comment on lines +32 to +36
pub(crate) init: fn(&Realm),
pub(crate) is_initialized: Cell<bool>,
pub(crate) kind: BuiltinKind,
pub(crate) realm_inner: Option<WeakGc<RealmInner>>,
pub(crate) name: JsString,
Copy link
Member

Choose a reason for hiding this comment

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

We can put init and realm_inner into a single Option and remove is_initialized. That way, we can use Option::take (which will set the option to None on access) to determine if the builtin has already been initialized; if Option::take returns Some, initialize. Else, don't initialize.


#[derive(Debug, Clone, Trace, Finalize)]
pub(crate) enum BuiltinKind {
Function(JsFunction),
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, thinking a bit more about this, I think Function should store a NativeFunctionObject instead, and the builder methods should be adjusted to consider this. Else, the builder won't set the builtin to the correct value while initializing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants