ReflectObject.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. // 28.1.1 Reflect.apply ( target, thisArgument, argumentsList ), https://tc39.es/ecma262/#sec-reflect.apply
  41. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::apply)
  42. {
  43. auto target = vm.argument(0);
  44. auto this_argument = vm.argument(1);
  45. auto arguments_list = vm.argument(2);
  46. // 1. If IsCallable(target) is false, throw a TypeError exception.
  47. if (!target.is_function())
  48. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, target.to_string_without_side_effects());
  49. // 2. Let args be ? CreateListFromArrayLike(argumentsList).
  50. auto args = TRY(create_list_from_array_like(global_object, arguments_list));
  51. // 3. Perform PrepareForTailCall().
  52. // 4. Return ? Call(target, thisArgument, args).
  53. return TRY(call(global_object, target.as_function(), this_argument, move(args)));
  54. }
  55. // 28.1.2 Reflect.construct ( target, argumentsList [ , newTarget ] ), https://tc39.es/ecma262/#sec-reflect.construct
  56. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::construct)
  57. {
  58. auto target = vm.argument(0);
  59. auto arguments_list = vm.argument(1);
  60. auto new_target = vm.argument(2);
  61. // 1. If IsConstructor(target) is false, throw a TypeError exception.
  62. if (!target.is_constructor())
  63. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAConstructor, target.to_string_without_side_effects());
  64. // 2. If newTarget is not present, set newTarget to target.
  65. if (vm.argument_count() < 3)
  66. new_target = target;
  67. // 3. Else if IsConstructor(newTarget) is false, throw a TypeError exception.
  68. else if (!new_target.is_constructor())
  69. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAConstructor, new_target.to_string_without_side_effects());
  70. // 4. Let args be ? CreateListFromArrayLike(argumentsList).
  71. auto args = TRY(create_list_from_array_like(global_object, arguments_list));
  72. // 5. Return ? Construct(target, args, newTarget).
  73. return TRY(JS::construct(global_object, target.as_function(), move(args), &new_target.as_function()));
  74. }
  75. // 28.1.3 Reflect.defineProperty ( target, propertyKey, attributes ), https://tc39.es/ecma262/#sec-reflect.defineproperty
  76. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::define_property)
  77. {
  78. auto target = vm.argument(0);
  79. auto property_key = vm.argument(1);
  80. auto attributes = vm.argument(2);
  81. // 1. If Type(target) is not Object, throw a TypeError exception.
  82. if (!target.is_object())
  83. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  84. // 2. Let key be ? ToPropertyKey(propertyKey).
  85. auto key = TRY(property_key.to_property_key(global_object));
  86. // 3. Let desc be ? ToPropertyDescriptor(attributes).
  87. auto descriptor = TRY(to_property_descriptor(global_object, attributes));
  88. // 4. Return ? target.[[DefineOwnProperty]](key, desc).
  89. return Value(TRY(target.as_object().internal_define_own_property(key, descriptor)));
  90. }
  91. // 28.1.4 Reflect.deleteProperty ( target, propertyKey ), https://tc39.es/ecma262/#sec-reflect.deleteproperty
  92. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::delete_property)
  93. {
  94. auto target = vm.argument(0);
  95. auto property_key = vm.argument(1);
  96. // 1. If Type(target) is not Object, throw a TypeError exception.
  97. if (!target.is_object())
  98. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  99. // 2. Let key be ? ToPropertyKey(propertyKey).
  100. auto key = TRY(property_key.to_property_key(global_object));
  101. // 3. Return ? target.[[Delete]](key).
  102. return Value(TRY(target.as_object().internal_delete(key)));
  103. }
  104. // 28.1.5 Reflect.get ( target, propertyKey [ , receiver ] ), https://tc39.es/ecma262/#sec-reflect.get
  105. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get)
  106. {
  107. auto target = vm.argument(0);
  108. auto property_key = vm.argument(1);
  109. auto receiver = vm.argument(2);
  110. // 1. If Type(target) is not Object, throw a TypeError exception.
  111. if (!target.is_object())
  112. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  113. // 2. Let key be ? ToPropertyKey(propertyKey).
  114. auto key = TRY(property_key.to_property_key(global_object));
  115. // 3. If receiver is not present, then
  116. if (vm.argument_count() < 3) {
  117. // a. Set receiver to target.
  118. receiver = target;
  119. }
  120. // 4. Return ? target.[[Get]](key, receiver).
  121. return TRY(target.as_object().internal_get(key, receiver));
  122. }
  123. // 28.1.6 Reflect.getOwnPropertyDescriptor ( target, propertyKey ), https://tc39.es/ecma262/#sec-reflect.getownpropertydescriptor
  124. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get_own_property_descriptor)
  125. {
  126. auto target = vm.argument(0);
  127. auto property_key = vm.argument(1);
  128. // 1. If Type(target) is not Object, throw a TypeError exception.
  129. if (!target.is_object())
  130. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  131. // 2. Let key be ? ToPropertyKey(propertyKey).
  132. auto key = TRY(property_key.to_property_key(global_object));
  133. // 3. Let desc be ? target.[[GetOwnProperty]](key).
  134. auto descriptor = TRY(target.as_object().internal_get_own_property(key));
  135. // 4. Return FromPropertyDescriptor(desc).
  136. return from_property_descriptor(global_object, descriptor);
  137. }
  138. // 28.1.7 Reflect.getPrototypeOf ( target ), https://tc39.es/ecma262/#sec-reflect.getprototypeof
  139. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get_prototype_of)
  140. {
  141. auto target = vm.argument(0);
  142. // 1. If Type(target) is not Object, throw a TypeError exception.
  143. if (!target.is_object())
  144. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  145. // 2. Return ? target.[[GetPrototypeOf]]().
  146. return TRY(target.as_object().internal_get_prototype_of());
  147. }
  148. // 28.1.8 Reflect.has ( target, propertyKey ), https://tc39.es/ecma262/#sec-reflect.has
  149. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::has)
  150. {
  151. auto target = vm.argument(0);
  152. auto property_key = vm.argument(1);
  153. // 1. If Type(target) is not Object, throw a TypeError exception.
  154. if (!target.is_object())
  155. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  156. // 2. Let key be ? ToPropertyKey(propertyKey).
  157. auto key = TRY(property_key.to_property_key(global_object));
  158. // 3. Return ? target.[[HasProperty]](key).
  159. return Value(TRY(target.as_object().internal_has_property(key)));
  160. }
  161. // 28.1.9 Reflect.isExtensible ( target ), https://tc39.es/ecma262/#sec-reflect.isextensible
  162. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::is_extensible)
  163. {
  164. auto target = vm.argument(0);
  165. // 1. If Type(target) is not Object, throw a TypeError exception.
  166. if (!target.is_object())
  167. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  168. // 2. Return ? target.[[IsExtensible]]().
  169. return Value(TRY(target.as_object().internal_is_extensible()));
  170. }
  171. // 28.1.10 Reflect.ownKeys ( target ), https://tc39.es/ecma262/#sec-reflect.ownkeys
  172. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::own_keys)
  173. {
  174. auto target = vm.argument(0);
  175. // 1. If Type(target) is not Object, throw a TypeError exception.
  176. if (!target.is_object())
  177. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  178. // 2. Let keys be ? target.[[OwnPropertyKeys]]().
  179. auto keys = TRY(target.as_object().internal_own_property_keys());
  180. // 3. Return CreateArrayFromList(keys).
  181. return Array::create_from(global_object, keys);
  182. }
  183. // 28.1.11 Reflect.preventExtensions ( target ), https://tc39.es/ecma262/#sec-reflect.preventextensions
  184. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::prevent_extensions)
  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. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  190. // 2. Return ? target.[[PreventExtensions]]().
  191. return Value(TRY(target.as_object().internal_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 = vm.argument(0);
  197. auto property_key = vm.argument(1);
  198. auto value = vm.argument(2);
  199. auto receiver = vm.argument(3);
  200. // 1. If Type(target) is not Object, throw a TypeError exception.
  201. if (!target.is_object())
  202. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  203. // 2. Let key be ? ToPropertyKey(propertyKey).
  204. auto key = TRY(property_key.to_property_key(global_object));
  205. // 3. If receiver is not present, then
  206. if (vm.argument_count() < 4) {
  207. // a. Set receiver to target.
  208. receiver = target;
  209. }
  210. // 4. Return ? target.[[Set]](key, V, receiver).
  211. return Value(TRY(target.as_object().internal_set(key, value, receiver)));
  212. }
  213. // 28.1.13 Reflect.setPrototypeOf ( target, proto ), https://tc39.es/ecma262/#sec-reflect.setprototypeof
  214. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set_prototype_of)
  215. {
  216. auto target = vm.argument(0);
  217. auto proto = vm.argument(1);
  218. // 1. If Type(target) is not Object, throw a TypeError exception.
  219. if (!target.is_object())
  220. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects());
  221. // 2. If Type(proto) is not Object and proto is not null, throw a TypeError exception.
  222. if (!proto.is_object() && !proto.is_null())
  223. return vm.throw_completion<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  224. // 3. Return ? target.[[SetPrototypeOf]](proto).
  225. return Value(TRY(target.as_object().internal_set_prototype_of(proto.is_null() ? nullptr : &proto.as_object())));
  226. }
  227. }