use crate::{
builtins::function::{make_builtin_fn, make_constructor_fn},
object::ObjectData,
profiler::BoaProfiler,
Context, Result, Value,
};
pub(crate) mod range;
pub(crate) mod reference;
pub(crate) mod syntax;
pub(crate) mod r#type;
pub(crate) use self::r#type::TypeError;
pub(crate) use self::range::RangeError;
pub(crate) use self::reference::ReferenceError;
pub(crate) use self::syntax::SyntaxError;
#[derive(Debug, Clone, Copy)]
pub(crate) struct Error;
impl Error {
pub(crate) const NAME: &'static str = "Error";
pub(crate) const LENGTH: usize = 1;
pub(crate) fn make_error(this: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> {
if let Some(message) = args.get(0) {
this.set_field("message", message.to_string(ctx)?);
}
this.set_data(ObjectData::Error);
Err(this.clone())
}
#[allow(clippy::wrong_self_convention)]
pub(crate) fn to_string(this: &Value, _: &[Value], _: &mut Context) -> Result<Value> {
let name = this.get_field("name");
let message = this.get_field("message");
Ok(Value::from(format!(
"{}: {}",
name.display(),
message.display()
)))
}
#[inline]
pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) {
let global = interpreter.global_object();
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");
let prototype = Value::new_object(Some(global));
prototype.set_field("name", Self::NAME);
prototype.set_field("message", "");
make_builtin_fn(Self::to_string, "toString", &prototype, 0, interpreter);
let error_object = make_constructor_fn(
Self::NAME,
Self::LENGTH,
Self::make_error,
global,
prototype,
true,
true,
);
(Self::NAME, error_object)
}
}