2020-05-01 07:06:27 -03:00
|
|
|
/*
|
2021-06-07 19:11:08 -03:00
|
|
|
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
|
2020-05-01 07:06:27 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-01 07:06:27 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
2021-04-06 16:39:17 -03:00
|
|
|
#include <LibJS/Runtime/Array.h>
|
2020-05-01 07:06:27 -03:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
|
|
|
|
#include <LibJS/Runtime/Function.h>
|
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
|
|
#include <LibJS/Runtime/NativeFunction.h>
|
|
|
|
|
#include <LibJS/Runtime/ReflectObject.h>
|
|
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2020-09-27 13:36:49 -03:00
|
|
|
static Object* get_target_object_from(GlobalObject& global_object, const String& name)
|
2020-05-01 07:06:27 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto& vm = global_object.vm();
|
|
|
|
|
auto target = vm.argument(0);
|
2020-05-01 07:06:27 -03:00
|
|
|
if (!target.is_object()) {
|
2020-10-04 09:55:20 -03:00
|
|
|
vm.throw_exception<TypeError>(global_object, ErrorType::ReflectArgumentMustBeAnObject, name);
|
2020-05-01 07:06:27 -03:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return static_cast<Object*>(&target.as_object());
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-27 13:36:49 -03:00
|
|
|
static Function* get_target_function_from(GlobalObject& global_object, const String& name)
|
2020-05-01 07:06:27 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto& vm = global_object.vm();
|
|
|
|
|
auto target = vm.argument(0);
|
2020-05-06 07:52:53 -03:00
|
|
|
if (!target.is_function()) {
|
2020-10-04 09:55:20 -03:00
|
|
|
vm.throw_exception<TypeError>(global_object, ErrorType::ReflectArgumentMustBeAFunction, name);
|
2020-05-01 07:06:27 -03:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
2020-05-06 07:52:53 -03:00
|
|
|
return &target.as_function();
|
2020-05-01 07:06:27 -03:00
|
|
|
}
|
|
|
|
|
|
2020-06-20 12:11:11 -03:00
|
|
|
ReflectObject::ReflectObject(GlobalObject& global_object)
|
2020-06-23 12:21:53 -03:00
|
|
|
: Object(*global_object.object_prototype())
|
2020-06-20 12:11:11 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-22 12:50:18 -03:00
|
|
|
void ReflectObject::initialize(GlobalObject& global_object)
|
2020-05-01 07:06:27 -03:00
|
|
|
{
|
2020-10-13 18:49:19 -03:00
|
|
|
auto& vm = this->vm();
|
2020-07-22 12:50:18 -03:00
|
|
|
Object::initialize(global_object);
|
2020-05-01 07:06:27 -03:00
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
2020-10-13 18:49:19 -03:00
|
|
|
define_native_function(vm.names.apply, apply, 3, attr);
|
|
|
|
|
define_native_function(vm.names.construct, construct, 2, attr);
|
|
|
|
|
define_native_function(vm.names.defineProperty, define_property, 3, attr);
|
|
|
|
|
define_native_function(vm.names.deleteProperty, delete_property, 2, attr);
|
|
|
|
|
define_native_function(vm.names.get, get, 2, attr);
|
|
|
|
|
define_native_function(vm.names.getOwnPropertyDescriptor, get_own_property_descriptor, 2, attr);
|
|
|
|
|
define_native_function(vm.names.getPrototypeOf, get_prototype_of, 1, attr);
|
|
|
|
|
define_native_function(vm.names.has, has, 2, attr);
|
|
|
|
|
define_native_function(vm.names.isExtensible, is_extensible, 1, attr);
|
|
|
|
|
define_native_function(vm.names.ownKeys, own_keys, 1, attr);
|
|
|
|
|
define_native_function(vm.names.preventExtensions, prevent_extensions, 1, attr);
|
|
|
|
|
define_native_function(vm.names.set, set, 3, attr);
|
|
|
|
|
define_native_function(vm.names.setPrototypeOf, set_prototype_of, 2, attr);
|
2021-06-12 20:22:35 -03:00
|
|
|
|
|
|
|
|
// 28.1.14 Reflect [ @@toStringTag ], https://tc39.es/ecma262/#sec-reflect-@@tostringtag
|
2021-06-13 13:59:07 -03:00
|
|
|
Object::define_property(vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), vm.names.Reflect.as_string()), Attribute::Configurable);
|
2020-05-01 07:06:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReflectObject::~ReflectObject()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 28.1.1 Reflect.apply ( target, thisArgument, argumentsList ), https://tc39.es/ecma262/#sec-reflect.apply
|
2020-06-20 08:55:34 -03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::apply)
|
2020-05-01 07:06:27 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto* target = get_target_function_from(global_object, "apply");
|
2020-05-01 07:06:27 -03:00
|
|
|
if (!target)
|
|
|
|
|
return {};
|
2020-09-27 13:36:49 -03:00
|
|
|
auto this_arg = vm.argument(1);
|
2021-06-09 19:28:20 -03:00
|
|
|
auto arguments = create_list_from_array_like(global_object, vm.argument(2));
|
2020-09-27 13:36:49 -03:00
|
|
|
if (vm.exception())
|
2020-05-01 07:06:27 -03:00
|
|
|
return {};
|
2020-09-27 13:36:49 -03:00
|
|
|
return vm.call(*target, this_arg, move(arguments));
|
2020-05-01 07:06:27 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 28.1.2 Reflect.construct ( target, argumentsList [ , newTarget ] ), https://tc39.es/ecma262/#sec-reflect.construct
|
2020-06-20 08:55:34 -03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::construct)
|
2020-05-01 07:06:27 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto* target = get_target_function_from(global_object, "construct");
|
2020-05-01 07:06:27 -03:00
|
|
|
if (!target)
|
|
|
|
|
return {};
|
2021-06-09 19:28:20 -03:00
|
|
|
auto arguments = create_list_from_array_like(global_object, vm.argument(1));
|
2020-09-27 13:36:49 -03:00
|
|
|
if (vm.exception())
|
2020-05-01 07:06:27 -03:00
|
|
|
return {};
|
|
|
|
|
auto* new_target = target;
|
2020-09-27 13:36:49 -03:00
|
|
|
if (vm.argument_count() > 2) {
|
|
|
|
|
auto new_target_value = vm.argument(2);
|
2020-05-06 07:52:53 -03:00
|
|
|
if (!new_target_value.is_function()
|
2021-01-01 13:46:39 -03:00
|
|
|
|| (is<NativeFunction>(new_target_value.as_object()) && !static_cast<NativeFunction&>(new_target_value.as_object()).has_constructor())) {
|
2020-09-27 13:36:49 -03:00
|
|
|
vm.throw_exception<TypeError>(global_object, ErrorType::ReflectBadNewTarget);
|
2020-05-01 07:06:27 -03:00
|
|
|
return {};
|
|
|
|
|
}
|
2020-05-06 07:52:53 -03:00
|
|
|
new_target = &new_target_value.as_function();
|
2020-05-01 07:06:27 -03:00
|
|
|
}
|
2021-06-10 18:17:29 -03:00
|
|
|
return vm.construct(*target, *new_target, move(arguments));
|
2020-05-01 07:06:27 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 28.1.3 Reflect.defineProperty ( target, propertyKey, attributes ), https://tc39.es/ecma262/#sec-reflect.defineproperty
|
2020-06-20 08:55:34 -03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::define_property)
|
2020-05-01 07:06:27 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto* target = get_target_object_from(global_object, "defineProperty");
|
2020-05-01 07:06:27 -03:00
|
|
|
if (!target)
|
|
|
|
|
return {};
|
2021-06-05 09:22:11 -03:00
|
|
|
auto property_key = vm.argument(1).to_property_key(global_object);
|
|
|
|
|
if (vm.exception())
|
|
|
|
|
return {};
|
2020-09-27 13:36:49 -03:00
|
|
|
if (!vm.argument(2).is_object()) {
|
|
|
|
|
vm.throw_exception<TypeError>(global_object, ErrorType::ReflectBadDescriptorArgument);
|
2020-08-25 07:52:32 -03:00
|
|
|
return {};
|
|
|
|
|
}
|
2020-09-27 13:36:49 -03:00
|
|
|
auto& descriptor = vm.argument(2).as_object();
|
2020-05-01 07:06:27 -03:00
|
|
|
auto success = target->define_property(property_key, descriptor, false);
|
2020-09-27 13:36:49 -03:00
|
|
|
if (vm.exception())
|
2020-06-07 14:53:14 -03:00
|
|
|
return {};
|
2020-05-01 07:06:27 -03:00
|
|
|
return Value(success);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 28.1.4 Reflect.deleteProperty ( target, propertyKey ), https://tc39.es/ecma262/#sec-reflect.deleteproperty
|
2020-06-20 08:55:34 -03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::delete_property)
|
2020-05-01 07:06:27 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto* target = get_target_object_from(global_object, "deleteProperty");
|
2020-05-01 07:06:27 -03:00
|
|
|
if (!target)
|
|
|
|
|
return {};
|
2021-06-07 19:11:08 -03:00
|
|
|
auto property_key = vm.argument(1).to_property_key(global_object);
|
2020-09-27 13:36:49 -03:00
|
|
|
if (vm.exception())
|
2020-05-17 20:28:00 -03:00
|
|
|
return {};
|
2021-06-07 19:11:08 -03:00
|
|
|
return Value(target->delete_property(property_key));
|
2020-05-01 07:06:27 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 28.1.5 Reflect.get ( target, propertyKey [ , receiver ] ), https://tc39.es/ecma262/#sec-reflect.get
|
2020-06-20 08:55:34 -03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get)
|
2020-05-01 07:06:27 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto* target = get_target_object_from(global_object, "get");
|
2020-05-01 07:06:27 -03:00
|
|
|
if (!target)
|
|
|
|
|
return {};
|
2021-06-07 19:11:08 -03:00
|
|
|
auto property_key = vm.argument(1).to_property_key(global_object);
|
2020-09-27 13:36:49 -03:00
|
|
|
if (vm.exception())
|
2020-05-15 08:39:24 -03:00
|
|
|
return {};
|
2020-06-25 10:00:13 -03:00
|
|
|
Value receiver = {};
|
2020-09-27 13:36:49 -03:00
|
|
|
if (vm.argument_count() > 2)
|
|
|
|
|
receiver = vm.argument(2);
|
2020-06-25 10:00:13 -03:00
|
|
|
return target->get(property_key, receiver).value_or(js_undefined());
|
2020-05-01 07:06:27 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 28.1.6 Reflect.getOwnPropertyDescriptor ( target, propertyKey ), https://tc39.es/ecma262/#sec-reflect.getownpropertydescriptor
|
2020-06-20 08:55:34 -03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get_own_property_descriptor)
|
2020-05-01 07:06:27 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto* target = get_target_object_from(global_object, "getOwnPropertyDescriptor");
|
2020-05-01 07:06:27 -03:00
|
|
|
if (!target)
|
|
|
|
|
return {};
|
2021-06-07 19:11:08 -03:00
|
|
|
auto property_key = vm.argument(1).to_property_key(global_object);
|
2020-09-27 13:36:49 -03:00
|
|
|
if (vm.exception())
|
2020-05-15 08:39:24 -03:00
|
|
|
return {};
|
2020-06-03 13:40:17 -03:00
|
|
|
return target->get_own_property_descriptor_object(property_key);
|
2020-05-01 07:06:27 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 28.1.7 Reflect.getPrototypeOf ( target ), https://tc39.es/ecma262/#sec-reflect.getprototypeof
|
2020-06-20 08:55:34 -03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get_prototype_of)
|
2020-05-01 07:06:27 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto* target = get_target_object_from(global_object, "getPrototypeOf");
|
2020-05-01 07:06:27 -03:00
|
|
|
if (!target)
|
|
|
|
|
return {};
|
|
|
|
|
return target->prototype();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 28.1.8 Reflect.has ( target, propertyKey ), https://tc39.es/ecma262/#sec-reflect.has
|
2020-06-20 08:55:34 -03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::has)
|
2020-05-01 07:06:27 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto* target = get_target_object_from(global_object, "has");
|
2020-05-01 07:06:27 -03:00
|
|
|
if (!target)
|
|
|
|
|
return {};
|
2021-06-07 19:11:08 -03:00
|
|
|
auto property_key = vm.argument(1).to_property_key(global_object);
|
2020-09-27 13:36:49 -03:00
|
|
|
if (vm.exception())
|
2020-05-15 08:39:24 -03:00
|
|
|
return {};
|
2020-05-01 07:06:27 -03:00
|
|
|
return Value(target->has_property(property_key));
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 28.1.9 Reflect.isExtensible ( target ), https://tc39.es/ecma262/#sec-reflect.isextensible
|
2020-06-20 08:55:34 -03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::is_extensible)
|
2020-05-01 07:06:27 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto* target = get_target_object_from(global_object, "isExtensible");
|
2020-06-02 08:36:11 -03:00
|
|
|
if (!target)
|
|
|
|
|
return {};
|
|
|
|
|
return Value(target->is_extensible());
|
2020-05-01 07:06:27 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 28.1.10 Reflect.ownKeys ( target ), https://tc39.es/ecma262/#sec-reflect.ownkeys
|
2020-06-20 08:55:34 -03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::own_keys)
|
2020-05-01 07:06:27 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto* target = get_target_object_from(global_object, "ownKeys");
|
2020-05-01 07:06:27 -03:00
|
|
|
if (!target)
|
|
|
|
|
return {};
|
2021-04-06 16:39:17 -03:00
|
|
|
return Array::create_from(global_object, target->get_own_properties(PropertyKind::Key));
|
2020-05-01 07:06:27 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 28.1.11 Reflect.preventExtensions ( target ), https://tc39.es/ecma262/#sec-reflect.preventextensions
|
2020-06-20 08:55:34 -03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::prevent_extensions)
|
2020-05-01 07:06:27 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto* target = get_target_object_from(global_object, "preventExtensions");
|
2020-06-02 08:36:11 -03:00
|
|
|
if (!target)
|
|
|
|
|
return {};
|
|
|
|
|
return Value(target->prevent_extensions());
|
2020-05-01 07:06:27 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 28.1.12 Reflect.set ( target, propertyKey, V [ , receiver ] ), https://tc39.es/ecma262/#sec-reflect.set
|
2020-06-20 08:55:34 -03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set)
|
2020-05-01 07:06:27 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto* target = get_target_object_from(global_object, "set");
|
2020-05-01 07:06:27 -03:00
|
|
|
if (!target)
|
|
|
|
|
return {};
|
2021-06-07 19:11:08 -03:00
|
|
|
auto property_key = vm.argument(1).to_property_key(global_object);
|
2020-09-27 13:36:49 -03:00
|
|
|
if (vm.exception())
|
2020-05-15 08:39:24 -03:00
|
|
|
return {};
|
2020-09-27 13:36:49 -03:00
|
|
|
auto value = vm.argument(2);
|
2020-06-25 10:00:13 -03:00
|
|
|
Value receiver = {};
|
2020-09-27 13:36:49 -03:00
|
|
|
if (vm.argument_count() > 3)
|
|
|
|
|
receiver = vm.argument(3);
|
2020-06-25 10:00:13 -03:00
|
|
|
return Value(target->put(property_key, value, receiver));
|
2020-05-01 07:06:27 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
// 28.1.13 Reflect.setPrototypeOf ( target, proto ), https://tc39.es/ecma262/#sec-reflect.setprototypeof
|
2020-06-20 08:55:34 -03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set_prototype_of)
|
2020-05-01 07:06:27 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto* target = get_target_object_from(global_object, "setPrototypeOf");
|
2020-05-01 07:06:27 -03:00
|
|
|
if (!target)
|
|
|
|
|
return {};
|
2020-09-27 13:36:49 -03:00
|
|
|
auto prototype_value = vm.argument(1);
|
2020-05-01 07:06:27 -03:00
|
|
|
if (!prototype_value.is_object() && !prototype_value.is_null()) {
|
2020-09-27 13:36:49 -03:00
|
|
|
vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
|
2020-05-01 07:06:27 -03:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
Object* prototype = nullptr;
|
|
|
|
|
if (!prototype_value.is_null())
|
|
|
|
|
prototype = const_cast<Object*>(&prototype_value.as_object());
|
2020-06-02 08:39:02 -03:00
|
|
|
return Value(target->set_prototype(prototype));
|
2020-05-01 07:06:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|