refactor: 统一错误对象构造与异常类型传播

This commit is contained in:
2026-06-23 14:51:58 +08:00
parent 69500513b3
commit b1cd4661ed
11 changed files with 71 additions and 77 deletions
+20 -13
View File
@@ -36,34 +36,41 @@ fn create_error_object(vm: &mut Vm, error_proto_ptr: *mut JsObject, name: &str,
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 {
let proto_ptr = P::as_ptr(&vm.session().builtin_world().type_error_proto) as *mut JsObject;
create_error_object(vm, proto_ptr, "TypeError", msg)
create_kind_error(vm, "TypeError", msg)
}
pub fn create_error(vm: &mut Vm, msg: &str) -> JsValue {
let proto_ptr = P::as_ptr(&vm.session().builtin_world().error_proto) as *mut JsObject;
create_error_object(vm, proto_ptr, "Error", msg)
create_kind_error(vm, "Error", msg)
}
pub fn create_reference_error(vm: &mut Vm, msg: &str) -> JsValue {
let proto_ptr = P::as_ptr(&vm.session().builtin_world().reference_error_proto) as *mut JsObject;
create_error_object(vm, proto_ptr, "ReferenceError", msg)
create_kind_error(vm, "ReferenceError", msg)
}
pub fn create_range_error(vm: &mut Vm, msg: &str) -> JsValue {
let proto_ptr = P::as_ptr(&vm.session().builtin_world().range_error_proto) as *mut JsObject;
create_error_object(vm, proto_ptr, "RangeError", msg)
create_kind_error(vm, "RangeError", msg)
}
pub fn create_syntax_error(vm: &mut Vm, msg: &str) -> JsValue {
let proto_ptr = P::as_ptr(&vm.session().builtin_world().syntax_error_proto) as *mut JsObject;
create_error_object(vm, proto_ptr, "SyntaxError", msg)
create_kind_error(vm, "SyntaxError", msg)
}
pub fn create_uri_error(vm: &mut Vm, msg: &str) -> JsValue {
let proto_ptr = P::as_ptr(&vm.session().builtin_world().uri_error_proto) as *mut JsObject;
create_error_object(vm, proto_ptr, "URIError", msg)
create_kind_error(vm, "URIError", msg)
}
fn get_msg(vm: &mut Vm, args: &[u8]) -> String {
@@ -137,7 +144,7 @@ pub fn error_to_string(vm: &mut Vm, args: &[u8]) -> NativeResult {
} else {
("Error".to_string(), String::new())
};
let result = if msg.is_empty() { name } else { format!("{}: {}", name, msg) };
let result = crate::vm::format_error_message(&name, &msg);
let si = vm.kernel_core().string_forge().intern(&result).0;
NativeResult::Ok(JsValue::string(si, 0))
}
+1 -1
View File
@@ -105,7 +105,7 @@ pub fn object_assign(vm: &mut Vm, args: &[u8]) -> NativeResult {
let target_val = vm.reg(args[1]);
let target_val = match coercion::to_object(target_val, vm) {
Ok(val) => val,
Err(msg) => return NativeResult::Err(crate::builtins::error::create_type_error(vm, msg)),
Err(msg) => return NativeResult::Err(crate::builtins::error::create_type_error(vm, &msg)),
};
let target_ptr = target_val.as_js_object_ptr();
if target_ptr.is_null() {
+14 -24
View File
@@ -6,28 +6,22 @@ use crate::coercion;
use crate::native::NativeResult;
use crate::vm::Vm;
fn get_regexp_ptr(vm: &Vm, args: &[u8]) -> Result<*mut JsObject, JsValue> {
fn get_regexp_ptr(vm: &mut Vm, args: &[u8]) -> Result<*mut JsObject, JsValue> {
let this_val = vm.reg(args[0]);
if !this_val.is_object() {
return Err(JsValue::string(
vm.kernel_core()
.string_forge()
.intern("TypeError: RegExp.prototype method called on non-object")
.0,
0,
return Err(crate::builtins::error::create_type_error(
vm,
"RegExp.prototype method called on non-object",
));
}
let ptr = this_val.as_js_object_ptr();
if ptr.is_null() {
return Err(JsValue::string(vm.kernel_core().string_forge().intern("TypeError: null object").0, 0));
return Err(crate::builtins::error::create_type_error(vm, "null object"));
}
if !unsafe { &*ptr }.is_regexp_obj() {
return Err(JsValue::string(
vm.kernel_core()
.string_forge()
.intern("TypeError: RegExp.prototype method called on non-RegExp object")
.0,
0,
return Err(crate::builtins::error::create_type_error(
vm,
"RegExp.prototype method called on non-RegExp object",
));
}
Ok(ptr)
@@ -107,8 +101,10 @@ pub fn regexp_constructor(vm: &mut Vm, args: &[u8]) -> NativeResult {
obj.set_native_fn(Some(unsafe { NativeFnPtr::from_raw(re_ptr as *const ()) }));
}
Err(e) => {
let msg = format!("SyntaxError: Invalid regular expression: {}", e);
return NativeResult::Err(JsValue::string(vm.kernel_core().string_forge().intern(&msg).0, 0));
return NativeResult::Err(crate::builtins::error::create_syntax_error(
vm,
&format!("Invalid regular expression: {e}"),
));
}
}
@@ -152,10 +148,7 @@ pub fn regexp_test(vm: &mut Vm, args: &[u8]) -> NativeResult {
let fn_ptr = match re.native_fn() {
None => {
return NativeResult::Err(JsValue::string(
vm.kernel_core().string_forge().intern("TypeError: invalid RegExp").0,
0,
));
return NativeResult::Err(crate::builtins::error::create_type_error(vm, "invalid RegExp"));
}
Some(p) => p,
};
@@ -187,10 +180,7 @@ pub fn regexp_exec(vm: &mut Vm, args: &[u8]) -> NativeResult {
let re = unsafe { &*re_ptr };
match re.native_fn() {
None => {
return NativeResult::Err(JsValue::string(
vm.kernel_core().string_forge().intern("TypeError: invalid RegExp").0,
0,
));
return NativeResult::Err(crate::builtins::error::create_type_error(vm, "invalid RegExp"));
}
Some(p) => p,
}
+1 -5
View File
@@ -1,12 +1,8 @@
use oxide_types::value::JsValue;
use crate::native::NativeResult;
use crate::vm::Vm;
fn stub_error(vm: &mut Vm, name: &str) -> NativeResult {
let msg = format!("TypeError: {} is not implemented", name);
let si = vm.kernel_core().string_forge().intern(&msg).0;
NativeResult::Err(JsValue::string(si, 0))
NativeResult::Err(crate::builtins::error::create_type_error(vm, &format!("{name} is not implemented")))
}
// Architecturally deferred — see 13.5-CONTEXT.md for rationale
+6 -12
View File
@@ -17,12 +17,9 @@ pub fn symbol_constructor(vm: &mut Vm, args: &[u8]) -> NativeResult {
if !proto_ptr.is_null() {
let sp = vm.session().builtin_world().symbol_proto.as_ptr() as *mut JsObject;
if std::ptr::eq(proto_ptr, sp) {
return NativeResult::Err(JsValue::string(
vm.kernel_core()
.string_forge()
.intern("TypeError: Symbol is not a constructor")
.0,
0,
return NativeResult::Err(crate::builtins::error::create_type_error(
vm,
"Symbol is not a constructor",
));
}
}
@@ -45,12 +42,9 @@ pub fn symbol_constructor(vm: &mut Vm, args: &[u8]) -> NativeResult {
pub fn symbol_to_string(vm: &mut Vm, args: &[u8]) -> NativeResult {
let this_val = vm.reg(args[0]);
if !this_val.is_symbol() {
return NativeResult::Err(JsValue::string(
vm.kernel_core()
.string_forge()
.intern("TypeError: Symbol.prototype.toString requires a Symbol")
.0,
0,
return NativeResult::Err(crate::builtins::error::create_type_error(
vm,
"Symbol.prototype.toString requires a Symbol",
));
}