#![allow(clippy::inline_always)]
use crate::{Context, JsError, JsResult, JsValue};
use boa_gc::{Finalize, Trace, custom_trace};
use std::ops::ControlFlow;
#[derive(Debug, Clone, Finalize)]
pub(crate) enum CompletionRecord {
Normal(JsValue),
Return(JsValue),
Throw(JsError),
}
unsafe impl Trace for CompletionRecord {
custom_trace!(this, mark, {
match this {
Self::Normal(v) => mark(v),
Self::Return(r) => mark(r),
Self::Throw(th) => mark(th),
}
});
}
impl CompletionRecord {
pub(crate) const fn is_throw_completion(&self) -> bool {
matches!(self, Self::Throw(_))
}
#[allow(clippy::missing_const_for_fn)]
pub(crate) fn consume(self) -> JsResult<JsValue> {
match self {
Self::Throw(error) => Err(error),
Self::Normal(value) | Self::Return(value) => Ok(value),
}
}
}
pub(crate) trait IntoCompletionRecord {
fn into_completion_record(self, context: &mut Context) -> ControlFlow<CompletionRecord>;
}
impl IntoCompletionRecord for () {
#[inline(always)]
fn into_completion_record(self, _: &mut Context) -> ControlFlow<CompletionRecord> {
ControlFlow::Continue(())
}
}
impl IntoCompletionRecord for JsError {
#[inline(always)]
fn into_completion_record(self, context: &mut Context) -> ControlFlow<CompletionRecord> {
context.handle_error(self)
}
}
impl IntoCompletionRecord for JsResult<()> {
#[inline(always)]
fn into_completion_record(self, context: &mut Context) -> ControlFlow<CompletionRecord> {
match self {
Ok(()) => ControlFlow::Continue(()),
Err(err) => context.handle_error(err),
}
}
}
impl IntoCompletionRecord for ControlFlow<CompletionRecord> {
#[inline(always)]
fn into_completion_record(self, _: &mut Context) -> ControlFlow<CompletionRecord> {
self
}
}