Skip to content

Commit

Permalink
Feeding realm through into the build function
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonwilliams committed Sep 15, 2024
1 parent ac4c15b commit c52c687
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 107 deletions.
4 changes: 0 additions & 4 deletions core/engine/src/builtins/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,6 @@ impl BuiltInConstructorWithPrototype<'_> {
}

let mut object = self.object.borrow_mut();
let built_in = object
.downcast_mut::<BuiltIn>()
.expect("Builtin must be a function object");
built_in.realm = Some(self.realm.clone());
object
.properties_mut()
.shape
Expand Down
18 changes: 9 additions & 9 deletions core/engine/src/context/intrinsics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! 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::{
Expand All @@ -14,7 +14,7 @@ use crate::{
JsFunction, JsObject, Object, CONSTRUCTOR, PROTOTYPE,
},
property::{Attribute, PropertyKey},
realm::Realm,
realm::{Realm, RealmInner},
JsSymbol,
};

Expand Down Expand Up @@ -43,8 +43,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 @@ -99,9 +99,9 @@ impl StandardConstructor {
}

/// Similar to `with_prototype`, but the prototype is lazily initialized.
fn with_lazy(init: fn(&Realm) -> ()) -> Self {
fn with_lazy(init: fn(&Realm) -> (), realm_inner: WeakGc<RealmInner>) -> Self {
Self {
constructor: JsFunction::lazy_intrinsic_function(true, init),
constructor: JsFunction::lazy_intrinsic_function(true, init, realm_inner),
prototype: JsObject::default(),
}
}
Expand Down Expand Up @@ -214,8 +214,8 @@ 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(),
Expand All @@ -230,7 +230,7 @@ impl Default for StandardConstructors {
},
async_function: StandardConstructor::default(),
generator_function: StandardConstructor::default(),
array: StandardConstructor::with_lazy(Array::init),
array: StandardConstructor::with_lazy(Array::init, realm_inner),
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
12 changes: 8 additions & 4 deletions core/engine/src/object/builtins/jsfunction.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! A Rust API wrapper for Boa's `Function` Builtin ECMAScript Object
use crate::realm::Realm;
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::cell::Cell;
use std::marker::PhantomData;
use std::ops::Deref;
Expand Down Expand Up @@ -105,15 +105,19 @@ 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)) -> Self {
pub(crate) fn lazy_intrinsic_function(
constructor: bool,
init: fn(&Realm),
realm_inner: WeakGc<RealmInner>,
) -> Self {
Self {
inner: JsObject::from_proto_and_data(
None,
BuiltIn {
init,
is_initialized: Cell::new(false),
kind: BuiltinKind::Constructor(Self::empty_intrinsic_function(constructor)),
realm: None,
realm_inner: Some(realm_inner),
},
),
}
Expand Down
Loading

0 comments on commit c52c687

Please sign in to comment.