ReflectObject.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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>(String::format("First argument of Reflect.%s() must be an object", 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>(String::format("First argument of Reflect.%s() must be a function", 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>("Arguments list must be an object");
  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();
  63. for (size_t i = 0; i < length; ++i) {
  64. auto element = arguments_list.get(String::number(i));
  65. if (interpreter.exception())
  66. return;
  67. arguments->append(element.value_or(js_undefined()));
  68. }
  69. }
  70. ReflectObject::ReflectObject()
  71. : Object(interpreter().global_object().object_prototype())
  72. {
  73. u8 attr = Attribute::Writable | Attribute::Configurable;
  74. put_native_function("apply", apply, 3, attr);
  75. put_native_function("construct", construct, 2, attr);
  76. put_native_function("defineProperty", define_property, 3, attr);
  77. put_native_function("deleteProperty", delete_property, 2, attr);
  78. put_native_function("get", get, 2, attr);
  79. put_native_function("getOwnPropertyDescriptor", get_own_property_descriptor, 2, attr);
  80. put_native_function("getPrototypeOf", get_prototype_of, 1, attr);
  81. put_native_function("has", has, 2, attr);
  82. put_native_function("isExtensible", is_extensible, 1, attr);
  83. put_native_function("ownKeys", own_keys, 1, attr);
  84. put_native_function("preventExtensions", prevent_extensions, 1, attr);
  85. put_native_function("set", set, 3, attr);
  86. put_native_function("setPrototypeOf", set_prototype_of, 2, attr);
  87. }
  88. ReflectObject::~ReflectObject()
  89. {
  90. }
  91. Value ReflectObject::apply(Interpreter& interpreter)
  92. {
  93. auto* target = get_target_function_from(interpreter, "apply");
  94. if (!target)
  95. return {};
  96. auto this_arg = interpreter.argument(1);
  97. MarkedValueList arguments(interpreter.heap());
  98. prepare_arguments_list(interpreter, interpreter.argument(2), &arguments);
  99. if (interpreter.exception())
  100. return {};
  101. return interpreter.call(*target, this_arg, move(arguments));
  102. }
  103. Value ReflectObject::construct(Interpreter& interpreter)
  104. {
  105. auto* target = get_target_function_from(interpreter, "construct");
  106. if (!target)
  107. return {};
  108. MarkedValueList arguments(interpreter.heap());
  109. prepare_arguments_list(interpreter, interpreter.argument(1), &arguments);
  110. if (interpreter.exception())
  111. return {};
  112. auto* new_target = target;
  113. if (interpreter.argument_count() > 2) {
  114. auto new_target_value = interpreter.argument(2);
  115. if (!new_target_value.is_function()
  116. || (new_target_value.as_object().is_native_function() && !static_cast<NativeFunction&>(new_target_value.as_object()).has_constructor())) {
  117. interpreter.throw_exception<TypeError>("Optional third argument of Reflect.construct() must be a constructor");
  118. return {};
  119. }
  120. new_target = &new_target_value.as_function();
  121. }
  122. return interpreter.construct(*target, *new_target, move(arguments));
  123. }
  124. Value ReflectObject::define_property(Interpreter& interpreter)
  125. {
  126. auto* target = get_target_object_from(interpreter, "defineProperty");
  127. if (!target)
  128. return {};
  129. if (!interpreter.argument(2).is_object())
  130. return interpreter.throw_exception<TypeError>("Descriptor argument is not an object");
  131. auto property_key = interpreter.argument(1).to_string();
  132. auto& descriptor = interpreter.argument(2).as_object();
  133. auto success = target->define_property(property_key, descriptor, false);
  134. return Value(success);
  135. }
  136. Value ReflectObject::delete_property(Interpreter& interpreter)
  137. {
  138. auto* target = get_target_object_from(interpreter, "deleteProperty");
  139. if (!target)
  140. return {};
  141. auto property_key = interpreter.argument(1);
  142. PropertyName property = PropertyName(property_key.to_string());
  143. if (property_key.to_number().is_finite_number()) {
  144. auto property_key_as_double = property_key.to_double();
  145. if (property_key_as_double >= 0 && (i32)property_key_as_double == property_key_as_double)
  146. property = PropertyName(property_key_as_double);
  147. }
  148. return target->delete_property(property);
  149. }
  150. Value ReflectObject::get(Interpreter& interpreter)
  151. {
  152. // FIXME: There's a third argument, receiver, for getters - use it once we have those.
  153. auto* target = get_target_object_from(interpreter, "get");
  154. if (!target)
  155. return {};
  156. auto property_key = interpreter.argument(1).to_string();
  157. return target->get(property_key).value_or(js_undefined());
  158. }
  159. Value ReflectObject::get_own_property_descriptor(Interpreter& interpreter)
  160. {
  161. auto* target = get_target_object_from(interpreter, "getOwnPropertyDescriptor");
  162. if (!target)
  163. return {};
  164. auto property_key = interpreter.argument(1).to_string();
  165. return target->get_own_property_descriptor(property_key);
  166. }
  167. Value ReflectObject::get_prototype_of(Interpreter& interpreter)
  168. {
  169. auto* target = get_target_object_from(interpreter, "getPrototypeOf");
  170. if (!target)
  171. return {};
  172. return target->prototype();
  173. }
  174. Value ReflectObject::has(Interpreter& interpreter)
  175. {
  176. auto* target = get_target_object_from(interpreter, "has");
  177. if (!target)
  178. return {};
  179. auto property_key = interpreter.argument(1).to_string();
  180. return Value(target->has_property(property_key));
  181. }
  182. Value ReflectObject::is_extensible(Interpreter&)
  183. {
  184. // FIXME: For this to be useful we need one of these:
  185. // Object.seal(), Object.freeze(), Reflect.preventExtensions()
  186. // For now we just return true, as that's always the case.
  187. return Value(true);
  188. }
  189. Value ReflectObject::own_keys(Interpreter& interpreter)
  190. {
  191. auto* target = get_target_object_from(interpreter, "ownKeys");
  192. if (!target)
  193. return {};
  194. return target->get_own_properties(*target, GetOwnPropertyMode::Key);
  195. }
  196. Value ReflectObject::prevent_extensions(Interpreter&)
  197. {
  198. // FIXME: Implement me :^)
  199. ASSERT_NOT_REACHED();
  200. }
  201. Value ReflectObject::set(Interpreter& interpreter)
  202. {
  203. // FIXME: There's a fourth argument, receiver, for setters - use it once we have those.
  204. auto* target = get_target_object_from(interpreter, "set");
  205. if (!target)
  206. return {};
  207. auto property_key = interpreter.argument(1).to_string();
  208. auto value = interpreter.argument(2);
  209. return Value(target->put(property_key, value));
  210. }
  211. Value ReflectObject::set_prototype_of(Interpreter& interpreter)
  212. {
  213. auto* target = get_target_object_from(interpreter, "setPrototypeOf");
  214. if (!target)
  215. return {};
  216. auto prototype_value = interpreter.argument(1);
  217. if (!prototype_value.is_object() && !prototype_value.is_null()) {
  218. interpreter.throw_exception<TypeError>("Prototype must be an object or null");
  219. return {};
  220. }
  221. Object* prototype = nullptr;
  222. if (!prototype_value.is_null())
  223. prototype = const_cast<Object*>(&prototype_value.as_object());
  224. target->set_prototype(prototype);
  225. // FIXME: Needs to return false for prototype chain cycles and non-extensible objects (don't have those yet).
  226. return Value(true);
  227. }
  228. }