ReflectObject.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Function.h>
  27. #include <LibJS/Runtime/Error.h>
  28. #include <LibJS/Runtime/Function.h>
  29. #include <LibJS/Runtime/GlobalObject.h>
  30. #include <LibJS/Runtime/NativeFunction.h>
  31. #include <LibJS/Runtime/ReflectObject.h>
  32. namespace JS {
  33. static Object* get_target_object_from(GlobalObject& global_object, const String& name)
  34. {
  35. auto& vm = global_object.vm();
  36. auto target = vm.argument(0);
  37. if (!target.is_object()) {
  38. vm.throw_exception<TypeError>(global_object, ErrorType::ReflectArgumentMustBeAnObject, name);
  39. return nullptr;
  40. }
  41. return static_cast<Object*>(&target.as_object());
  42. }
  43. static Function* get_target_function_from(GlobalObject& global_object, const String& name)
  44. {
  45. auto& vm = global_object.vm();
  46. auto target = vm.argument(0);
  47. if (!target.is_function()) {
  48. vm.throw_exception<TypeError>(global_object, ErrorType::ReflectArgumentMustBeAFunction, name);
  49. return nullptr;
  50. }
  51. return &target.as_function();
  52. }
  53. static void prepare_arguments_list(GlobalObject& global_object, Value value, MarkedValueList* arguments)
  54. {
  55. auto& vm = global_object.vm();
  56. if (!value.is_object()) {
  57. vm.throw_exception<TypeError>(global_object, ErrorType::ReflectBadArgumentsList);
  58. return;
  59. }
  60. auto& arguments_list = value.as_object();
  61. auto length_property = arguments_list.get(vm.names.length);
  62. if (vm.exception())
  63. return;
  64. auto length = length_property.to_size_t(global_object);
  65. if (vm.exception())
  66. return;
  67. for (size_t i = 0; i < length; ++i) {
  68. auto element = arguments_list.get(String::number(i));
  69. if (vm.exception())
  70. return;
  71. arguments->append(element.value_or(js_undefined()));
  72. }
  73. }
  74. ReflectObject::ReflectObject(GlobalObject& global_object)
  75. : Object(*global_object.object_prototype())
  76. {
  77. }
  78. void ReflectObject::initialize(GlobalObject& global_object)
  79. {
  80. auto& vm = this->vm();
  81. Object::initialize(global_object);
  82. u8 attr = Attribute::Writable | Attribute::Configurable;
  83. define_native_function(vm.names.apply, apply, 3, attr);
  84. define_native_function(vm.names.construct, construct, 2, attr);
  85. define_native_function(vm.names.defineProperty, define_property, 3, attr);
  86. define_native_function(vm.names.deleteProperty, delete_property, 2, attr);
  87. define_native_function(vm.names.get, get, 2, attr);
  88. define_native_function(vm.names.getOwnPropertyDescriptor, get_own_property_descriptor, 2, attr);
  89. define_native_function(vm.names.getPrototypeOf, get_prototype_of, 1, attr);
  90. define_native_function(vm.names.has, has, 2, attr);
  91. define_native_function(vm.names.isExtensible, is_extensible, 1, attr);
  92. define_native_function(vm.names.ownKeys, own_keys, 1, attr);
  93. define_native_function(vm.names.preventExtensions, prevent_extensions, 1, attr);
  94. define_native_function(vm.names.set, set, 3, attr);
  95. define_native_function(vm.names.setPrototypeOf, set_prototype_of, 2, attr);
  96. }
  97. ReflectObject::~ReflectObject()
  98. {
  99. }
  100. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::apply)
  101. {
  102. auto* target = get_target_function_from(global_object, "apply");
  103. if (!target)
  104. return {};
  105. auto this_arg = vm.argument(1);
  106. MarkedValueList arguments(vm.heap());
  107. prepare_arguments_list(global_object, vm.argument(2), &arguments);
  108. if (vm.exception())
  109. return {};
  110. return vm.call(*target, this_arg, move(arguments));
  111. }
  112. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::construct)
  113. {
  114. auto* target = get_target_function_from(global_object, "construct");
  115. if (!target)
  116. return {};
  117. MarkedValueList arguments(vm.heap());
  118. prepare_arguments_list(global_object, vm.argument(1), &arguments);
  119. if (vm.exception())
  120. return {};
  121. auto* new_target = target;
  122. if (vm.argument_count() > 2) {
  123. auto new_target_value = vm.argument(2);
  124. if (!new_target_value.is_function()
  125. || (new_target_value.as_object().is_native_function() && !static_cast<NativeFunction&>(new_target_value.as_object()).has_constructor())) {
  126. vm.throw_exception<TypeError>(global_object, ErrorType::ReflectBadNewTarget);
  127. return {};
  128. }
  129. new_target = &new_target_value.as_function();
  130. }
  131. return vm.construct(*target, *new_target, move(arguments), global_object);
  132. }
  133. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::define_property)
  134. {
  135. auto* target = get_target_object_from(global_object, "defineProperty");
  136. if (!target)
  137. return {};
  138. if (!vm.argument(2).is_object()) {
  139. vm.throw_exception<TypeError>(global_object, ErrorType::ReflectBadDescriptorArgument);
  140. return {};
  141. }
  142. auto property_key = StringOrSymbol::from_value(global_object, vm.argument(1));
  143. if (vm.exception())
  144. return {};
  145. auto& descriptor = vm.argument(2).as_object();
  146. auto success = target->define_property(property_key, descriptor, false);
  147. if (vm.exception())
  148. return {};
  149. return Value(success);
  150. }
  151. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::delete_property)
  152. {
  153. auto* target = get_target_object_from(global_object, "deleteProperty");
  154. if (!target)
  155. return {};
  156. auto property_key = vm.argument(1);
  157. auto property_name = PropertyName::from_value(global_object, property_key);
  158. if (vm.exception())
  159. return {};
  160. auto property_key_number = property_key.to_number(global_object);
  161. if (vm.exception())
  162. return {};
  163. if (property_key_number.is_finite_number()) {
  164. auto property_key_as_double = property_key_number.as_double();
  165. if (property_key_as_double >= 0 && (i32)property_key_as_double == property_key_as_double)
  166. property_name = PropertyName(property_key_as_double);
  167. }
  168. return target->delete_property(property_name);
  169. }
  170. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get)
  171. {
  172. auto* target = get_target_object_from(global_object, "get");
  173. if (!target)
  174. return {};
  175. auto property_key = PropertyName::from_value(global_object, vm.argument(1));
  176. if (vm.exception())
  177. return {};
  178. Value receiver = {};
  179. if (vm.argument_count() > 2)
  180. receiver = vm.argument(2);
  181. return target->get(property_key, receiver).value_or(js_undefined());
  182. }
  183. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get_own_property_descriptor)
  184. {
  185. auto* target = get_target_object_from(global_object, "getOwnPropertyDescriptor");
  186. if (!target)
  187. return {};
  188. auto property_key = PropertyName::from_value(global_object, vm.argument(1));
  189. if (vm.exception())
  190. return {};
  191. return target->get_own_property_descriptor_object(property_key);
  192. }
  193. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get_prototype_of)
  194. {
  195. auto* target = get_target_object_from(global_object, "getPrototypeOf");
  196. if (!target)
  197. return {};
  198. return target->prototype();
  199. }
  200. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::has)
  201. {
  202. auto* target = get_target_object_from(global_object, "has");
  203. if (!target)
  204. return {};
  205. auto property_key = PropertyName::from_value(global_object, vm.argument(1));
  206. if (vm.exception())
  207. return {};
  208. return Value(target->has_property(property_key));
  209. }
  210. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::is_extensible)
  211. {
  212. auto* target = get_target_object_from(global_object, "isExtensible");
  213. if (!target)
  214. return {};
  215. return Value(target->is_extensible());
  216. }
  217. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::own_keys)
  218. {
  219. auto* target = get_target_object_from(global_object, "ownKeys");
  220. if (!target)
  221. return {};
  222. return target->get_own_properties(*target, PropertyKind::Key);
  223. }
  224. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::prevent_extensions)
  225. {
  226. auto* target = get_target_object_from(global_object, "preventExtensions");
  227. if (!target)
  228. return {};
  229. return Value(target->prevent_extensions());
  230. }
  231. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set)
  232. {
  233. auto* target = get_target_object_from(global_object, "set");
  234. if (!target)
  235. return {};
  236. auto property_key = vm.argument(1).to_string(global_object);
  237. if (vm.exception())
  238. return {};
  239. auto value = vm.argument(2);
  240. Value receiver = {};
  241. if (vm.argument_count() > 3)
  242. receiver = vm.argument(3);
  243. return Value(target->put(property_key, value, receiver));
  244. }
  245. JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set_prototype_of)
  246. {
  247. auto* target = get_target_object_from(global_object, "setPrototypeOf");
  248. if (!target)
  249. return {};
  250. auto prototype_value = vm.argument(1);
  251. if (!prototype_value.is_object() && !prototype_value.is_null()) {
  252. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  253. return {};
  254. }
  255. Object* prototype = nullptr;
  256. if (!prototype_value.is_null())
  257. prototype = const_cast<Object*>(&prototype_value.as_object());
  258. return Value(target->set_prototype(prototype));
  259. }
  260. }