1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:57:45 +00:00

LibJS: Add and use the CreateNonEnumerableDataPropertyOrThrow AO

This commit is contained in:
Idan Horowitz 2021-07-06 00:13:19 +03:00 committed by Linus Groh
parent 6da7f43580
commit 6787e86a3a
5 changed files with 67 additions and 59 deletions

View file

@ -46,13 +46,11 @@ Value AggregateErrorConstructor::construct(FunctionObject& new_target)
if (vm.exception()) if (vm.exception())
return {}; return {};
u8 attr = Attribute::Writable | Attribute::Configurable;
if (!vm.argument(1).is_undefined()) { if (!vm.argument(1).is_undefined()) {
auto message = vm.argument(1).to_string(global_object); auto message = vm.argument(1).to_string(global_object);
if (vm.exception()) if (vm.exception())
return {}; return {};
aggregate_error->define_property(vm.names.message, js_string(vm, message), attr); aggregate_error->create_non_enumerable_data_property_or_throw(vm.names.message, js_string(vm, message));
} }
aggregate_error->install_error_cause(vm.argument(2)); aggregate_error->install_error_cause(vm.argument(2));
@ -63,7 +61,7 @@ Value AggregateErrorConstructor::construct(FunctionObject& new_target)
if (vm.exception()) if (vm.exception())
return {}; return {};
aggregate_error->define_property(vm.names.errors, Array::create_from(global_object, errors_list), attr); aggregate_error->define_property_or_throw(vm.names.errors, { .value = Array::create_from(global_object, errors_list), .writable = true, .enumerable = false, .configurable = true });
return aggregate_error; return aggregate_error;
} }

View file

@ -41,7 +41,7 @@ void Error::install_error_cause(Value options)
auto cause = options_object.get(vm.names.cause); auto cause = options_object.get(vm.names.cause);
if (vm.exception()) if (vm.exception())
return; return;
define_property(vm.names.cause, cause, Attribute::Writable | Attribute::Configurable); create_non_enumerable_data_property_or_throw(vm.names.cause, cause);
} }
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \ #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \

View file

@ -43,13 +43,11 @@ Value ErrorConstructor::construct(FunctionObject& new_target)
if (vm.exception()) if (vm.exception())
return {}; return {};
u8 attr = Attribute::Writable | Attribute::Configurable;
if (!vm.argument(0).is_undefined()) { if (!vm.argument(0).is_undefined()) {
auto message = vm.argument(0).to_string(global_object); auto message = vm.argument(0).to_string(global_object);
if (vm.exception()) if (vm.exception())
return {}; return {};
error->define_property(vm.names.message, js_string(vm, message), attr); error->create_non_enumerable_data_property_or_throw(vm.names.message, js_string(vm, message));
} }
error->install_error_cause(vm.argument(1)); error->install_error_cause(vm.argument(1));
@ -96,13 +94,11 @@ Value ErrorConstructor::construct(FunctionObject& new_target)
if (vm.exception()) \ if (vm.exception()) \
return {}; \ return {}; \
\ \
u8 attr = Attribute::Writable | Attribute::Configurable; \
\
if (!vm.argument(0).is_undefined()) { \ if (!vm.argument(0).is_undefined()) { \
auto message = vm.argument(0).to_string(global_object); \ auto message = vm.argument(0).to_string(global_object); \
if (vm.exception()) \ if (vm.exception()) \
return {}; \ return {}; \
error->define_property(vm.names.message, js_string(vm, message), attr); \ error->create_non_enumerable_data_property_or_throw(vm.names.message, js_string(vm, message)); \
} \ } \
\ \
error->install_error_cause(vm.argument(1)); \ error->install_error_cause(vm.argument(1)); \

View file

@ -190,6 +190,19 @@ bool Object::create_data_property_or_throw(PropertyName const& property_name, Va
return success; return success;
} }
// 7.3.6 CreateNonEnumerableDataPropertyOrThrow ( O, P, V ), https://tc39.es/proposal-error-cause/#sec-createnonenumerabledatapropertyorthrow
bool Object::create_non_enumerable_data_property_or_throw(PropertyName const& property_name, Value value)
{
VERIFY(!value.is_empty());
VERIFY(property_name.is_valid());
// 1. Let newDesc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }.
auto new_description = PropertyDescriptor { .value = value, .writable = true, .enumerable = false, .configurable = true };
// 2. Return ? DefinePropertyOrThrow(O, P, newDesc).
return define_property_or_throw(property_name, new_description);
}
// 7.3.8 DefinePropertyOrThrow ( O, P, desc ), https://tc39.es/ecma262/#sec-definepropertyorthrow // 7.3.8 DefinePropertyOrThrow ( O, P, desc ), https://tc39.es/ecma262/#sec-definepropertyorthrow
bool Object::define_property_or_throw(PropertyName const& property_name, PropertyDescriptor const& property_descriptor) bool Object::define_property_or_throw(PropertyName const& property_name, PropertyDescriptor const& property_descriptor)
{ {

View file

@ -78,6 +78,7 @@ public:
bool create_data_property(PropertyName const&, Value); bool create_data_property(PropertyName const&, Value);
bool create_method_property(PropertyName const&, Value); bool create_method_property(PropertyName const&, Value);
bool create_data_property_or_throw(PropertyName const&, Value); bool create_data_property_or_throw(PropertyName const&, Value);
bool create_non_enumerable_data_property_or_throw(PropertyName const&, Value);
bool define_property_or_throw(PropertyName const&, PropertyDescriptor const&); bool define_property_or_throw(PropertyName const&, PropertyDescriptor const&);
bool delete_property_or_throw(PropertyName const&); bool delete_property_or_throw(PropertyName const&);
bool has_property(PropertyName const&) const; bool has_property(PropertyName const&) const;