ReflectObject.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. static void prepare_arguments_list(GlobalObject& global_object, Value value, MarkedValueList& arguments)
  35. {
  36. auto& vm = global_object.vm();
  37. if (!value.is_object()) {
  38. vm.throw_exception<TypeError>(global_object, ErrorType::ReflectBadArgumentsList);
  39. return;
  40. }
  41. auto& arguments_list = value.as_object();
  42. auto length = length_of_array_like(global_object, arguments_list);
  43. if (vm.exception())
  44. return;
  45. for (size_t i = 0; i < length; ++i) {
  46. auto element = arguments_list.get(String::number(i));
  47. if (vm.exception())
  48. return;
  49. arguments.append(element.value_or(js_undefined()));
  50. }
  51. }
  52. ReflectObject::ReflectObject(GlobalObject& global_object)
  53. : Object(*global_object.object_prototype())
  54. {
  55. }
  56. void ReflectObject::initialize(GlobalObject& global_object)
  57. {
  58. auto& vm = this->vm();
  59. Object::initialize(global_object);
  60. u8 attr = Attribute::Writable | Attribute::Configurable;
  61. define_native_function(vm.names.apply, apply, 3, attr);
  62. define_native_function(vm.names.construct, construct, 2, attr);
  63. define_native_function(vm.names.defineProperty, define_property, 3, attr);
  64. define_native_function(vm.names.deleteProperty, delete_property, 2, attr);
  65. define_native_function(vm.names.get, get, 2, attr);
  66. define_native_function(vm.names.getOwnPropertyDescriptor, get_own_property_descriptor, 2, attr);
  67. define_native_function(vm.names.getPrototypeOf, get_prototype_of, 1, attr);
  68. define_native_function(vm.names.has, has, 2, attr);
  69. define_native_function(vm.names.isExtensible, is_extensible, 1, attr);
  70. define_native_function(vm.names.ownKeys, own_keys, 1, attr);
  71. define_native_function(vm.names.preventExtensions, prevent_extensions, 1, attr);
  72. define_native_function(vm.names.set, set, 3, attr);
  73. define_native_function(vm.names.setPrototypeOf, set_prototype_of, 2, attr);
  74. Object::define_property(vm.well_known_symbol_to_string_tag(), js_string(vm.heap(), "Reflect"), Attribute::Configurable);
  75. }
  76. ReflectObject::~ReflectObject()
  77. {
  78. }
  79. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::apply)
  80. {
  81. auto* target = get_target_function_from(global_object, "apply");
  82. if (!target)
  83. return {};
  84. auto this_arg = vm.argument(1);
  85. MarkedValueList arguments(vm.heap());
  86. prepare_arguments_list(global_object, vm.argument(2), arguments);
  87. if (vm.exception())
  88. return {};
  89. return vm.call(*target, this_arg, move(arguments));
  90. }
  91. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::construct)
  92. {
  93. auto* target = get_target_function_from(global_object, "construct");
  94. if (!target)
  95. return {};
  96. MarkedValueList arguments(vm.heap());
  97. prepare_arguments_list(global_object, vm.argument(1), arguments);
  98. if (vm.exception())
  99. return {};
  100. auto* new_target = target;
  101. if (vm.argument_count() > 2) {
  102. auto new_target_value = vm.argument(2);
  103. if (!new_target_value.is_function()
  104. || (is<NativeFunction>(new_target_value.as_object()) && !static_cast<NativeFunction&>(new_target_value.as_object()).has_constructor())) {
  105. vm.throw_exception<TypeError>(global_object, ErrorType::ReflectBadNewTarget);
  106. return {};
  107. }
  108. new_target = &new_target_value.as_function();
  109. }
  110. return vm.construct(*target, *new_target, move(arguments), global_object);
  111. }
  112. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::define_property)
  113. {
  114. auto* target = get_target_object_from(global_object, "defineProperty");
  115. if (!target)
  116. return {};
  117. auto property_key = vm.argument(1).to_property_key(global_object);
  118. if (vm.exception())
  119. return {};
  120. if (!vm.argument(2).is_object()) {
  121. vm.throw_exception<TypeError>(global_object, ErrorType::ReflectBadDescriptorArgument);
  122. return {};
  123. }
  124. auto& descriptor = vm.argument(2).as_object();
  125. auto success = target->define_property(property_key, descriptor, false);
  126. if (vm.exception())
  127. return {};
  128. return Value(success);
  129. }
  130. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::delete_property)
  131. {
  132. auto* target = get_target_object_from(global_object, "deleteProperty");
  133. if (!target)
  134. return {};
  135. auto property_key = vm.argument(1).to_property_key(global_object);
  136. if (vm.exception())
  137. return {};
  138. return Value(target->delete_property(property_key));
  139. }
  140. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get)
  141. {
  142. auto* target = get_target_object_from(global_object, "get");
  143. if (!target)
  144. return {};
  145. auto property_key = vm.argument(1).to_property_key(global_object);
  146. if (vm.exception())
  147. return {};
  148. Value receiver = {};
  149. if (vm.argument_count() > 2)
  150. receiver = vm.argument(2);
  151. return target->get(property_key, receiver).value_or(js_undefined());
  152. }
  153. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get_own_property_descriptor)
  154. {
  155. auto* target = get_target_object_from(global_object, "getOwnPropertyDescriptor");
  156. if (!target)
  157. return {};
  158. auto property_key = vm.argument(1).to_property_key(global_object);
  159. if (vm.exception())
  160. return {};
  161. return target->get_own_property_descriptor_object(property_key);
  162. }
  163. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get_prototype_of)
  164. {
  165. auto* target = get_target_object_from(global_object, "getPrototypeOf");
  166. if (!target)
  167. return {};
  168. return target->prototype();
  169. }
  170. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::has)
  171. {
  172. auto* target = get_target_object_from(global_object, "has");
  173. if (!target)
  174. return {};
  175. auto property_key = vm.argument(1).to_property_key(global_object);
  176. if (vm.exception())
  177. return {};
  178. return Value(target->has_property(property_key));
  179. }
  180. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::is_extensible)
  181. {
  182. auto* target = get_target_object_from(global_object, "isExtensible");
  183. if (!target)
  184. return {};
  185. return Value(target->is_extensible());
  186. }
  187. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::own_keys)
  188. {
  189. auto* target = get_target_object_from(global_object, "ownKeys");
  190. if (!target)
  191. return {};
  192. return Array::create_from(global_object, target->get_own_properties(PropertyKind::Key));
  193. }
  194. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::prevent_extensions)
  195. {
  196. auto* target = get_target_object_from(global_object, "preventExtensions");
  197. if (!target)
  198. return {};
  199. return Value(target->prevent_extensions());
  200. }
  201. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set)
  202. {
  203. auto* target = get_target_object_from(global_object, "set");
  204. if (!target)
  205. return {};
  206. auto property_key = vm.argument(1).to_property_key(global_object);
  207. if (vm.exception())
  208. return {};
  209. auto value = vm.argument(2);
  210. Value receiver = {};
  211. if (vm.argument_count() > 3)
  212. receiver = vm.argument(3);
  213. return Value(target->put(property_key, value, receiver));
  214. }
  215. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set_prototype_of)
  216. {
  217. auto* target = get_target_object_from(global_object, "setPrototypeOf");
  218. if (!target)
  219. return {};
  220. auto prototype_value = vm.argument(1);
  221. if (!prototype_value.is_object() && !prototype_value.is_null()) {
  222. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  223. return {};
  224. }
  225. Object* prototype = nullptr;
  226. if (!prototype_value.is_null())
  227. prototype = const_cast<Object*>(&prototype_value.as_object());
  228. return Value(target->set_prototype(prototype));
  229. }
  230. }