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
|
|
|
}
|
|
|
|
|
|
2021-04-17 12:21:51 -03:00
|
|
|
static void prepare_arguments_list(GlobalObject& global_object, Value value, MarkedValueList& arguments)
|
2020-05-01 07:06:27 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto& vm = global_object.vm();
|
2020-05-01 07:06:27 -03:00
|
|
|
if (!value.is_object()) {
|
2020-09-27 13:36:49 -03:00
|
|
|
vm.throw_exception<TypeError>(global_object, ErrorType::ReflectBadArgumentsList);
|
2020-05-01 07:06:27 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto& arguments_list = value.as_object();
|
2021-01-10 17:13:58 -03:00
|
|
|
auto length = length_of_array_like(global_object, arguments_list);
|
2020-09-27 13:36:49 -03:00
|
|
|
if (vm.exception())
|
2020-05-17 20:28:00 -03:00
|
|
|
return;
|
2020-05-01 07:06:27 -03:00
|
|
|
for (size_t i = 0; i < length; ++i) {
|
|
|
|
|
auto element = arguments_list.get(String::number(i));
|
2020-09-27 13:36:49 -03:00
|
|
|
if (vm.exception())
|
2020-05-01 07:06:27 -03:00
|
|
|
return;
|
2021-04-17 12:21:51 -03:00
|
|
|
arguments.append(element.value_or(js_undefined()));
|
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-08 15:12:14 -03:00
|
|
|
Object::define_property(vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Reflect"), Attribute::Configurable);
|
2020-05-01 07:06:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReflectObject::~ReflectObject()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
MarkedValueList arguments(vm.heap());
|
2021-04-17 12:21:51 -03:00
|
|
|
prepare_arguments_list(global_object, vm.argument(2), arguments);
|
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
|
|
|
}
|
|
|
|
|
|
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 {};
|
2020-09-27 13:36:49 -03:00
|
|
|
MarkedValueList arguments(vm.heap());
|
2021-04-17 12:21:51 -03:00
|
|
|
prepare_arguments_list(global_object, vm.argument(1), arguments);
|
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
|
|
|
}
|
2020-09-27 13:36:49 -03:00
|
|
|
return vm.construct(*target, *new_target, move(arguments), global_object);
|
2020-05-01 07:06:27 -03:00
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|