-
-
Notifications
You must be signed in to change notification settings - Fork 397
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
base: main
Are you sure you want to change the base?
lazy builtins #3973
Conversation
1c4cc00
to
c52c687
Compare
Handle toString for lazyBuiltIn objects
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()); |
There was a problem hiding this comment.
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.
} 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(), | ||
); |
There was a problem hiding this comment.
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.
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, |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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.
Lazy Builtins