Skip to content

Commit

Permalink
Proof of concept of ordinary function being initialized. plus tidy up…
Browse files Browse the repository at this point in the history
… and documentation
  • Loading branch information
jasonwilliams committed Oct 6, 2024
1 parent 5ceedff commit a373d78
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion core/engine/src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl Realm {
AsyncIterator::init(self);
AsyncFromSyncIterator::init(self);
ForInIterator::init(self);
Math::init(self);
// Math::init(self);
Json::init(self);
ArrayIterator::init(self);
Proxy::init(self);
Expand Down
8 changes: 4 additions & 4 deletions core/engine/src/context/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use boa_macros::js_str;

use crate::{
builtins::{
iterable::IteratorPrototypes, uri::UriFunctions, Array, Date, IntrinsicObject,
iterable::IteratorPrototypes, uri::UriFunctions, Array, Date, IntrinsicObject, Math,
OrdinaryObject,
},
js_string,
Expand Down Expand Up @@ -50,7 +50,7 @@ impl Intrinsics {

Some(Self {
constructors,
objects: IntrinsicObjects::uninit()?,
objects: IntrinsicObjects::uninit(realm_inner)?,
templates,
})
}
Expand Down Expand Up @@ -1132,10 +1132,10 @@ impl IntrinsicObjects {
///
/// [`Realm::initialize`]: crate::realm::Realm::initialize
#[allow(clippy::unnecessary_wraps)]
pub(crate) fn uninit() -> Option<Self> {
pub(crate) fn uninit(realm_inner: &WeakGc<RealmInner>) -> Option<Self> {
Some(Self {
reflect: JsObject::default(),
math: JsObject::default(),
math: JsObject::lazy(Math::init, realm_inner),
json: JsObject::default(),
throw_type_error: JsFunction::empty_intrinsic_function(false),
array_prototype_values: JsFunction::empty_intrinsic_function(false),
Expand Down
16 changes: 14 additions & 2 deletions core/engine/src/object/jsobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use super::{
internal_methods::{InternalMethodContext, InternalObjectMethods, ORDINARY_INTERNAL_METHODS},
shape::RootShape,
JsPrototype, NativeObject, Object, PrivateName, PropertyMap,
BuiltinKind, JsPrototype, LazyBuiltIn, NativeObject, Object, PrivateName, PropertyMap,
};
use crate::{
builtins::{
Expand All @@ -17,10 +17,11 @@ use crate::{
error::JsNativeError,
js_string,
property::{PropertyDescriptor, PropertyKey},
realm::{Realm, RealmInner},
value::PreferredType,
Context, JsResult, JsString, JsValue,
};
use boa_gc::{self, Finalize, Gc, GcBox, GcRefCell, Trace};
use boa_gc::{self, Finalize, Gc, GcBox, GcRefCell, Trace, WeakGc};
use boa_macros::js_str;
use std::{
cell::RefCell,
Expand Down Expand Up @@ -92,6 +93,17 @@ impl JsObject {
inner: coerce_gc(gc),
}
}
/// Creates a new lazy `JsObject` from its inner object and its vtable.
/// This is used for built-in objects that are lazily initialized.
pub(crate) fn lazy(init: fn(&Realm) -> (), realm_inner: &WeakGc<RealmInner>) -> Self {
Self::from_proto_and_data(
None,
LazyBuiltIn {
init_and_realm: Some((init, realm_inner.clone())),
kind: BuiltinKind::Ordinary,
},
)
}

/// Creates a new ordinary object with its prototype set to the `Object` prototype.
///
Expand Down
7 changes: 7 additions & 0 deletions core/engine/src/realm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use boa_profiler::Profiler;
/// In the specification these are called Realm Records.
#[derive(Clone, Trace, Finalize)]
pub struct Realm {
/// The inner data of the realm, which includes the intrinsics, environment,
/// global object, and other realm-specific information.
pub inner: Gc<RealmInner>,
}

Expand All @@ -53,6 +55,11 @@ impl std::fmt::Debug for Realm {
}

#[derive(Trace, Finalize)]

/// The inner data of a Realm.
///
/// This struct contains all the realm-specific information, including the intrinsics,
/// environment, global object, and other necessary data for the execution context.
pub struct RealmInner {
intrinsics: Intrinsics,

Expand Down

0 comments on commit a373d78

Please sign in to comment.