ReflectObject.cpp 12 KB

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