fix: defineProperty 纯属性描述符按 ES 规范默认创建 value=undefined,不再错误抛 TypeError
This commit is contained in:
@@ -287,10 +287,13 @@ pub fn object_define_property<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResul
|
||||
}
|
||||
} else {
|
||||
if existing_pos.is_none() {
|
||||
return NativeResult::Err(crate::error::create_type_error(
|
||||
vm,
|
||||
"Cannot define new property without value or accessor",
|
||||
));
|
||||
if vm
|
||||
.define_data_property(obj, si, JsValue::undefined(), PropAttributes::new(false, enumerable, configurable))
|
||||
.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 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]
|
||||
fn define_property_new_without_value_throws() {
|
||||
let result = eval_str("var obj={}; Object.defineProperty(obj,'x',{enumerable:true})");
|
||||
assert_err_contains(result, "Cannot define new property without value or accessor");
|
||||
fn define_property_new_attributes_only_defaults_value_undefined() {
|
||||
let r = eval_str("var obj={}; Object.defineProperty(obj,'x',{enumerable:true}); obj.x").unwrap();
|
||||
assert!(r.is_undefined(), "new prop with only enumerable should default value to undefined, got {:?}", r);
|
||||
}
|
||||
|
||||
// -- Partial configurable on existing property preserves value --
|
||||
|
||||
Reference in New Issue
Block a user