1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 18:18:12 +00:00

LibJS: Rewrite most of Object for spec compliance :^)

This is a huge patch, I know. In hindsight this perhaps could've been
done slightly more incremental, but I started and then fixed everything
until it worked, and here we are. I tried splitting of some completely
unrelated changes into separate commits, however. Anyway.

This is a rewrite of most of Object, and by extension large parts of
Array, Proxy, Reflect, String, TypedArray, and some other things.

What we already had worked fine for about 90% of things, but getting the
last 10% right proved to be increasingly difficult with the current code
that sort of grew organically and is only very loosely based on the
spec - this became especially obvious when we started fixing a large
number of test262 failures.

Key changes include:

- 1:1 matching function names and parameters of all object-related
  functions, to avoid ambiguity. Previously we had things like put(),
  which the spec doesn't have - as a result it wasn't always clear which
  need to be used.
- Better separation between object abstract operations and internal
  methods - the former are always the same, the latter can be overridden
  (and are therefore virtual). The internal methods (i.e. [[Foo]] in the
  spec) are now prefixed with 'internal_' for clarity - again, it was
  previously not always clear which AO a certain method represents,
  get() could've been both Get and [[Get]] (I don't know which one it
  was closer to right now).
  Note that some of the old names have been kept until all code relying
  on them is updated, but they are now simple wrappers around the
  closest matching standard abstract operation.
- Simplifications of the storage layer: functions that write values to
  storage are now prefixed with 'storage_' to make their purpose clear,
  and as they are not part of the spec they should not contain any steps
  specified by it. Much functionality is now covered by the layers above
  it and was removed (e.g. handling of accessors, attribute checks).
- PropertyAttributes has been greatly simplified, and is being replaced
  by PropertyDescriptor - a concept similar to the current
  implementation, but more aligned with the actual spec. See the commit
  message of the previous commit where it was introduced for details.
- As a bonus, and since I had to look at the spec a whole lot anyway, I
  introduced more inline comments with the exact steps from the spec -
  this makes it super easy to verify correctness.
- East-const all the things.

As a result of all of this, things are much more correct but a bit
slower now. Retaining speed wasn't a consideration at all, I have done
no profiling of the new code - there might be low hanging fruits, which
we can then harvest separately.

Special thanks to Idan for helping me with this by tracking down bugs,
updating everything outside of LibJS to work with these changes (LibWeb,
Spreadsheet, HackStudio), as well as providing countless patches to fix
regressions I introduced - there still are very few (we got it down to
5), but we also get many new passing test262 tests in return. :^)

Co-authored-by: Idan Horowitz <idan.horowitz@gmail.com>
This commit is contained in:
Linus Groh 2021-07-04 18:14:16 +01:00
parent 4e5362b7cb
commit 09bd5f8772
80 changed files with 4019 additions and 2239 deletions

View file

