ReflectObject.cpp 10 KB

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