use crate::runner::ds::error::JErrorType;
use crate::runner::ds::value::JsValue;
use crate::runner::plugin::registry::BuiltInRegistry;
use crate::runner::plugin::types::{BuiltInObject, EvalContext};
pub fn register(registry: &mut BuiltInRegistry) {
let error = BuiltInObject::new("Error")
.with_constructor(error_constructor)
.add_method("toString", error_to_string)
.add_method("message", error_message);
registry.register_object(error);
let type_error = BuiltInObject::new("TypeError")
.with_prototype("Error")
.with_constructor(type_error_constructor)
.add_method("toString", error_to_string);
registry.register_object(type_error);
let reference_error = BuiltInObject::new("ReferenceError")
.with_prototype("Error")
.with_constructor(reference_error_constructor)
.add_method("toString", error_to_string);
registry.register_object(reference_error);
let syntax_error = BuiltInObject::new("SyntaxError")
.with_prototype("Error")
.with_constructor(syntax_error_constructor)
.add_method("toString", error_to_string);
registry.register_object(syntax_error);
let range_error = BuiltInObject::new("RangeError")
.with_prototype("Error")
.with_constructor(range_error_constructor)
.add_method("toString", error_to_string);
registry.register_object(range_error);
let eval_error = BuiltInObject::new("EvalError")
.with_prototype("Error")
.with_constructor(eval_error_constructor)
.add_method("toString", error_to_string);
registry.register_object(eval_error);
let uri_error = BuiltInObject::new("URIError")
.with_prototype("Error")
.with_constructor(uri_error_constructor)
.add_method("toString", error_to_string);
registry.register_object(uri_error);
}
fn get_message(args: &[JsValue]) -> String {
if args.is_empty() {
String::new()
} else {
match &args[0] {
JsValue::String(s) => s.clone(),
JsValue::Undefined => String::new(),
v => v.to_string(),
}
}
}
fn error_constructor(
_ctx: &mut EvalContext,
_this: JsValue,
args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
let message = get_message(&args);
Ok(JsValue::String(format!("Error: {}", message)))
}
fn type_error_constructor(
_ctx: &mut EvalContext,
_this: JsValue,
args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
let message = get_message(&args);
Ok(JsValue::String(format!("TypeError: {}", message)))
}
fn reference_error_constructor(
_ctx: &mut EvalContext,
_this: JsValue,
args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
let message = get_message(&args);
Ok(JsValue::String(format!("ReferenceError: {}", message)))
}
fn syntax_error_constructor(
_ctx: &mut EvalContext,
_this: JsValue,
args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
let message = get_message(&args);
Ok(JsValue::String(format!("SyntaxError: {}", message)))
}
fn range_error_constructor(
_ctx: &mut EvalContext,
_this: JsValue,
args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
let message = get_message(&args);
Ok(JsValue::String(format!("RangeError: {}", message)))
}
fn eval_error_constructor(
_ctx: &mut EvalContext,
_this: JsValue,
args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
let message = get_message(&args);
Ok(JsValue::String(format!("EvalError: {}", message)))
}
fn uri_error_constructor(
_ctx: &mut EvalContext,
_this: JsValue,
args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
let message = get_message(&args);
Ok(JsValue::String(format!("URIError: {}", message)))
}
fn error_to_string(
_ctx: &mut EvalContext,
this: JsValue,
_args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
match this {
JsValue::String(s) => Ok(JsValue::String(s)),
JsValue::Object(_) => {
Ok(JsValue::String("Error".to_string()))
}
_ => Ok(JsValue::String("Error".to_string())),
}
}
fn error_message(
_ctx: &mut EvalContext,
this: JsValue,
_args: Vec<JsValue>,
) -> Result<JsValue, JErrorType> {
match this {
JsValue::String(s) => {
if let Some(idx) = s.find(": ") {
Ok(JsValue::String(s[idx + 2..].to_string()))
} else {
Ok(JsValue::String(String::new()))
}
}
_ => Ok(JsValue::String(String::new())),
}
}