Skip to content

Commit

Permalink
Access hooks and queue using getters
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Jan 11, 2023
1 parent ae8aeb1 commit 868705a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 55 deletions.
2 changes: 1 addition & 1 deletion boa_engine/src/builtins/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl Eval {
// Because of implementation details the following code differs from the spec.

// 5. Perform ? HostEnsureCanCompileStrings(evalRealm).
context.host_hooks.ensure_can_compile_strings(context)?;
context.host_hooks().ensure_can_compile_strings(context)?;

// 11. Perform the following substeps in an implementation-defined order, possibly interleaving parsing and error detection:
// a. Let script be ParseText(StringToCodePoints(x), Script).
Expand Down
24 changes: 13 additions & 11 deletions boa_engine/src/builtins/promise/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ impl Promise {
};

// 13. Let thenJobCallback be HostMakeJobCallback(thenAction).
let then_job_callback = context.host_hooks.make_job_callback(then_action, context);
let then_job_callback = context.host_hooks().make_job_callback(then_action, context);

// 14. Let job be NewPromiseResolveThenableJob(promise, resolution, thenJobCallback).
let job = new_promise_resolve_thenable_job(
Expand All @@ -1369,7 +1369,7 @@ impl Promise {
);

// 15. Perform HostEnqueuePromiseJob(job.[[Job]], job.[[Realm]]).
context.job_queue.enqueue_promise_job(job, context);
context.job_queue().enqueue_promise_job(job, context);

// 16. Return undefined.
Ok(JsValue::Undefined)
Expand Down Expand Up @@ -1525,7 +1525,7 @@ impl Promise {
// 7. If promise.[[PromiseIsHandled]] is false, perform HostPromiseRejectionTracker(promise, "reject").
if !handled {
context
.host_hooks
.host_hooks()
.promise_rejection_tracker(promise, OperationType::Reject, context);
}

Expand Down Expand Up @@ -1556,7 +1556,7 @@ impl Promise {
let job = new_promise_reaction_job(reaction, argument.clone());

// b. Perform HostEnqueuePromiseJob(job.[[Job]], job.[[Realm]]).
context.job_queue.enqueue_promise_job(job, context);
context.job_queue().enqueue_promise_job(job, context);
}

// 2. Return unused.
Expand Down Expand Up @@ -2011,7 +2011,7 @@ impl Promise {
.and_then(JsFunction::from_object)
// 4. Else,
// a. Let onFulfilledJobCallback be HostMakeJobCallback(onFulfilled).
.map(|f| context.host_hooks.make_job_callback(f, context));
.map(|f| context.host_hooks().make_job_callback(f, context));

// 5. If IsCallable(onRejected) is false, then
// a. Let onRejectedJobCallback be empty.
Expand All @@ -2021,7 +2021,7 @@ impl Promise {
.and_then(JsFunction::from_object)
// 6. Else,
// a. Let onRejectedJobCallback be HostMakeJobCallback(onRejected).
.map(|f| context.host_hooks.make_job_callback(f, context));
.map(|f| context.host_hooks().make_job_callback(f, context));

// 7. Let fulfillReaction be the PromiseReaction { [[Capability]]: resultCapability, [[Type]]: Fulfill, [[Handler]]: onFulfilledJobCallback }.
let fulfill_reaction = ReactionRecord {
Expand Down Expand Up @@ -2064,7 +2064,9 @@ impl Promise {
let fulfill_job = new_promise_reaction_job(fulfill_reaction, value.clone());

// c. Perform HostEnqueuePromiseJob(fulfillJob.[[Job]], fulfillJob.[[Realm]]).
context.job_queue.enqueue_promise_job(fulfill_job, context);
context
.job_queue()
.enqueue_promise_job(fulfill_job, context);
}

// 11. Else,
Expand All @@ -2073,7 +2075,7 @@ impl Promise {
PromiseState::Rejected(ref reason) => {
// c. If promise.[[PromiseIsHandled]] is false, perform HostPromiseRejectionTracker(promise, "handle").
if !handled {
context.host_hooks.promise_rejection_tracker(
context.host_hooks().promise_rejection_tracker(
promise,
OperationType::Handle,
context,
Expand All @@ -2084,7 +2086,7 @@ impl Promise {
let reject_job = new_promise_reaction_job(reject_reaction, reason.clone());

// e. Perform HostEnqueuePromiseJob(rejectJob.[[Job]], rejectJob.[[Realm]]).
context.job_queue.enqueue_promise_job(reject_job, context);
context.job_queue().enqueue_promise_job(reject_job, context);

// 12. Set promise.[[PromiseIsHandled]] to true.
promise
Expand Down Expand Up @@ -2196,7 +2198,7 @@ fn new_promise_reaction_job(mut reaction: ReactionRecord, argument: JsValue) ->
},
// e. Else, let handlerResult be Completion(HostCallJobCallback(handler, undefined, « argument »)).
Some(handler) => context
.host_hooks
.host_hooks()
.call_job_callback(handler, &JsValue::Undefined, &[argument.clone()], context)
.map_err(|e| e.to_opaque(context)),
};
Expand Down Expand Up @@ -2263,7 +2265,7 @@ fn new_promise_resolve_thenable_job(
let resolving_functions = Promise::create_resolving_functions(&promise_to_resolve, context);

// b. Let thenCallResult be Completion(HostCallJobCallback(then, thenable, « resolvingFunctions.[[Resolve]], resolvingFunctions.[[Reject]] »)).
let then_call_result = context.host_hooks.call_job_callback(
let then_call_result = context.host_hooks().call_job_callback(
then,
&thenable,
&[
Expand Down
82 changes: 39 additions & 43 deletions boa_engine/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ pub struct Context<'host> {
/// Intrinsic objects
intrinsics: Intrinsics,

/// ICU related utilities
#[cfg(feature = "intl")]
icu: icu::Icu<'host>,

/// Number of instructions remaining before a forced exit
#[cfg(feature = "fuzz")]
pub(crate) instructions_remaining: usize,
Expand All @@ -103,9 +99,13 @@ pub struct Context<'host> {

pub(crate) kept_alive: Vec<JsObject>,

pub(crate) host_hooks: &'host dyn HostHooks,
/// ICU related utilities
#[cfg(feature = "intl")]
icu: icu::Icu<'host>,

host_hooks: &'host dyn HostHooks,

pub(crate) job_queue: &'host dyn JobQueue,
job_queue: &'host dyn JobQueue,
}

impl std::fmt::Debug for Context<'_> {
Expand Down Expand Up @@ -472,9 +472,19 @@ impl Context<'_> {
}
}

#[cfg(feature = "intl")]
impl<'host> Context<'host> {
/// Get the host hooks.
pub(crate) fn host_hooks(&self) -> &'host dyn HostHooks {
self.host_hooks
}

/// Get the job queue.
pub(crate) fn job_queue(&mut self) -> &'host dyn JobQueue {
self.job_queue
}

/// Get the ICU related utilities
#[cfg(feature = "intl")]
pub(crate) const fn icu(&self) -> &icu::Icu<'host> {
&self.icu
}
Expand All @@ -491,6 +501,7 @@ impl<'host> Context<'host> {
feature = "intl",
doc = "The required data in a valid provider is specified in [`BoaProvider`]"
)]
#[derive(Default)]
pub struct ContextBuilder<'icu, 'hooks, 'queue> {
interner: Option<Interner>,
host_hooks: Option<&'hooks dyn HostHooks>,
Expand Down Expand Up @@ -520,29 +531,14 @@ impl std::fmt::Debug for ContextBuilder<'_, '_, '_> {
}
}

impl Default for ContextBuilder<'static, 'static, 'static> {
fn default() -> Self {
Self {
interner: Default::default(),
host_hooks: Default::default(),
job_queue: Default::default(),
icu: Default::default(),
#[cfg(feature = "fuzz")]
instructions_remaining: Default::default(),
}
}
}

impl ContextBuilder<'static, 'static, 'static> {
impl<'icu, 'hooks, 'queue> ContextBuilder<'icu, 'hooks, 'queue> {
/// Creates a new [`ContextBuilder`] with a default empty [`Interner`]
/// and a default [`BoaProvider`] if the `intl` feature is enabled.
#[must_use]
pub fn new() -> Self {
Self::default()
}
}

impl<'icu, 'hooks, 'queue> ContextBuilder<'icu, 'hooks, 'queue> {
/// Initializes the context [`Interner`] to the provided interner.
///
/// This is useful when you want to initialize an [`Interner`] with
Expand All @@ -554,26 +550,6 @@ impl<'icu, 'hooks, 'queue> ContextBuilder<'icu, 'hooks, 'queue> {
self
}

/// Initializes the [`Host Hooks`] for the context.
///
/// [`Host Hooks`]: https://tc39.es/ecma262/#sec-host-hooks-summary
#[must_use]
pub fn host_hooks(self, host_hooks: &dyn HostHooks) -> ContextBuilder<'icu, '_, 'queue> {
ContextBuilder {
host_hooks: Some(host_hooks),
..self
}
}

/// Initializes the [`JobQueue`] for the context.
#[must_use]
pub fn job_queue(self, job_queue: &dyn JobQueue) -> ContextBuilder<'icu, 'hooks, '_> {
ContextBuilder {
job_queue: Some(job_queue),
..self
}
}

/// Provides an icu data provider to the [`Context`].
///
/// This function is only available if the `intl` feature is enabled.
Expand All @@ -598,6 +574,26 @@ impl<'icu, 'hooks, 'queue> ContextBuilder<'icu, 'hooks, 'queue> {
})
}

/// Initializes the [`HostHooks`] for the context.
///
/// [`Host Hooks`]: https://tc39.es/ecma262/#sec-host-hooks-summary
#[must_use]
pub fn host_hooks(self, host_hooks: &dyn HostHooks) -> ContextBuilder<'icu, '_, 'queue> {
ContextBuilder {
host_hooks: Some(host_hooks),
..self
}
}

/// Initializes the [`JobQueue`] for the context.
#[must_use]
pub fn job_queue(self, job_queue: &dyn JobQueue) -> ContextBuilder<'icu, 'hooks, '_> {
ContextBuilder {
job_queue: Some(job_queue),
..self
}
}

/// Specifies the number of instructions remaining to the [`Context`].
///
/// This function is only available if the `fuzz` feature is enabled.
Expand Down

0 comments on commit 868705a

Please sign in to comment.