use crate::{
builtins::{iterable::iterable_to_list, Array, BuiltIn, JsArgs},
context::intrinsics::StandardConstructors,
object::{
internal_methods::get_prototype_from_constructor, ConstructorBuilder, JsObject, ObjectData,
},
property::{Attribute, PropertyDescriptorBuilder},
Context, JsResult, JsValue,
};
use boa_profiler::Profiler;
use tap::{Conv, Pipe};
use super::Error;
#[derive(Debug, Clone, Copy)]
pub(crate) struct AggregateError;
impl BuiltIn for AggregateError {
const NAME: &'static str = "AggregateError";
fn init(context: &mut Context) -> Option<JsValue> {
let _timer = Profiler::global().start_event(Self::NAME, "init");
let error_constructor = context.intrinsics().constructors().error().constructor();
let error_prototype = context.intrinsics().constructors().error().prototype();
let attribute = Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE;
ConstructorBuilder::with_standard_constructor(
context,
Self::constructor,
context
.intrinsics()
.constructors()
.aggregate_error()
.clone(),
)
.name(Self::NAME)
.length(Self::LENGTH)
.inherit(error_prototype)
.custom_prototype(error_constructor)
.property("name", Self::NAME, attribute)
.property("message", "", attribute)
.build()
.conv::<JsValue>()
.pipe(Some)
}
}
impl AggregateError {
pub(crate) const LENGTH: usize = 2;
pub(crate) fn constructor(
new_target: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
let prototype = get_prototype_from_constructor(
new_target,
StandardConstructors::aggregate_error,
context,
)?;
let o = JsObject::from_proto_and_data(prototype, ObjectData::error());
let message = args.get_or_undefined(1);
if !message.is_undefined() {
let msg = message.to_string(context)?;
o.create_non_enumerable_data_property_or_throw("message", msg, context);
}
Error::install_error_cause(&o, args.get_or_undefined(2), context)?;
let errors = args.get_or_undefined(0);
let errors_list = iterable_to_list(context, errors, None)?;
o.define_property_or_throw(
"errors",
PropertyDescriptorBuilder::new()
.configurable(true)
.enumerable(false)
.writable(true)
.value(Array::create_array_from_list(errors_list, context))
.build(),
context,
)
.expect("should not fail according to spec");
Ok(o.into())
}
}