Skip to content

Commit

Permalink
Fold init and realm into a single option type, no need for NAME to be…
Browse files Browse the repository at this point in the history
… passed through
  • Loading branch information
jasonwilliams committed Oct 6, 2024
1 parent 7b4069a commit cf4a379
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 197 deletions.
9 changes: 1 addition & 8 deletions core/engine/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,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 All @@ -850,13 +850,6 @@ 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(),
);
}

let function = object_borrow
Expand Down
13 changes: 6 additions & 7 deletions core/engine/src/context/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

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

use crate::{
builtins::{
iterable::IteratorPrototypes, uri::UriFunctions, Array, BuiltInObject, Date,
IntrinsicObject, OrdinaryObject,
iterable::IteratorPrototypes, uri::UriFunctions, Array, Date, IntrinsicObject,
OrdinaryObject,
},
js_string,
object::{
Expand Down Expand Up @@ -101,9 +100,9 @@ impl StandardConstructor {
}

/// Similar to `with_prototype`, but the prototype is lazily initialized.
fn lazy(init: fn(&Realm) -> (), name: JsString, realm_inner: WeakGc<RealmInner>) -> Self {
fn lazy(init: fn(&Realm) -> (), realm_inner: WeakGc<RealmInner>) -> Self {
Self {
constructor: JsFunction::lazy_intrinsic_function(true, init, name, realm_inner),
constructor: JsFunction::lazy_intrinsic_function(true, init, realm_inner),
prototype: JsObject::default(),
}
}
Expand Down Expand Up @@ -225,14 +224,14 @@ impl StandardConstructors {
)),
async_generator_function: StandardConstructor::default(),
proxy: StandardConstructor::default(),
date: StandardConstructor::lazy(Date::init, Date::NAME, realm_inner.clone()),
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::lazy(Array::init, Array::NAME, realm_inner.clone()),
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
8 changes: 1 addition & 7 deletions core/engine/src/object/builtins/jsfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use crate::{
value::TryFromJs, Context, JsNativeError, JsResult, JsValue, NativeFunction, TryIntoJsResult,
};
use boa_gc::{Finalize, Trace, WeakGc};
use boa_string::JsString;
use std::cell::Cell;
use std::marker::PhantomData;
use std::ops::Deref;

Expand Down Expand Up @@ -109,7 +107,6 @@ impl JsFunction {
pub(crate) fn lazy_intrinsic_function(
constructor: bool,
init: fn(&Realm),
name: JsString,
realm_inner: WeakGc<RealmInner>,
) -> Self {
let kind = if constructor {
Expand All @@ -122,11 +119,8 @@ impl JsFunction {
inner: JsObject::from_proto_and_data(
None,
LazyBuiltIn {
init,
is_initialized: Cell::new(false),
init_and_realm: Some((init, realm_inner)),
kind,
name,
realm_inner: Some(realm_inner),
},
),
}
Expand Down
Loading

0 comments on commit cf4a379

Please sign in to comment.