ReflectObject.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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/AbstractOperations.h>
  8. #include <LibJS/Runtime/Array.h>
  9. #include <LibJS/Runtime/Error.h>
  10. #include <LibJS/Runtime/FunctionObject.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. #include <LibJS/Runtime/NativeFunction.h>
  13. #include <LibJS/Runtime/ReflectObject.h>
  14. namespace JS {
  15. ReflectObject::ReflectObject(GlobalObject& global_object)
  16. : Object(*global_object.object_prototype())
  17. {
  18. }
  19. void ReflectObject::initialize(GlobalObject& global_object)
  20. {
  21. auto& vm = this->vm();
  22. Object::initialize(global_object);
  23. u8 attr = Attribute::Writable | Attribute::Configurable;
  24. define_native_function(vm.names.apply, apply, 3, attr);
  25. define_native_function(vm.names.construct, construct, 2, attr);
  26. define_native_function(vm.names.defineProperty, define_property, 3, attr);
  27. define_native_function(vm.names.deleteProperty, delete_property, 2, attr);
  28. define_native_function(vm.names.get, get, 2, attr);
  29. define_native_function(vm.names.getOwnPropertyDescriptor, get_own_property_descriptor, 2, attr);
  30. define_native_function(vm.names.getPrototypeOf, get_prototype_of, 1, attr);
  31. define_native_function(vm.names.has, has, 2, attr);
  32. define_native_function(vm.names.isExtensible, is_extensible, 1, attr);
  33. define_native_function(vm.names.ownKeys, own_keys, 1, attr);
  34. define_native_function(vm.names.preventExtensions, prevent_extensions, 1, attr);
  35. define_native_function(vm.names.set, set, 3, attr);
  36. define_native_function(vm.names.setPrototypeOf, set_prototype_of, 2, attr);
  37. // 28.1.14 Reflect [ @@toStringTag ], https://tc39.es/ecma262/#sec-reflect-@@tostringtag
  38. define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, vm.names.Reflect.as_string()), Attribute::Configurable);
  39. }
  40. ReflectObject::~ReflectObject()
  41. {
  42. }
  43. // 28.1.1 Reflect.apply ( target, thisArgument, argumentsList ), https://tc39.es/ecma262/#sec-reflect.apply
  44. JS_DEFINE_OLD_NATIVE_FUNCTION(ReflectObject::apply)
  45. {
  46. auto target = vm.argument(0);
  47. auto this_argument = vm.argument(1);
  48. auto arguments_list = vm.argument(2);
  49. // 1. If IsCallable(target) is false, throw a TypeError exception.
  50. if (!target.is_function()) {
  51. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, target.to_string_without_side_effects());
  52. return {};
  53. }
  54. // 2. Let args be ? CreateListFromArrayLike(argumentsList).
  55. auto args = TRY_OR_DISCARD(create_list_from_array_like(global_object, arguments_list));
  56. // 3. Perform PrepareForTailCall().
  57. // 4. Return ? Call(target, thisArgument, args).
  58. return TRY_OR_DISCARD(vm.call(target.as_function(), this_argument, move(args)));
  59. }
  60. // 28.1.2 Reflect.construct ( target, argumentsList [ , newTarget ] ), https://tc39.es/ecma262/#sec-reflect.construct
  61. JS_DEFINE_OLD_NATIVE_FUNCTION(ReflectObject::construct)
  62. {
  63. auto target = vm.argument(0);
  64. auto arguments_list = vm.argument(1);
  65. auto new_target = vm.argument(2);
  66. // 1. If IsConstructor(target) is false, throw a TypeError exception.
  67. if (!target.is_constructor()) {
  68. vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, target.to_string_without_side_effects());
  69. return {};
  70. }
  71. // 2. If newTarget is not present, set newTarget to target.
  72. if (vm.argument_count() < 3) {
  73. new_target = target;
  74. }
  75. // 3. Else if IsConstructor(newTarget) is false, throw a TypeError exception.
  76. else if (!new_target.is_constructor()) {
  77. vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, new_target.to_string_without_side_effects());
  78. return {};
  79. }
  80. // 4. Let args be ? CreateListFromArrayLike(argumentsList).
  81. auto args = TRY_OR_DISCARD(create_list_from_array_like(global_object, arguments_list));
  82. // 5. Return ? Construct(target, args, newTarget).
  83. return vm.construct(target.as_function(), new_target.as_function(), move(args));
  84. }
  85. // 28.1.3 Reflect.defineProperty ( target, propertyKey, attributes ), https://tc39.es/ecma262/#sec-reflect.defineproperty
  86. JS_DEFINE_OLD_NATIVE_FUNCTION(ReflectObject::define_property)
  87. {
  88. auto target = vm.argument(0);
  89. auto property_key = vm.argument(1);
  90. auto attributes = vm.argument(2);
  91. // 1. If Type(target) is not Object, throw a TypeError exception.
  92. if (!target.is_object()) {
  93. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  94. return {};
  95. }
  96. // 2. Let key be ? ToPropertyKey(propertyKey).
  97. auto key = TRY_OR_DISCARD(property_key.to_property_key(global_object));
  98. // 3. Let desc be ? ToPropertyDescriptor(attributes).
  99. auto descriptor = TRY_OR_DISCARD(to_property_descriptor(global_object, attributes));
  100. // 4. Return ? target.[[DefineOwnProperty]](key, desc).
  101. return Value(TRY_OR_DISCARD(target.as_object().internal_define_own_property(key, descriptor)));
  102. }
  103. // 28.1.4 Reflect.deleteProperty ( target, propertyKey ), https://tc39.es/ecma262/#sec-reflect.deleteproperty
  104. JS_DEFINE_OLD_NATIVE_FUNCTION(ReflectObject::delete_property)
  105. {
  106. auto target = vm.argument(0);
  107. auto property_key = vm.argument(1);
  108. // 1. If Type(target) is not Object, throw a TypeError exception.
  109. if (!target.is_object()) {
  110. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  111. return {};
  112. }
  113. // 2. Let key be ? ToPropertyKey(propertyKey).
  114. auto key = TRY_OR_DISCARD(property_key.to_property_key(global_object));
  115. // 3. Return ? target.[[Delete]](key).
  116. return Value(TRY_OR_DISCARD(target.as_object().internal_delete(key)));
  117. }
  118. // 28.1.5 Reflect.get ( target, propertyKey [ , receiver ] ), https://tc39.es/ecma262/#sec-reflect.get
  119. JS_DEFINE_OLD_NATIVE_FUNCTION(ReflectObject::get)
  120. {
  121. auto target = vm.argument(0);
  122. auto property_key = vm.argument(1);
  123. auto receiver = vm.argument(2);
  124. // 1. If Type(target) is not Object, throw a TypeError exception.
  125. if (!target.is_object()) {
  126. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  127. return {};
  128. }
  129. // 2. Let key be ? ToPropertyKey(propertyKey).
  130. auto key = TRY_OR_DISCARD(property_key.to_property_key(global_object));
  131. // 3. If receiver is not present, then
  132. if (vm.argument_count() < 3) {
  133. // a. Set receiver to target.
  134. receiver = target;
  135. }
  136. // 4. Return ? target.[[Get]](key, receiver).
  137. return TRY_OR_DISCARD(target.as_object().internal_get(key, receiver));
  138. }
  139. // 28.1.6 Reflect.getOwnPropertyDescriptor ( target, propertyKey ), https://tc39.es/ecma262/#sec-reflect.getownpropertydescriptor
  140. JS_DEFINE_OLD_NATIVE_FUNCTION(ReflectObject::get_own_property_descriptor)
  141. {
  142. auto target = vm.argument(0);
  143. auto property_key = vm.argument(1);
  144. // 1. If Type(target) is not Object, throw a TypeError exception.
  145. if (!target.is_object()) {
  146. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  147. return {};
  148. }
  149. // 2. Let key be ? ToPropertyKey(propertyKey).
  150. auto key = TRY_OR_DISCARD(property_key.to_property_key(global_object));
  151. // 3. Let desc be ? target.[[GetOwnProperty]](key).
  152. auto descriptor = TRY_OR_DISCARD(target.as_object().internal_get_own_property(key));
  153. // 4. Return FromPropertyDescriptor(desc).
  154. return from_property_descriptor(global_object, descriptor);
  155. }
  156. // 28.1.7 Reflect.getPrototypeOf ( target ), https://tc39.es/ecma262/#sec-reflect.getprototypeof
  157. JS_DEFINE_OLD_NATIVE_FUNCTION(ReflectObject::get_prototype_of)
  158. {
  159. auto target = vm.argument(0);
  160. // 1. If Type(target) is not Object, throw a TypeError exception.
  161. if (!target.is_object()) {
  162. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  163. return {};
  164. }
  165. // 2. Return ? target.[[GetPrototypeOf]]().
  166. return TRY_OR_DISCARD(target.as_object().internal_get_prototype_of());
  167. }
  168. // 28.1.8 Reflect.has ( target, propertyKey ), https://tc39.es/ecma262/#sec-reflect.has
  169. JS_DEFINE_OLD_NATIVE_FUNCTION(ReflectObject::has)
  170. {
  171. auto target = vm.argument(0);
  172. auto property_key = vm.argument(1);
  173. // 1. If Type(target) is not Object, throw a TypeError exception.
  174. if (!target.is_object()) {
  175. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  176. return {};
  177. }
  178. // 2. Let key be ? ToPropertyKey(propertyKey).
  179. auto key = TRY_OR_DISCARD(property_key.to_property_key(global_object));
  180. // 3. Return ? target.[[HasProperty]](key).
  181. return Value(TRY_OR_DISCARD(target.as_object().internal_has_property(key)));
  182. }
  183. // 28.1.9 Reflect.isExtensible ( target ), https://tc39.es/ecma262/#sec-reflect.isextensible
  184. JS_DEFINE_OLD_NATIVE_FUNCTION(ReflectObject::is_extensible)
  185. {
  186. auto target = vm.argument(0);
  187. // 1. If Type(target) is not Object, throw a TypeError exception.
  188. if (!target.is_object()) {
  189. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  190. return {};
  191. }
  192. // 2. Return ? target.[[IsExtensible]]().
  193. return Value(TRY_OR_DISCARD(target.as_object().internal_is_extensible()));
  194. }
  195. // 28.1.10 Reflect.ownKeys ( target ), https://tc39.es/ecma262/#sec-reflect.ownkeys
  196. JS_DEFINE_OLD_NATIVE_FUNCTION(ReflectObject::own_keys)
  197. {
  198. auto target = vm.argument(0);
  199. // 1. If Type(target) is not Object, throw a TypeError exception.
  200. if (!target.is_object()) {
  201. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  202. return {};
  203. }
  204. // 2. Let keys be ? target.[[OwnPropertyKeys]]().
  205. auto keys = TRY_OR_DISCARD(target.as_object().internal_own_property_keys());
  206. // 3. Return CreateArrayFromList(keys).
  207. return Array::create_from(global_object, keys);
  208. }
  209. // 28.1.11 Reflect.preventExtensions ( target ), https://tc39.es/ecma262/#sec-reflect.preventextensions
  210. JS_DEFINE_OLD_NATIVE_FUNCTION(ReflectObject::prevent_extensions)
  211. {
  212. auto target = vm.argument(0);
  213. // 1. If Type(target) is not Object, throw a TypeError exception.
  214. if (!target.is_object()) {
  215. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  216. return {};
  217. }
  218. // 2. Return ? target.[[PreventExtensions]]().
  219. return Value(TRY_OR_DISCARD(target.as_object().internal_prevent_extensions()));
  220. }
  221. // 28.1.12 Reflect.set ( target, propertyKey, V [ , receiver ] ), https://tc39.es/ecma262/#sec-reflect.set
  222. JS_DEFINE_OLD_NATIVE_FUNCTION(ReflectObject::set)
  223. {
  224. auto target = vm.argument(0);
  225. auto property_key = vm.argument(1);
  226. auto value = vm.argument(2);
  227. auto receiver = vm.argument(3);
  228. // 1. If Type(target) is not Object, throw a TypeError exception.
  229. if (!target.is_object()) {
  230. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  231. return {};
  232. }
  233. // 2. Let key be ? ToPropertyKey(propertyKey).
  234. auto key = TRY_OR_DISCARD(property_key.to_property_key(global_object));
  235. // 3. If receiver is not present, then
  236. if (vm.argument_count() < 4) {
  237. // a. Set receiver to target.
  238. receiver = target;
  239. }
  240. // 4. Return ? target.[[Set]](key, V, receiver).
  241. return Value(TRY_OR_DISCARD(target.as_object().internal_set(key, value, receiver)));
  242. }
  243. // 28.1.13 Reflect.setPrototypeOf ( target, proto ), https://tc39.es/ecma262/#sec-reflect.setprototypeof
  244. JS_DEFINE_OLD_NATIVE_FUNCTION(ReflectObject::set_prototype_of)
  245. {
  246. auto target = vm.argument(0);
  247. auto proto = vm.argument(1);
  248. // 1. If Type(target) is not Object, throw a TypeError exception.
  249. if (!target.is_object()) {
  250. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  251. return {};
  252. }
  253. // 2. If Type(proto) is not Object and proto is not null, throw a TypeError exception.
  254. if (!proto.is_object() && !proto.is_null()) {
  255. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  256. return {};
  257. }
  258. // 3. Return ? target.[[SetPrototypeOf]](proto).
  259. return Value(TRY_OR_DISCARD(target.as_object().internal_set_prototype_of(proto.is_null() ? nullptr : &proto.as_object())));
  260. }
  261. }