fix: defineProperty 纯属性描述符按 ES 规范默认创建 value=undefined,不再错误抛 TypeError

This commit is contained in:
2026-06-27 17:23:50 +08:00
parent 9c4a169c33
commit 749a1c7aec
2 changed files with 11 additions and 8 deletions
+7 -4
View File
@@ -287,10 +287,13 @@ pub fn object_define_property<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResul
} }
} else { } else {
if existing_pos.is_none() { if existing_pos.is_none() {
return NativeResult::Err(crate::error::create_type_error( if vm
vm, .define_data_property(obj, si, JsValue::undefined(), PropAttributes::new(false, enumerable, configurable))
"Cannot define new property without value or accessor", .is_err()
)); {
return NativeResult::Err(crate::error::create_type_error(vm, "Cannot define property"));
}
return NativeResult::Ok(obj_val);
} }
let is_accessor = existing_meta.map(|m| m.is_accessor).unwrap_or(false); let is_accessor = existing_meta.map(|m| m.is_accessor).unwrap_or(false);
let writable = existing_meta.map(|m| m.attributes.writable()).unwrap_or(true); let writable = existing_meta.map(|m| m.attributes.writable()).unwrap_or(true);
@@ -92,11 +92,11 @@ fn get_own_property_descriptor_accessor_fields() {
); );
} }
// -- New property without value or accessor throws -- // -- New property without value or accessor defaults to data descriptor (ES spec behavior) --
#[test] #[test]
fn define_property_new_without_value_throws() { fn define_property_new_attributes_only_defaults_value_undefined() {
let result = eval_str("var obj={}; Object.defineProperty(obj,'x',{enumerable:true})"); let r = eval_str("var obj={}; Object.defineProperty(obj,'x',{enumerable:true}); obj.x").unwrap();
assert_err_contains(result, "Cannot define new property without value or accessor"); assert!(r.is_undefined(), "new prop with only enumerable should default value to undefined, got {:?}", r);
} }
// -- Partial configurable on existing property preserves value -- // -- Partial configurable on existing property preserves value --