ReflectObject.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Function.h>
  7. #include <LibJS/Runtime/Array.h>
  8. #include <LibJS/Runtime/Error.h>
  9. #include <LibJS/Runtime/Function.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/NativeFunction.h>
  12. #include <LibJS/Runtime/ReflectObject.h>
  13. namespace JS {
  14. static Object* get_target_object_from(GlobalObject& global_object, const String& name)
  15. {
  16. auto& vm = global_object.vm();
  17. auto target = vm.argument(0);
  18. if (!target.is_object()) {
  19. vm.throw_exception<TypeError>(global_object, ErrorType::ReflectArgumentMustBeAnObject, name);
  20. return nullptr;
  21. }
  22. return static_cast<Object*>(&target.as_object());
  23. }
  24. static Function* get_target_function_from(GlobalObject& global_object, const String& name)
  25. {
  26. auto& vm = global_object.vm();
  27. auto target = vm.argument(0);
  28. if (!target.is_function()) {
  29. vm.throw_exception<TypeError>(global_object, ErrorType::ReflectArgumentMustBeAFunction, name);
  30. return nullptr;
  31. }
  32. return &target.as_function();
  33. }
  34. ReflectObject::ReflectObject(GlobalObject& global_object)
  35. : Object(*global_object.object_prototype())
  36. {
  37. }
  38. void ReflectObject::initialize(GlobalObject& global_object)
  39. {
  40. auto& vm = this->vm();
  41. Object::initialize(global_object);
  42. u8 attr = Attribute::Writable | Attribute::Configurable;
  43. define_native_function(vm.names.apply, apply, 3, attr);
  44. define_native_function(vm.names.construct, construct, 2, attr);
  45. define_native_function(vm.names.defineProperty, define_property, 3, attr);
  46. define_native_function(vm.names.deleteProperty, delete_property, 2, attr);
  47. define_native_function(vm.names.get, get, 2, attr);
  48. define_native_function(vm.names.getOwnPropertyDescriptor, get_own_property_descriptor, 2, attr);
  49. define_native_function(vm.names.getPrototypeOf, get_prototype_of, 1, attr);
  50. define_native_function(vm.names.has, has, 2, attr);
  51. define_native_function(vm.names.isExtensible, is_extensible, 1, attr);
  52. define_native_function(vm.names.ownKeys, own_keys, 1, attr);
  53. define_native_function(vm.names.preventExtensions, prevent_extensions, 1, attr);
  54. define_native_function(vm.names.set, set, 3, attr);
  55. define_native_function(vm.names.setPrototypeOf, set_prototype_of, 2, attr);
  56. // 28.1.14 Reflect [ @@toStringTag ], https://tc39.es/ecma262/#sec-reflect-@@tostringtag
  57. Object::define_property(vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), vm.names.Reflect.as_string()), Attribute::Configurable);
  58. }
  59. ReflectObject::~ReflectObject()
  60. {
  61. }
  62. // 28.1.1 Reflect.apply ( target, thisArgument, argumentsList ), https://tc39.es/ecma262/#sec-reflect.apply
  63. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::apply)
  64. {
  65. auto* target = get_target_function_from(global_object, "apply");
  66. if (!target)
  67. return {};
  68. auto this_arg = vm.argument(1);
  69. auto arguments = create_list_from_array_like(global_object, vm.argument(2));
  70. if (vm.exception())
  71. return {};
  72. return vm.call(*target, this_arg, move(arguments));
  73. }
  74. // 28.1.2 Reflect.construct ( target, argumentsList [ , newTarget ] ), https://tc39.es/ecma262/#sec-reflect.construct
  75. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::construct)
  76. {
  77. auto* target = get_target_function_from(global_object, "construct");
  78. if (!target)
  79. return {};
  80. auto arguments = create_list_from_array_like(global_object, vm.argument(1));
  81. if (vm.exception())
  82. return {};
  83. auto* new_target = target;
  84. if (vm.argument_count() > 2) {
  85. auto new_target_value = vm.argument(2);
  86. if (!new_target_value.is_function()
  87. || (is<NativeFunction>(new_target_value.as_object()) && !static_cast<NativeFunction&>(new_target_value.as_object()).has_constructor())) {
  88. vm.throw_exception<TypeError>(global_object, ErrorType::ReflectBadNewTarget);
  89. return {};
  90. }
  91. new_target = &new_target_value.as_function();
  92. }
  93. return vm.construct(*target, *new_target, move(arguments));
  94. }
  95. // 28.1.3 Reflect.defineProperty ( target, propertyKey, attributes ), https://tc39.es/ecma262/#sec-reflect.defineproperty
  96. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::define_property)
  97. {
  98. auto* target = get_target_object_from(global_object, "defineProperty");
  99. if (!target)
  100. return {};
  101. auto property_key = vm.argument(1).to_property_key(global_object);
  102. if (vm.exception())
  103. return {};
  104. if (!vm.argument(2).is_object()) {
  105. vm.throw_exception<TypeError>(global_object, ErrorType::ReflectBadDescriptorArgument);
  106. return {};
  107. }
  108. auto& descriptor = vm.argument(2).as_object();
  109. auto success = target->define_property(property_key, descriptor, false);
  110. if (vm.exception())
  111. return {};
  112. return Value(success);
  113. }
  114. // 28.1.4 Reflect.deleteProperty ( target, propertyKey ), https://tc39.es/ecma262/#sec-reflect.deleteproperty
  115. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::delete_property)
  116. {
  117. auto* target = get_target_object_from(global_object, "deleteProperty");
  118. if (!target)
  119. return {};
  120. auto property_key = vm.argument(1).to_property_key(global_object);
  121. if (vm.exception())
  122. return {};
  123. return Value(target->delete_property(property_key));
  124. }
  125. // 28.1.5 Reflect.get ( target, propertyKey [ , receiver ] ), https://tc39.es/ecma262/#sec-reflect.get
  126. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get)
  127. {
  128. auto* target = get_target_object_from(global_object, "get");
  129. if (!target)
  130. return {};
  131. auto property_key = vm.argument(1).to_property_key(global_object);
  132. if (vm.exception())
  133. return {};
  134. Value receiver = {};
  135. if (vm.argument_count() > 2)
  136. receiver = vm.argument(2);
  137. return target->get(property_key, receiver).value_or(js_undefined());
  138. }
  139. // 28.1.6 Reflect.getOwnPropertyDescriptor ( target, propertyKey ), https://tc39.es/ecma262/#sec-reflect.getownpropertydescriptor
  140. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get_own_property_descriptor)
  141. {
  142. auto* target = get_target_object_from(global_object, "getOwnPropertyDescriptor");
  143. if (!target)
  144. return {};
  145. auto property_key = vm.argument(1).to_property_key(global_object);
  146. if (vm.exception())
  147. return {};
  148. return target->get_own_property_descriptor_object(property_key);
  149. }
  150. // 28.1.7 Reflect.getPrototypeOf ( target ), https://tc39.es/ecma262/#sec-reflect.getprototypeof
  151. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get_prototype_of)
  152. {
  153. auto* target = get_target_object_from(global_object, "getPrototypeOf");
  154. if (!target)
  155. return {};
  156. return target->prototype();
  157. }
  158. // 28.1.8 Reflect.has ( target, propertyKey ), https://tc39.es/ecma262/#sec-reflect.has
  159. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::has)
  160. {
  161. auto* target = get_target_object_from(global_object, "has");
  162. if (!target)
  163. return {};
  164. auto property_key = vm.argument(1).to_property_key(global_object);
  165. if (vm.exception())
  166. return {};
  167. return Value(target->has_property(property_key));
  168. }
  169. // 28.1.9 Reflect.isExtensible ( target ), https://tc39.es/ecma262/#sec-reflect.isextensible
  170. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::is_extensible)
  171. {
  172. auto* target = get_target_object_from(global_object, "isExtensible");
  173. if (!target)
  174. return {};
  175. return Value(target->is_extensible());
  176. }
  177. // 28.1.10 Reflect.ownKeys ( target ), https://tc39.es/ecma262/#sec-reflect.ownkeys
  178. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::own_keys)
  179. {
  180. auto* target = get_target_object_from(global_object, "ownKeys");
  181. if (!target)
  182. return {};
  183. return Array::create_from(global_object, target->get_own_properties(PropertyKind::Key));
  184. }
  185. // 28.1.11 Reflect.preventExtensions ( target ), https://tc39.es/ecma262/#sec-reflect.preventextensions
  186. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::prevent_extensions)
  187. {
  188. auto* target = get_target_object_from(global_object, "preventExtensions");
  189. if (!target)
  190. return {};
  191. return Value(target->prevent_extensions());
  192. }
  193. // 28.1.12 Reflect.set ( target, propertyKey, V [ , receiver ] ), https://tc39.es/ecma262/#sec-reflect.set
  194. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set)
  195. {
  196. auto* target = get_target_object_from(global_object, "set");
  197. if (!target)
  198. return {};
  199. auto property_key = vm.argument(1).to_property_key(global_object);
  200. if (vm.exception())
  201. return {};
  202. auto value = vm.argument(2);
  203. Value receiver = {};
  204. if (vm.argument_count() > 3)
  205. receiver = vm.argument(3);
  206. return Value(target->put(property_key, value, receiver));
  207. }
  208. // 28.1.13 Reflect.setPrototypeOf ( target, proto ), https://tc39.es/ecma262/#sec-reflect.setprototypeof
  209. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set_prototype_of)
  210. {
  211. auto* target = get_target_object_from(global_object, "setPrototypeOf");
  212. if (!target)
  213. return {};
  214. auto prototype_value = vm.argument(1);
  215. if (!prototype_value.is_object() && !prototype_value.is_null()) {
  216. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  217. return {};
  218. }
  219. Object* prototype = nullptr;
  220. if (!prototype_value.is_null())
  221. prototype = const_cast<Object*>(&prototype_value.as_object());
  222. return Value(target->set_prototype(prototype));
  223. }
  224. }