use std::cell::RefCell;
use std::rc::Rc;
use crate::RuntimeError;
use crate::vm::object::{JSObject, OrdinaryObject};
use crate::vm::property::PropertyKey;
use crate::vm::value::Value;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ErrorType {
Error,
TypeError,
ReferenceError,
RangeError,
SyntaxError,
URIError,
EvalError,
}
impl ErrorType {
pub fn name(&self) -> &'static str {
match self {
ErrorType::Error => "Error",
ErrorType::TypeError => "TypeError",
ErrorType::ReferenceError => "ReferenceError",
ErrorType::RangeError => "RangeError",
ErrorType::SyntaxError => "SyntaxError",
ErrorType::URIError => "URIError",
ErrorType::EvalError => "EvalError",
}
}
}
fn options_cause(options: Option<&Value>) -> Option<crate::vm::property::PropertyDescriptor> {
let Value::Object(obj_ref) = options? else {
return None;
};
obj_ref
.borrow()
.property_get(&PropertyKey::from_str("cause"))
.map(|d| error_message_descriptor(d.value))
}
fn error_message_descriptor(value: Value) -> crate::vm::property::PropertyDescriptor {
crate::vm::property::PropertyDescriptor {
value,
writable: true,
enumerable: false,
configurable: true,
getter: None,
setter: None,
}
}
pub fn create_error_object(
message: Option<String>,
prototype: Rc<RefCell<dyn JSObject>>,
_name: &str,
) -> Value {
let mut obj = OrdinaryObject::with_class_name("Error");
obj.set_prototype(Some(Rc::clone(&prototype)));
if let Some(msg) = message {
obj.define_property(
PropertyKey::from_str("message"),
error_message_descriptor(Value::string(&msg)),
)
.ok();
}
Value::Object(Rc::new(RefCell::new(obj)))
}
pub fn error_constructor(error_type: ErrorType, args: &[Value]) -> Result<Value, RuntimeError> {
let message = match args.first() {
None | Some(Value::Undefined) => None,
Some(v) => Some(v.to_js_string()),
};
let mut obj = OrdinaryObject::with_class_name("Error");
if let Some(proto) = crate::builtins::wrapper_prototype(error_type.name()) {
obj.set_prototype(Some(proto));
}
if let Some(msg) = message {
obj.define_property(
PropertyKey::from_str("message"),
error_message_descriptor(Value::string(&msg)),
)
.ok();
}
if let Some(cause) = options_cause(args.get(1)) {
obj.define_property(PropertyKey::from_str("cause"), cause).ok();
}
Ok(Value::Object(Rc::new(RefCell::new(obj))))
}
pub fn runtime_error_to_js_error(
err: &RuntimeError,
builtins: &crate::builtins::Builtins,
) -> Value {
match err {
RuntimeError::TypeError(msg) => create_error_object(
Some(msg.clone()),
Rc::clone(&builtins.type_error_prototype),
"TypeError",
),
RuntimeError::ReferenceError(msg) => create_error_object(
Some(msg.clone()),
Rc::clone(&builtins.reference_error_prototype),
"ReferenceError",
),
RuntimeError::RangeError(msg) => create_error_object(
Some(msg.clone()),
Rc::clone(&builtins.range_error_prototype),
"RangeError",
),
RuntimeError::SyntaxError(msg) => create_error_object(
Some(msg.clone()),
Rc::clone(&builtins.syntax_error_prototype),
"SyntaxError",
),
RuntimeError::InternalError(msg) => create_error_object(
Some(msg.clone()),
Rc::clone(&builtins.error_prototype),
"Error",
),
RuntimeError::NotImplemented(msg) => create_error_object(
Some(format!("Not implemented: {}", msg)),
Rc::clone(&builtins.error_prototype),
"Error",
),
RuntimeError::Thrown(val) => val.clone(),
}
}
pub fn error_prototype_to_string(obj: &Value) -> Result<Value, RuntimeError> {
let Value::Object(obj_ref) = obj else {
return Err(RuntimeError::TypeError(
"Error.prototype.toString requires that 'this' be an Object".to_string(),
));
};
let get = |prop: &str| -> Option<Value> {
crate::vm::prototype::find_descriptor(Rc::clone(obj_ref), &PropertyKey::from_str(prop))
.ok()
.flatten()
.map(|(_, desc)| desc.value)
};
let name = match get("name") {
None | Some(Value::Undefined) => "Error".to_string(),
Some(v) => v.to_js_string(),
};
let message = match get("message") {
None | Some(Value::Undefined) => String::new(),
Some(v) => v.to_js_string(),
};
let result = if name.is_empty() {
message
} else if message.is_empty() {
name
} else {
format!("{name}: {message}")
};
Ok(Value::string(&result))
}
pub fn setup_error_prototype(
proto: &Rc<RefCell<dyn JSObject>>,
_builtins: &crate::builtins::Builtins,
) {
use crate::builtins::set_prototype_method;
use crate::vm::property::PropertyDescriptor;
let writable_string = |value: Value| PropertyDescriptor {
value,
writable: true,
enumerable: false,
configurable: true,
getter: None,
setter: None,
};
let _ = proto
.borrow_mut()
.define_property(PropertyKey::from_str("name"), writable_string(Value::string("Error")));
let _ = proto
.borrow_mut()
.define_property(PropertyKey::from_str("message"), writable_string(Value::string("")));
set_prototype_method(proto, "toString", |this, _args| {
error_prototype_to_string(this)
});
}
pub fn setup_native_error_prototype(proto: &Rc<RefCell<dyn JSObject>>, name: &str) {
use crate::vm::property::PropertyDescriptor;
let writable = |value: Value| PropertyDescriptor {
value,
writable: true,
enumerable: false,
configurable: true,
getter: None,
setter: None,
};
let _ = proto
.borrow_mut()
.define_property(PropertyKey::from_str("name"), writable(Value::string(name)));
let _ = proto
.borrow_mut()
.define_property(PropertyKey::from_str("message"), writable(Value::string("")));
}