@ -15,17 +15,6 @@
namespace JS {
static Object* get_target_object_from(GlobalObject& global_object, const String& name)
{
auto& vm = global_object.vm();
auto target = vm.argument(0);
if (!target.is_object()) {
vm.throw_exception<TypeError>(global_object, ErrorType::ReflectArgumentMustBeAnObject, name);
return nullptr;
}
return static_cast<Object*>(&target.as_object());
}
ReflectObject::ReflectObject(GlobalObject& global_object)
: Object(*global_object.object_prototype())
{
@ -62,182 +51,294 @@ ReflectObject::~ReflectObject()
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::apply)
{
auto target = vm.argument(0);
auto this_argument = vm.argument(1);
auto arguments_list = vm.argument(2);
// 1. If IsCallable(target) is false, throw a TypeError exception.
if (!target.is_function()) {
vm.throw_exception<TypeError>(global_object, ErrorType::ReflectArgumentMustBeAFunction, vm.names.apply);
vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, target.to_string_without_side_effects());
return {};
}
auto this_arg = vm.argument(1);
auto arguments = create_list_from_array_like(global_object, vm.argument(2));
// 2. Let args be ? CreateListFromArrayLike(argumentsList).
auto args = create_list_from_array_like(global_object, arguments_list);
if (vm.exception())
return {};
return vm.call(target.as_function(), this_arg, move(arguments));
// 3. Perform PrepareForTailCall().
// 4. Return ? Call(target, thisArgument, args).
return vm.call(target.as_function(), this_argument, move(args));
}
// 28.1.2 Reflect.construct ( target, argumentsList [ , newTarget ] ), https://tc39.es/ecma262/#sec-reflect.construct
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::construct)
{
auto target = vm.argument(0);
auto arguments_list = vm.argument(1);
auto new_target = vm.argument(2);
// 1. If IsConstructor(target) is false, throw a TypeError exception.
if (!target.is_constructor()) {
vm.throw_exception<TypeError>(global_object, ErrorType::ReflectArgumentMustBeAConstructor, vm.names.construct);
vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, target.to_string_without_side_effects());
return {};
}
auto* new_target = &target.as_function();
if (vm.argument_count() > 2) {
auto new_target_value = vm.argument(2);
if (!new_target_value.is_constructor()) {
vm.throw_exception<TypeError>(global_object, ErrorType::ReflectBadNewTarget);
return {};
}
new_target = &new_target_value.as_function();
// 2. If newTarget is not present, set newTarget to target.
if (vm.argument_count() < 3) {
new_target = target;
}
// 3. Else if IsConstructor(newTarget) is false, throw a TypeError exception.
else if (!new_target.is_constructor()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, new_target.to_string_without_side_effects());
return {};
}
auto arguments = create_list_from_array_like(global_object, vm.argument(1));
// 4. Let args be ? CreateListFromArrayLike(argumentsList).
auto args = create_list_from_array_like(global_object, arguments_list);
if (vm.exception())
return {};
return vm.construct(target.as_function(), *new_target, move(arguments));
// 5. Return ? Construct(target, args, newTarget).
return vm.construct(target.as_function(), new_target.as_function(), move(args));
}
// 28.1.3 Reflect.defineProperty ( target, propertyKey, attributes ), https://tc39.es/ecma262/#sec-reflect.defineproperty
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::define_property)
{
auto* target = get_target_object_from(global_object, "defineProperty");
if (!target)
return {};
auto property_key = vm.argument(1).to_property_key(global_object);
if (vm.exception())
return {};
if (!vm.argument(2).is_object()) {
vm.throw_exception<TypeError>(global_object, ErrorType::ReflectBadDescriptorArgument);
auto target = vm.argument(0);
auto property_key = vm.argument(1);
auto attributes = vm.argument(2);
// 1. If Type(target) is not Object, throw a TypeError exception.
if (!target.is_object()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
return {};
}
auto& descriptor = vm.argument(2).as_object();
auto success = target->define_property(property_key, descriptor, false);
// 2. Let key be ? ToPropertyKey(propertyKey).
auto key = property_key.to_property_key(global_object);
if (vm.exception())
return {};
return Value(success);
// 3. Let desc be ? ToPropertyDescriptor(attributes).
auto descriptor = to_property_descriptor(global_object, attributes);
if (vm.exception())
return {};
// 4. Return ? target.[[DefineOwnProperty]](key, desc).
return Value(target.as_object().internal_define_own_property(key, descriptor));
}
// 28.1.4 Reflect.deleteProperty ( target, propertyKey ), https://tc39.es/ecma262/#sec-reflect.deleteproperty
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::delete_property)
{
auto* target = get_target_object_from(global_object, "deleteProperty");
if (!target)
auto target = vm.argument(0);
auto property_key = vm.argument(1);
// 1. If Type(target) is not Object, throw a TypeError exception.
if (!target.is_object()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
return {};
auto property_key = vm.argument(1).to_property_key(global_object);
}
// 2. Let key be ? ToPropertyKey(propertyKey).
auto key = property_key.to_property_key(global_object);
if (vm.exception())
return {};
return Value(target->delete_property(property_key));
// 3. Return ? target.[[Delete]](key).
return Value(target.as_object().internal_delete(key));
}
// 28.1.5 Reflect.get ( target, propertyKey [ , receiver ] ), https://tc39.es/ecma262/#sec-reflect.get
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get)
{
auto* target = get_target_object_from(global_object, "get");
if (!target)
auto target = vm.argument(0);
auto property_key = vm.argument(1);
auto receiver = vm.argument(2);
// 1. If Type(target) is not Object, throw a TypeError exception.
if (!target.is_object()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
return {};
auto property_key = vm.argument(1).to_property_key(global_object);
}
// 2. Let key be ? ToPropertyKey(propertyKey).
auto key = property_key.to_property_key(global_object);
if (vm.exception())
return {};
Value receiver = {};
if (vm.argument_count() > 2)
receiver = vm.argument(2);
return target->get(property_key, receiver).value_or(js_undefined());
// 3. If receiver is not present, then
if (vm.argument_count() < 3) {
// a. Set receiver to target.
receiver = target;
}
// 4. Return ? target.[[Get]](key, receiver).
return target.as_object().internal_get(key, receiver);
}
// 28.1.6 Reflect.getOwnPropertyDescriptor ( target, propertyKey ), https://tc39.es/ecma262/#sec-reflect.getownpropertydescriptor
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get_own_property_descriptor)
{
auto* target = get_target_object_from(global_object, "getOwnPropertyDescriptor");
if (!target)
auto target = vm.argument(0);
auto property_key = vm.argument(1);
// 1. If Type(target) is not Object, throw a TypeError exception.
if (!target.is_object()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
return {};
auto property_key = vm.argument(1).to_property_key(global_object);
}
// 2. Let key be ? ToPropertyKey(propertyKey).
auto key = property_key.to_property_key(global_object);
if (vm.exception())
return {};
return target->get_own_property_descriptor_object(property_key);
// 3. Let desc be ? target.[[GetOwnProperty]](key).
auto descriptor = target.as_object().internal_get_own_property(key);
if (vm.exception())
return {};
// 4. Return FromPropertyDescriptor(desc).
return from_property_descriptor(global_object, descriptor);
}
// 28.1.7 Reflect.getPrototypeOf ( target ), https://tc39.es/ecma262/#sec-reflect.getprototypeof
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get_prototype_of)
{
auto* target = get_target_object_from(global_object, "getPrototypeOf");
if (!target)
auto target = vm.argument(0);
// 1. If Type(target) is not Object, throw a TypeError exception.
if (!target.is_object()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
return {};
return target->prototype();
}
// 2. Return ? target.[[GetPrototypeOf]]().
return target.as_object().internal_get_prototype_of();
}
// 28.1.8 Reflect.has ( target, propertyKey ), https://tc39.es/ecma262/#sec-reflect.has
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::has)
{
auto* target = get_target_object_from(global_object, "has");
if (!target)
auto target = vm.argument(0);
auto property_key = vm.argument(1);
// 1. If Type(target) is not Object, throw a TypeError exception.
if (!target.is_object()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
return {};
auto property_key = vm.argument(1).to_property_key(global_object);
}
// 2. Let key be ? ToPropertyKey(propertyKey).
auto key = property_key.to_property_key(global_object);
if (vm.exception())
return {};
return Value(target->has_property(property_key));
// 3. Return ? target.[[HasProperty]](key).
return Value(target.as_object().internal_has_property(key));
}
// 28.1.9 Reflect.isExtensible ( target ), https://tc39.es/ecma262/#sec-reflect.isextensible
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::is_extensible)
{
auto* target = get_target_object_from(global_object, "isExtensible");
if (!target)
auto target = vm.argument(0);
// 1. If Type(target) is not Object, throw a TypeError exception.
if (!target.is_object()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
return {};
return Value(target->is_extensible());
}
// 2. Return ? target.[[IsExtensible]]().
return Value(target.as_object().internal_is_extensible());
}
// 28.1.10 Reflect.ownKeys ( target ), https://tc39.es/ecma262/#sec-reflect.ownkeys
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::own_keys)
{
auto* target = get_target_object_from(global_object, "ownKeys");
if (!target)
auto target = vm.argument(0);
// 1. If Type(target) is not Object, throw a TypeError exception.
if (!target.is_object()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
return {};
return Array::create_from(global_object, target->get_own_properties(PropertyKind::Key));
}
// 2. Let keys be ? target.[[OwnPropertyKeys]]().
auto keys = target.as_object().internal_own_property_keys();
if (vm.exception())
return {};
// 3. Return CreateArrayFromList(keys).
return Array::create_from(global_object, keys);
}
// 28.1.11 Reflect.preventExtensions ( target ), https://tc39.es/ecma262/#sec-reflect.preventextensions
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::prevent_extensions)
{
auto* target = get_target_object_from(global_object, "preventExtensions");
if (!target)
auto target = vm.argument(0);
// 1. If Type(target) is not Object, throw a TypeError exception.
if (!target.is_object()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
return {};
return Value(target->prevent_extensions());
}
// 2. Return ? target.[[PreventExtensions]]().
return Value(target.as_object().internal_prevent_extensions());
}
// 28.1.12 Reflect.set ( target, propertyKey, V [ , receiver ] ), https://tc39.es/ecma262/#sec-reflect.set
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set)
{
auto* target = get_target_object_from(global_object, "set");
if (!target)
auto target = vm.argument(0);
auto property_key = vm.argument(1);
auto value = vm.argument(2);
auto receiver = vm.argument(3);
// 1. If Type(target) is not Object, throw a TypeError exception.
if (!target.is_object()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
return {};
auto property_key = vm.argument(1).to_property_key(global_object);
}
// 2. Let key be ? ToPropertyKey(propertyKey).
auto key = property_key.to_property_key(global_object);
if (vm.exception())
return {};
auto value = vm.argument(2);
Value receiver = {};
if (vm.argument_count() > 3)
receiver = vm.argument(3);
return Value(target->put(property_key, value, receiver));
// 3. If receiver is not present, then
if (vm.argument_count() < 4) {
// a. Set receiver to target.
receiver = target;
}
// 4. Return ? target.[[Set]](key, V, receiver).
return Value(target.as_object().internal_set(key, value, receiver));
}
// 28.1.13 Reflect.setPrototypeOf ( target, proto ), https://tc39.es/ecma262/#sec-reflect.setprototypeof
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set_prototype_of)
{
auto* target = get_target_object_from(global_object, "setPrototypeOf");
if (!target)
auto target = vm.argument(0);
auto proto = vm.argument(1);
// 1. If Type(target) is not Object, throw a TypeError exception.
if (!target.is_object()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
return {};
auto prototype_value = vm.argument(1);
if (!prototype_value.is_object() && !prototype_value.is_null()) {
}
// 2. If Type(proto) is not Object and proto is not null, throw a TypeError exception.
if (!proto.is_object() && !proto.is_null()) {
vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
return {};
}
Object* prototype = nullptr;
if (!prototype_value.is_null())
prototype = const_cast<Object*>(&prototype_value.as_object());
return Value(target->set_prototype(prototype));
// 3. Return ? target.[[SetPrototypeOf]](proto).
return Value(target.as_object().internal_set_prototype_of(proto.is_null() ? nullptr : &proto.as_object()));
}
}