Skip to content

Commit

Permalink
Cleanup intrinsics and move to realm (#2555)
Browse files Browse the repository at this point in the history
Small (ish?) step towards having proper realm records 

This PR changes the following:

- Moves `Intrinsics` to `Realm`.
- Cleans up the initialization logic of our intrinsics to not depend on `Context`, unblocking things like #2314.
- Adds hooks to initialize the global object and the global this per the corresponding [`InitializeHostDefinedRealm ( )`](https://tc39.es/ecma262/#sec-initializehostdefinedrealm) hook. Though, this is currently broken because the vm uses `GlobalPropertyMap` instead of the `JsObject` API to initialize global properties.
  • Loading branch information
jedel1043 committed Feb 1, 2023
1 parent b76050d commit c039283
Show file tree
Hide file tree
Showing 91 changed files with 4,174 additions and 4,339 deletions.
5 changes: 4 additions & 1 deletion boa_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,10 @@ fn main() -> Result<(), io::Error> {
let args = Opt::parse();

let queue = Jobs::default();
let mut context = ContextBuilder::new().job_queue(&queue).build();
let mut context = ContextBuilder::new()
.job_queue(&queue)
.build()
.expect("cannot fail with default global object");

// Trace Output
context.set_trace(args.trace);
Expand Down
6 changes: 4 additions & 2 deletions boa_engine/benches/full.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Benchmarks of the whole execution engine in Boa.
use boa_engine::{realm::Realm, Context, Source};
use boa_engine::{context::DefaultHooks, realm::Realm, Context, Source};
use criterion::{criterion_group, criterion_main, Criterion};
use std::hint::black_box;

Expand All @@ -12,7 +12,9 @@ use std::hint::black_box;
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

fn create_realm(c: &mut Criterion) {
c.bench_function("Create Realm", move |b| b.iter(|| Realm::create(None)));
c.bench_function("Create Realm", move |b| {
b.iter(|| Realm::create(&DefaultHooks))
});
}

macro_rules! full_benchmarks {
Expand Down
62 changes: 26 additions & 36 deletions boa_engine/src/builtins/array/array_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
//! [spec]: https://tc39.es/ecma262/#sec-array-iterator-objects
use crate::{
builtins::{function::make_builtin_fn, iterable::create_iter_result_object, Array, JsValue},
builtins::{
iterable::create_iter_result_object, Array, BuiltInBuilder, IntrinsicObject, JsValue,
},
context::intrinsics::Intrinsics,
error::JsNativeError,
object::{JsObject, ObjectData},
property::{PropertyDescriptor, PropertyNameKind},
property::{Attribute, PropertyNameKind},
symbol::JsSymbol,
Context, JsResult,
};
Expand All @@ -31,9 +34,27 @@ pub struct ArrayIterator {
done: bool,
}

impl ArrayIterator {
pub(crate) const NAME: &'static str = "ArrayIterator";
impl IntrinsicObject for ArrayIterator {
fn init(intrinsics: &Intrinsics) {
let _timer = Profiler::global().start_event("ArrayIterator", "init");

BuiltInBuilder::with_intrinsic::<Self>(intrinsics)
.prototype(intrinsics.objects().iterator_prototypes().iterator())
.static_method(Self::next, "next", 0)
.static_property(
JsSymbol::to_string_tag(),
"Array Iterator",
Attribute::CONFIGURABLE,
)
.build();
}

fn get(intrinsics: &Intrinsics) -> JsObject {
intrinsics.objects().iterator_prototypes().array()
}
}

impl ArrayIterator {
fn new(array: JsObject, kind: PropertyNameKind) -> Self {
Self {
array,
Expand All @@ -57,11 +78,7 @@ impl ArrayIterator {
context: &Context<'_>,
) -> JsValue {
let array_iterator = JsObject::from_proto_and_data(
context
.intrinsics()
.objects()
.iterator_prototypes()
.array_iterator(),
context.intrinsics().objects().iterator_prototypes().array(),
ObjectData::array_iterator(Self::new(array, kind)),
);
array_iterator.into()
Expand Down Expand Up @@ -130,31 +147,4 @@ impl ArrayIterator {
}
}
}

/// Create the `%ArrayIteratorPrototype%` object
///
/// More information:
/// - [ECMA reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-%arrayiteratorprototype%-object
pub(crate) fn create_prototype(
iterator_prototype: JsObject,
context: &mut Context<'_>,
) -> JsObject {
let _timer = Profiler::global().start_event(Self::NAME, "init");

// Create prototype
let array_iterator =
JsObject::from_proto_and_data(iterator_prototype, ObjectData::ordinary());
make_builtin_fn(Self::next, "next", &array_iterator, 0, context);

let to_string_tag = JsSymbol::to_string_tag();
let to_string_tag_property = PropertyDescriptor::builder()
.value("Array Iterator")
.writable(false)
.enumerable(false)
.configurable(true);
array_iterator.insert(to_string_tag, to_string_tag_property);
array_iterator
}
}
Loading

0 comments on commit c039283

Please sign in to comment.