ReflectObject.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2020, 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);
  135. auto property_name = PropertyName::from_value(global_object, property_key);
  136. if (vm.exception())
  137. return {};
  138. auto property_key_number = property_key.to_number(global_object);
  139. if (vm.exception())
  140. return {};
  141. if (property_key_number.is_finite_number()) {
  142. auto property_key_as_double = property_key_number.as_double();
  143. if (property_key_as_double >= 0 && (i32)property_key_as_double == property_key_as_double)
  144. property_name = PropertyName(property_key_as_double);
  145. }
  146. return Value(target->delete_property(property_name));
  147. }
  148. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get)
  149. {
  150. auto* target = get_target_object_from(global_object, "get");
  151. if (!target)
  152. return {};
  153. auto property_key = PropertyName::from_value(global_object, vm.argument(1));
  154. if (vm.exception())
  155. return {};
  156. Value receiver = {};
  157. if (vm.argument_count() > 2)
  158. receiver = vm.argument(2);
  159. return target->get(property_key, receiver).value_or(js_undefined());
  160. }
  161. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get_own_property_descriptor)
  162. {
  163. auto* target = get_target_object_from(global_object, "getOwnPropertyDescriptor");
  164. if (!target)
  165. return {};
  166. auto property_key = PropertyName::from_value(global_object, vm.argument(1));
  167. if (vm.exception())
  168. return {};
  169. return target->get_own_property_descriptor_object(property_key);
  170. }
  171. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get_prototype_of)
  172. {
  173. auto* target = get_target_object_from(global_object, "getPrototypeOf");
  174. if (!target)
  175. return {};
  176. return target->prototype();
  177. }
  178. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::has)
  179. {
  180. auto* target = get_target_object_from(global_object, "has");
  181. if (!target)
  182. return {};
  183. auto property_key = PropertyName::from_value(global_object, vm.argument(1));
  184. if (vm.exception())
  185. return {};
  186. return Value(target->has_property(property_key));
  187. }
  188. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::is_extensible)
  189. {
  190. auto* target = get_target_object_from(global_object, "isExtensible");
  191. if (!target)
  192. return {};
  193. return Value(target->is_extensible());
  194. }
  195. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::own_keys)
  196. {
  197. auto* target = get_target_object_from(global_object, "ownKeys");
  198. if (!target)
  199. return {};
  200. return Array::create_from(global_object, target->get_own_properties(PropertyKind::Key));
  201. }
  202. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::prevent_extensions)
  203. {
  204. auto* target = get_target_object_from(global_object, "preventExtensions");
  205. if (!target)
  206. return {};
  207. return Value(target->prevent_extensions());
  208. }
  209. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set)
  210. {
  211. auto* target = get_target_object_from(global_object, "set");
  212. if (!target)
  213. return {};
  214. auto property_key = vm.argument(1).to_string(global_object);
  215. if (vm.exception())
  216. return {};
  217. auto value = vm.argument(2);
  218. Value receiver = {};
  219. if (vm.argument_count() > 3)
  220. receiver = vm.argument(3);
  221. return Value(target->put(property_key, value, receiver));
  222. }
  223. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set_prototype_of)
  224. {
  225. auto* target = get_target_object_from(global_object, "setPrototypeOf");
  226. if (!target)
  227. return {};
  228. auto prototype_value = vm.argument(1);
  229. if (!prototype_value.is_object() && !prototype_value.is_null()) {
  230. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  231. return {};
  232. }
  233. Object* prototype = nullptr;
  234. if (!prototype_value.is_null())
  235. prototype = const_cast<Object*>(&prototype_value.as_object());
  236. return Value(target->set_prototype(prototype));
  237. }
  238. }