ObjectConstructor.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  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/Heap/Heap.h>
  28. #include <LibJS/Interpreter.h>
  29. #include <LibJS/Runtime/Array.h>
  30. #include <LibJS/Runtime/Error.h>
  31. #include <LibJS/Runtime/GlobalObject.h>
  32. #include <LibJS/Runtime/ObjectConstructor.h>
  33. #include <LibJS/Runtime/Shape.h>
  34. namespace JS {
  35. ObjectConstructor::ObjectConstructor(GlobalObject& global_object)
  36. : NativeFunction("Object", *global_object.function_prototype())
  37. {
  38. }
  39. void ObjectConstructor::initialize(GlobalObject& global_object)
  40. {
  41. NativeFunction::initialize(global_object);
  42. define_property("prototype", global_object.object_prototype(), 0);
  43. define_property("length", Value(1), Attribute::Configurable);
  44. u8 attr = Attribute::Writable | Attribute::Configurable;
  45. define_native_function("defineProperty", define_property_, 3, attr);
  46. define_native_function("is", is, 2, attr);
  47. define_native_function("getOwnPropertyDescriptor", get_own_property_descriptor, 2, attr);
  48. define_native_function("getOwnPropertyNames", get_own_property_names, 1, attr);
  49. define_native_function("getPrototypeOf", get_prototype_of, 1, attr);
  50. define_native_function("setPrototypeOf", set_prototype_of, 2, attr);
  51. define_native_function("isExtensible", is_extensible, 1, attr);
  52. define_native_function("preventExtensions", prevent_extensions, 1, attr);
  53. define_native_function("keys", keys, 1, attr);
  54. define_native_function("values", values, 1, attr);
  55. define_native_function("entries", entries, 1, attr);
  56. }
  57. ObjectConstructor::~ObjectConstructor()
  58. {
  59. }
  60. Value ObjectConstructor::call()
  61. {
  62. return Object::create_empty(global_object());
  63. }
  64. Value ObjectConstructor::construct(Interpreter&, Function&)
  65. {
  66. return call();
  67. }
  68. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_names)
  69. {
  70. if (!interpreter.argument_count())
  71. return {};
  72. auto* object = interpreter.argument(0).to_object(interpreter, global_object);
  73. if (interpreter.exception())
  74. return {};
  75. auto* result = Array::create(global_object);
  76. for (auto& entry : object->indexed_properties())
  77. result->indexed_properties().append(js_string(interpreter, String::number(entry.index())));
  78. for (auto& it : object->shape().property_table_ordered()) {
  79. if (!it.key.is_string())
  80. continue;
  81. result->indexed_properties().append(js_string(interpreter, it.key.as_string()));
  82. }
  83. return result;
  84. }
  85. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_prototype_of)
  86. {
  87. if (!interpreter.argument_count())
  88. return {};
  89. auto* object = interpreter.argument(0).to_object(interpreter, global_object);
  90. if (interpreter.exception())
  91. return {};
  92. return object->prototype();
  93. }
  94. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::set_prototype_of)
  95. {
  96. if (interpreter.argument_count() < 2) {
  97. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::ObjectSetPrototypeOfTwoArgs);
  98. return {};
  99. }
  100. auto* object = interpreter.argument(0).to_object(interpreter, global_object);
  101. if (interpreter.exception())
  102. return {};
  103. auto prototype_value = interpreter.argument(1);
  104. Object* prototype;
  105. if (prototype_value.is_null()) {
  106. prototype = nullptr;
  107. } else if (prototype_value.is_object()) {
  108. prototype = &prototype_value.as_object();
  109. } else {
  110. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  111. return {};
  112. }
  113. if (!object->set_prototype(prototype)) {
  114. if (!interpreter.exception())
  115. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::ObjectSetPrototypeOfReturnedFalse);
  116. return {};
  117. }
  118. return object;
  119. }
  120. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_extensible)
  121. {
  122. auto argument = interpreter.argument(0);
  123. if (!argument.is_object())
  124. return Value(false);
  125. return Value(argument.as_object().is_extensible());
  126. }
  127. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::prevent_extensions)
  128. {
  129. auto argument = interpreter.argument(0);
  130. if (!argument.is_object())
  131. return argument;
  132. if (!argument.as_object().prevent_extensions()) {
  133. if (!interpreter.exception())
  134. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::ObjectPreventExtensionsReturnedFalse);
  135. return {};
  136. }
  137. return argument;
  138. }
  139. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_descriptor)
  140. {
  141. auto* object = interpreter.argument(0).to_object(interpreter, global_object);
  142. if (interpreter.exception())
  143. return {};
  144. auto property_key = PropertyName::from_value(interpreter, interpreter.argument(1));
  145. if (interpreter.exception())
  146. return {};
  147. return object->get_own_property_descriptor_object(property_key);
  148. }
  149. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::define_property_)
  150. {
  151. if (!interpreter.argument(0).is_object()) {
  152. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Object argument");
  153. return {};
  154. }
  155. if (!interpreter.argument(2).is_object()) {
  156. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Descriptor argument");
  157. return {};
  158. }
  159. auto& object = interpreter.argument(0).as_object();
  160. auto property_key = StringOrSymbol::from_value(interpreter, interpreter.argument(1));
  161. if (interpreter.exception())
  162. return {};
  163. auto& descriptor = interpreter.argument(2).as_object();
  164. if (!object.define_property(property_key, descriptor)) {
  165. if (!interpreter.exception()) {
  166. if (object.is_proxy_object()) {
  167. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::ObjectDefinePropertyReturnedFalse);
  168. } else {
  169. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::NonExtensibleDefine, property_key.to_display_string().characters());
  170. }
  171. }
  172. return {};
  173. }
  174. return &object;
  175. }
  176. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is)
  177. {
  178. return Value(same_value(interpreter, interpreter.argument(0), interpreter.argument(1)));
  179. }
  180. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::keys)
  181. {
  182. if (!interpreter.argument_count()) {
  183. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::ConvertUndefinedToObject);
  184. return {};
  185. }
  186. auto* obj_arg = interpreter.argument(0).to_object(interpreter, global_object);
  187. if (interpreter.exception())
  188. return {};
  189. return obj_arg->get_own_properties(*obj_arg, PropertyKind::Key, true);
  190. }
  191. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::values)
  192. {
  193. if (!interpreter.argument_count()) {
  194. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::ConvertUndefinedToObject);
  195. return {};
  196. }
  197. auto* obj_arg = interpreter.argument(0).to_object(interpreter, global_object);
  198. if (interpreter.exception())
  199. return {};
  200. return obj_arg->get_own_properties(*obj_arg, PropertyKind::Value, true);
  201. }
  202. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::entries)
  203. {
  204. if (!interpreter.argument_count()) {
  205. interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::ConvertUndefinedToObject);
  206. return {};
  207. }
  208. auto* obj_arg = interpreter.argument(0).to_object(interpreter, global_object);
  209. if (interpreter.exception())
  210. return {};
  211. return obj_arg->get_own_properties(*obj_arg, PropertyKind::KeyAndValue, true);
  212. }
  213. }