use crate::builtins::arg;
use crate::interp::{Interp, JsResult};
use crate::object::{JsObject, Property};
use crate::value::Value;
use js_gc::Gc;
const CLASSES: [&str; 8] = [
"Error",
"TypeError",
"RangeError",
"ReferenceError",
"SyntaxError",
"EvalError",
"URIError",
"InternalError",
];
pub fn install(interp: &mut Interp<'_>) {
let object_proto = interp.realm.object_proto.clone();
let error_proto = interp.new_object(Some(object_proto));
error_proto.define(
"name",
Property::with_attributes(interp.string("Error"), true, false),
);
error_proto.define(
"message",
Property::with_attributes(interp.string(""), true, false),
);
crate::builtins::define_method(&error_proto, "toString", 0, to_string, interp);
interp.realm.error_proto = error_proto.clone();
for &class in &CLASSES {
let proto = if class == "Error" {
error_proto.clone()
} else {
let proto = interp.new_object(Some(error_proto.clone()));
proto.define(
"name",
Property::with_attributes(interp.string(class), true, false),
);
proto.define(
"message",
Property::with_attributes(interp.string(""), true, false),
);
proto
};
let constructor = interp.new_native_constructor(class, 1, error_constructor);
let class_text = interp.string(class);
interp.internal_set(&constructor, "_class", class_text);
constructor.define(
"prototype",
Property::with_attributes(Value::Object(proto.clone()), false, false),
);
proto.define(
"constructor",
Property::with_attributes(Value::Object(constructor.clone()), true, false),
);
interp
.realm
.errors
.insert(leak_class(class), constructor.clone());
let global = interp.realm.global.clone();
global.define(
class,
Property::with_attributes(Value::Object(constructor), true, false),
);
}
install_suppressed_error(interp, &error_proto);
}
fn install_suppressed_error(interp: &mut Interp<'_>, error_proto: &Gc<JsObject>) {
let proto = interp.new_object(Some(error_proto.clone()));
proto.define(
"name",
Property::with_attributes(interp.string("SuppressedError"), true, false),
);
proto.define(
"message",
Property::with_attributes(interp.string(""), true, false),
);
let constructor =
interp.new_native_constructor("SuppressedError", 3, suppressed_error_constructor);
constructor.define(
"prototype",
Property::with_attributes(Value::Object(proto.clone()), false, false),
);
proto.define(
"constructor",
Property::with_attributes(Value::Object(constructor.clone()), true, false),
);
interp.realm.suppressed_error_proto = Some(proto);
interp
.realm
.errors
.insert("SuppressedError", constructor.clone());
let global = interp.realm.global.clone();
global.define(
"SuppressedError",
Property::with_attributes(Value::Object(constructor), true, false),
);
}
fn suppressed_error_constructor(
interp: &mut Interp<'_>,
this: Value,
args: &[Value],
) -> JsResult<Value> {
let error = arg(args, 0);
let suppressed = arg(args, 1);
let message = arg(args, 2);
let object = match &this {
Value::Object(object) => object.clone(),
_ => {
let proto = interp
.realm
.suppressed_error_proto
.clone()
.unwrap_or_else(|| interp.realm.error_proto.clone());
interp.new_object(Some(proto))
}
};
object.define("error", Property::with_attributes(error, true, true));
object.define(
"suppressed",
Property::with_attributes(suppressed, true, true),
);
if !matches!(message, Value::Undefined) {
let text = interp.to_js_string(&message)?;
object.define(
"message",
Property::with_attributes(interp.string(text), true, true),
);
}
interp.internal_set(&object, "_class", interp.string("Error"));
interp.add_stack(&object);
Ok(Value::Object(object))
}
fn leak_class(class: &str) -> &'static str {
CLASSES
.into_iter()
.find(|known| *known == class)
.unwrap_or("Error")
}
fn error_constructor(interp: &mut Interp<'_>, this: Value, args: &[Value]) -> JsResult<Value> {
let class = interp
.current_callee()
.and_then(|callee| interp.internal_get(&callee, "_class"))
.map(|value| value.to_js_string())
.unwrap_or_else(|| "Error".to_string());
let target = match &this {
Value::Object(object) => object.clone(),
_ => {
let proto = interp
.realm
.errors
.get(class.as_str())
.map(|constructor| interp.prototype_of(constructor))
.unwrap_or_else(|| interp.realm.error_proto.clone());
interp.new_object(Some(proto))
}
};
if let Some(Value::String(_)) = args.first() {
target.define(
"message",
Property::with_attributes(arg(args, 0), true, false),
);
}
if class != "Error" && !target.has_own("name") {
target.define(
"name",
Property::with_attributes(interp.string(class.clone()), true, false),
);
}
if interp.internal_get(&target, "_class").is_none() {
interp.internal_set(&target, "_class", interp.string("Error"));
}
interp.add_stack_skipping(&target, 1);
Ok(Value::Object(target))
}
pub fn to_string(interp: &mut Interp<'_>, this: Value, _args: &[Value]) -> JsResult<Value> {
let Value::Object(object) = &this else {
return Err(interp.type_error("Error.prototype.toString called on a non-object"));
};
let name = interp
.get_property(object, "name")?
.filter(|value| !matches!(value, Value::Undefined))
.map_or_else(|| "Error".to_string(), |value| value.to_js_string());
let message = interp
.get_property(object, "message")?
.filter(|value| !matches!(value, Value::Undefined))
.map_or_else(String::new, |value| value.to_js_string());
let text = match (name.is_empty(), message.is_empty()) {
(true, true) => String::new(),
(true, false) => message,
(false, true) => name,
(false, false) => format!("{name}: {message}"),
};
Ok(interp.string(text))
}