mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-24 02:31:13 +08:00
195 lines
7.0 KiB
Rust
195 lines
7.0 KiB
Rust
use std::sync::Arc;
|
|
|
|
use oxide_kernel::shape_forge::EMPTY_SHAPE_ID;
|
|
use oxide_types::mem::P;
|
|
use oxide_types::object::JsObject;
|
|
use oxide_types::value::JsValue;
|
|
|
|
use crate::coercion;
|
|
use crate::vm::Vm;
|
|
use oxide_runtime_api::NativeResult;
|
|
|
|
fn create_error_object(vm: &mut Vm, error_proto_ptr: *mut JsObject, name: &str, message: &str) -> JsValue {
|
|
let obj = vm
|
|
.epoch()
|
|
.alloc(JsObject::new_empty(EMPTY_SHAPE_ID, JsValue::from_js_object(error_proto_ptr)));
|
|
|
|
let sf = Arc::clone(vm.kernel_core().perm_interner());
|
|
let sh = Arc::clone(vm.kernel_core().shape_forge());
|
|
|
|
let si_message = sf.intern("message").0;
|
|
let sh_message = sh.make_shape(EMPTY_SHAPE_ID, si_message);
|
|
let msg_val = vm.new_string(message);
|
|
unsafe {
|
|
(*obj).set_shape_id(sh_message);
|
|
(*obj).push_prop(msg_val);
|
|
}
|
|
|
|
let si_name = sf.intern("name").0;
|
|
let sh_name = sh.make_shape(sh_message, si_name);
|
|
let name_val = vm.new_string(name);
|
|
unsafe {
|
|
(*obj).set_shape_id(sh_name);
|
|
(*obj).push_prop(name_val);
|
|
}
|
|
|
|
JsValue::from_js_object(obj)
|
|
}
|
|
|
|
pub fn create_kind_error(vm: &mut Vm, kind: &str, msg: &str) -> JsValue {
|
|
let proto_ptr = match kind {
|
|
"TypeError" => P::as_ptr(&vm.session().builtin_world().type_error_proto) as *mut JsObject,
|
|
"RangeError" => P::as_ptr(&vm.session().builtin_world().range_error_proto) as *mut JsObject,
|
|
"ReferenceError" => P::as_ptr(&vm.session().builtin_world().reference_error_proto) as *mut JsObject,
|
|
"SyntaxError" => P::as_ptr(&vm.session().builtin_world().syntax_error_proto) as *mut JsObject,
|
|
"URIError" => P::as_ptr(&vm.session().builtin_world().uri_error_proto) as *mut JsObject,
|
|
"EvalError" => P::as_ptr(&vm.session().builtin_world().eval_error_proto) as *mut JsObject,
|
|
_ => P::as_ptr(&vm.session().builtin_world().error_proto) as *mut JsObject,
|
|
};
|
|
create_error_object(vm, proto_ptr, kind, msg)
|
|
}
|
|
|
|
pub fn create_type_error(vm: &mut Vm, msg: &str) -> JsValue {
|
|
create_kind_error(vm, "TypeError", msg)
|
|
}
|
|
|
|
pub fn create_error(vm: &mut Vm, msg: &str) -> JsValue {
|
|
create_kind_error(vm, "Error", msg)
|
|
}
|
|
|
|
pub fn create_reference_error(vm: &mut Vm, msg: &str) -> JsValue {
|
|
create_kind_error(vm, "ReferenceError", msg)
|
|
}
|
|
|
|
pub fn create_range_error(vm: &mut Vm, msg: &str) -> JsValue {
|
|
create_kind_error(vm, "RangeError", msg)
|
|
}
|
|
|
|
pub fn create_syntax_error(vm: &mut Vm, msg: &str) -> JsValue {
|
|
create_kind_error(vm, "SyntaxError", msg)
|
|
}
|
|
|
|
pub fn create_uri_error(vm: &mut Vm, msg: &str) -> JsValue {
|
|
create_kind_error(vm, "URIError", msg)
|
|
}
|
|
|
|
fn get_msg(vm: &mut Vm, args: &[u8]) -> String {
|
|
if args.len() > 1 {
|
|
coercion::to_string(vm.reg(args[1]))
|
|
} else {
|
|
String::new()
|
|
}
|
|
}
|
|
|
|
pub fn error_constructor(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
|
let msg = get_msg(vm, args);
|
|
let proto_ptr = P::as_ptr(&vm.session().builtin_world().error_proto) as *mut JsObject;
|
|
NativeResult::Ok(create_error_object(vm, proto_ptr, "Error", &msg))
|
|
}
|
|
|
|
pub fn type_error_constructor(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
|
let msg = get_msg(vm, args);
|
|
let proto_ptr = P::as_ptr(&vm.session().builtin_world().type_error_proto) as *mut JsObject;
|
|
NativeResult::Ok(create_error_object(vm, proto_ptr, "TypeError", &msg))
|
|
}
|
|
|
|
pub fn reference_error_constructor(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
|
let msg = get_msg(vm, args);
|
|
let proto_ptr = P::as_ptr(&vm.session().builtin_world().reference_error_proto) as *mut JsObject;
|
|
NativeResult::Ok(create_error_object(vm, proto_ptr, "ReferenceError", &msg))
|
|
}
|
|
|
|
pub fn range_error_constructor(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
|
let msg = get_msg(vm, args);
|
|
let proto_ptr = P::as_ptr(&vm.session().builtin_world().range_error_proto) as *mut JsObject;
|
|
NativeResult::Ok(create_error_object(vm, proto_ptr, "RangeError", &msg))
|
|
}
|
|
|
|
pub fn syntax_error_constructor(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
|
let msg = get_msg(vm, args);
|
|
let proto_ptr = P::as_ptr(&vm.session().builtin_world().syntax_error_proto) as *mut JsObject;
|
|
NativeResult::Ok(create_error_object(vm, proto_ptr, "SyntaxError", &msg))
|
|
}
|
|
|
|
pub fn uri_error_constructor(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
|
let msg = get_msg(vm, args);
|
|
let proto_ptr = P::as_ptr(&vm.session().builtin_world().uri_error_proto) as *mut JsObject;
|
|
NativeResult::Ok(create_error_object(vm, proto_ptr, "URIError", &msg))
|
|
}
|
|
|
|
pub fn eval_error_constructor(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
|
let msg = get_msg(vm, args);
|
|
let proto_ptr = P::as_ptr(&vm.session().builtin_world().eval_error_proto) as *mut JsObject;
|
|
NativeResult::Ok(create_error_object(vm, proto_ptr, "EvalError", &msg))
|
|
}
|
|
|
|
pub fn error_to_string(vm: &mut Vm, args: &[u8]) -> NativeResult {
|
|
let this_val = vm.reg(args[0]);
|
|
let (name, msg) = if this_val.is_object() {
|
|
let obj = unsafe { &*this_val.as_js_object_ptr() };
|
|
let name_val = obj
|
|
.hash_props_vec()
|
|
.and_then(|v| v.first())
|
|
.copied()
|
|
.unwrap_or(JsValue::undefined());
|
|
let msg_val = obj
|
|
.hash_props_vec()
|
|
.and_then(|v| v.get(1))
|
|
.copied()
|
|
.unwrap_or(JsValue::undefined());
|
|
(
|
|
vm.lookup_str(name_val).unwrap_or_else(|| "Error".to_string()),
|
|
vm.lookup_str(msg_val).unwrap_or_default(),
|
|
)
|
|
} else {
|
|
("Error".to_string(), String::new())
|
|
};
|
|
let result = crate::vm::format_error_message(&name, &msg);
|
|
NativeResult::Ok(vm.new_string(&result))
|
|
}
|
|
|
|
pub fn error_stack_getter(vm: &mut Vm, _args: &[u8]) -> NativeResult {
|
|
let mut lines = vec![];
|
|
for frame in vm.frames.iter().rev() {
|
|
let func_name = vm
|
|
.kernel_core()
|
|
.perm_interner()
|
|
.lookup(frame.function_name)
|
|
.unwrap_or("<anonymous>");
|
|
lines.push(format!(" at {} (native)", func_name));
|
|
}
|
|
lines.push(" at <anonymous> (native)".to_string());
|
|
let result = lines.join("\n");
|
|
NativeResult::Ok(vm.new_string(&result))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::error_stack_getter;
|
|
use crate::vm::{CallFrame, FrameContinuation, Vm};
|
|
use oxide_types::value::JsValue;
|
|
|
|
#[test]
|
|
fn error_stack_uses_call_frame_function_name() {
|
|
let mut vm = Vm::new();
|
|
let name_si = vm.kernel_core().perm_interner().intern("foo").0;
|
|
vm.frames.push(CallFrame {
|
|
return_addr: 0,
|
|
function_name: name_si,
|
|
caller_reg_limit: 1,
|
|
saved_reg_offset: 0,
|
|
saved_this: JsValue::undefined(),
|
|
saved_new_target: JsValue::undefined(),
|
|
callee: JsValue::undefined(),
|
|
construct_result_reg: None,
|
|
constructed_this: None,
|
|
is_derived_constructor: false,
|
|
continuation: FrameContinuation::None,
|
|
});
|
|
|
|
let stack = error_stack_getter(&mut vm, &[0]).unwrap();
|
|
let stack_str = vm.lookup_str(stack).unwrap_or_default();
|
|
assert!(stack_str.contains("foo"), "expected function name in stack: {stack_str}");
|
|
}
|
|
}
|