ReflectObject.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. }
  75. ReflectObject::~ReflectObject()
  76. {
  77. }
  78. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::apply)
  79. {
  80. auto* target = get_target_function_from(global_object, "apply");
  81. if (!target)
  82. return {};
  83. auto this_arg = vm.argument(1);
  84. MarkedValueList arguments(vm.heap());
  85. prepare_arguments_list(global_object, vm.argument(2), arguments);
  86. if (vm.exception())
  87. return {};
  88. return vm.call(*target, this_arg, move(arguments));
  89. }
  90. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::construct)
  91. {
  92. auto* target = get_target_function_from(global_object, "construct");
  93. if (!target)
  94. return {};
  95. MarkedValueList arguments(vm.heap());
  96. prepare_arguments_list(global_object, vm.argument(1), arguments);
  97. if (vm.exception())
  98. return {};
  99. auto* new_target = target;
  100. if (vm.argument_count() > 2) {
  101. auto new_target_value = vm.argument(2);
  102. if (!new_target_value.is_function()
  103. || (is<NativeFunction>(new_target_value.as_object()) && !static_cast<NativeFunction&>(new_target_value.as_object()).has_constructor())) {
  104. vm.throw_exception<TypeError>(global_object, ErrorType::ReflectBadNewTarget);
  105. return {};
  106. }
  107. new_target = &new_target_value.as_function();
  108. }
  109. return vm.construct(*target, *new_target, move(arguments), global_object);
  110. }
  111. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::define_property)
  112. {
  113. auto* target = get_target_object_from(global_object, "defineProperty");
  114. if (!target)
  115. return {};
  116. auto property_key = vm.argument(1).to_property_key(global_object);
  117. if (vm.exception())
  118. return {};
  119. if (!vm.argument(2).is_object()) {
  120. vm.throw_exception<TypeError>(global_object, ErrorType::ReflectBadDescriptorArgument);
  121. return {};
  122. }
  123. auto& descriptor = vm.argument(2).as_object();
  124. auto success = target->define_property(property_key, descriptor, false);
  125. if (vm.exception())
  126. return {};
  127. return Value(success);
  128. }
  129. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::delete_property)
  130. {
  131. auto* target = get_target_object_from(global_object, "deleteProperty");
  132. if (!target)
  133. return {};
  134. auto property_key = vm.argument(1).to_property_key(global_object);
  135. if (vm.exception())
  136. return {};
  137. return Value(target->delete_property(property_key));
  138. }
  139. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get)
  140. {
  141. auto* target = get_target_object_from(global_object, "get");
  142. if (!target)
  143. return {};
  144. auto property_key = vm.argument(1).to_property_key(global_object);
  145. if (vm.exception())
  146. return {};
  147. Value receiver = {};
  148. if (vm.argument_count() > 2)
  149. receiver = vm.argument(2);
  150. return target->get(property_key, receiver).value_or(js_undefined());
  151. }
  152. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get_own_property_descriptor)
  153. {
  154. auto* target = get_target_object_from(global_object, "getOwnPropertyDescriptor");
  155. if (!target)
  156. return {};
  157. auto property_key = vm.argument(1).to_property_key(global_object);
  158. if (vm.exception())
  159. return {};
  160. return target->get_own_property_descriptor_object(property_key);
  161. }
  162. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get_prototype_of)
  163. {
  164. auto* target = get_target_object_from(global_object, "getPrototypeOf");
  165. if (!target)
  166. return {};
  167. return target->prototype();
  168. }
  169. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::has)
  170. {
  171. auto* target = get_target_object_from(global_object, "has");
  172. if (!target)
  173. return {};
  174. auto property_key = vm.argument(1).to_property_key(global_object);
  175. if (vm.exception())
  176. return {};
  177. return Value(target->has_property(property_key));
  178. }
  179. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::is_extensible)
  180. {
  181. auto* target = get_target_object_from(global_object, "isExtensible");
  182. if (!target)
  183. return {};
  184. return Value(target->is_extensible());
  185. }
  186. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::own_keys)
  187. {
  188. auto* target = get_target_object_from(global_object, "ownKeys");
  189. if (!target)
  190. return {};
  191. return Array::create_from(global_object, target->get_own_properties(PropertyKind::Key));
  192. }
  193. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::prevent_extensions)
  194. {
  195. auto* target = get_target_object_from(global_object, "preventExtensions");
  196. if (!target)
  197. return {};
  198. return Value(target->prevent_extensions());
  199. }
  200. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set)
  201. {
  202. auto* target = get_target_object_from(global_object, "set");
  203. if (!target)
  204. return {};
  205. auto property_key = vm.argument(1).to_property_key(global_object);
  206. if (vm.exception())
  207. return {};
  208. auto value = vm.argument(2);
  209. Value receiver = {};
  210. if (vm.argument_count() > 3)
  211. receiver = vm.argument(3);
  212. return Value(target->put(property_key, value, receiver));
  213. }
  214. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set_prototype_of)
  215. {
  216. auto* target = get_target_object_from(global_object, "setPrototypeOf");
  217. if (!target)
  218. return {};
  219. auto prototype_value = vm.argument(1);
  220. if (!prototype_value.is_object() && !prototype_value.is_null()) {
  221. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  222. return {};
  223. }
  224. Object* prototype = nullptr;
  225. if (!prototype_value.is_null())
  226. prototype = const_cast<Object*>(&prototype_value.as_object());
  227. return Value(target->set_prototype(prototype));
  228. }
  229. }