ObjectConstructor.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/Function.h>
  28. #include <LibJS/Runtime/Array.h>
  29. #include <LibJS/Runtime/Error.h>
  30. #include <LibJS/Runtime/GlobalObject.h>
  31. #include <LibJS/Runtime/ObjectConstructor.h>
  32. #include <LibJS/Runtime/ProxyObject.h>
  33. #include <LibJS/Runtime/Shape.h>
  34. namespace JS {
  35. ObjectConstructor::ObjectConstructor(GlobalObject& global_object)
  36. : NativeFunction(vm().names.Object, *global_object.function_prototype())
  37. {
  38. }
  39. void ObjectConstructor::initialize(GlobalObject& global_object)
  40. {
  41. auto& vm = this->vm();
  42. NativeFunction::initialize(global_object);
  43. define_property(vm.names.prototype, global_object.object_prototype(), 0);
  44. define_property(vm.names.length, Value(1), Attribute::Configurable);
  45. u8 attr = Attribute::Writable | Attribute::Configurable;
  46. define_native_function(vm.names.defineProperty, define_property_, 3, attr);
  47. define_native_function(vm.names.is, is, 2, attr);
  48. define_native_function(vm.names.getOwnPropertyDescriptor, get_own_property_descriptor, 2, attr);
  49. define_native_function(vm.names.getOwnPropertyNames, get_own_property_names, 1, attr);
  50. define_native_function(vm.names.getPrototypeOf, get_prototype_of, 1, attr);
  51. define_native_function(vm.names.setPrototypeOf, set_prototype_of, 2, attr);
  52. define_native_function(vm.names.isExtensible, is_extensible, 1, attr);
  53. define_native_function(vm.names.isFrozen, is_frozen, 1, attr);
  54. define_native_function(vm.names.isSealed, is_sealed, 1, attr);
  55. define_native_function(vm.names.preventExtensions, prevent_extensions, 1, attr);
  56. define_native_function(vm.names.freeze, freeze, 1, attr);
  57. define_native_function(vm.names.seal, seal, 1, attr);
  58. define_native_function(vm.names.keys, keys, 1, attr);
  59. define_native_function(vm.names.values, values, 1, attr);
  60. define_native_function(vm.names.entries, entries, 1, attr);
  61. }
  62. ObjectConstructor::~ObjectConstructor()
  63. {
  64. }
  65. Value ObjectConstructor::call()
  66. {
  67. auto value = vm().argument(0);
  68. if (value.is_nullish())
  69. return Object::create_empty(global_object());
  70. return value.to_object(global_object());
  71. }
  72. Value ObjectConstructor::construct(Function&)
  73. {
  74. return call();
  75. }
  76. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_names)
  77. {
  78. if (!vm.argument_count())
  79. return {};
  80. auto* object = vm.argument(0).to_object(global_object);
  81. if (vm.exception())
  82. return {};
  83. auto* result = Array::create(global_object);
  84. for (auto& entry : object->indexed_properties())
  85. result->indexed_properties().append(js_string(vm, String::number(entry.index())));
  86. for (auto& it : object->shape().property_table_ordered()) {
  87. if (!it.key.is_string())
  88. continue;
  89. result->indexed_properties().append(js_string(vm, it.key.as_string()));
  90. }
  91. return result;
  92. }
  93. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_prototype_of)
  94. {
  95. if (!vm.argument_count())
  96. return {};
  97. auto* object = vm.argument(0).to_object(global_object);
  98. if (vm.exception())
  99. return {};
  100. return object->prototype();
  101. }
  102. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::set_prototype_of)
  103. {
  104. if (vm.argument_count() < 2) {
  105. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSetPrototypeOfTwoArgs);
  106. return {};
  107. }
  108. auto* object = vm.argument(0).to_object(global_object);
  109. if (vm.exception())
  110. return {};
  111. auto prototype_value = vm.argument(1);
  112. Object* prototype;
  113. if (prototype_value.is_null()) {
  114. prototype = nullptr;
  115. } else if (prototype_value.is_object()) {
  116. prototype = &prototype_value.as_object();
  117. } else {
  118. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  119. return {};
  120. }
  121. if (!object->set_prototype(prototype)) {
  122. if (!vm.exception())
  123. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSetPrototypeOfReturnedFalse);
  124. return {};
  125. }
  126. return object;
  127. }
  128. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_extensible)
  129. {
  130. auto argument = vm.argument(0);
  131. if (!argument.is_object())
  132. return Value(false);
  133. return Value(argument.as_object().is_extensible());
  134. }
  135. // 20.1.2.15 Object.isFrozen, https://tc39.es/ecma262/#sec-object.isfrozen
  136. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_frozen)
  137. {
  138. auto argument = vm.argument(0);
  139. if (!argument.is_object())
  140. return Value(true);
  141. return Value(argument.as_object().test_integrity_level(Object::IntegrityLevel::Frozen));
  142. }
  143. // 20.1.2.16 Object.isSealed, https://tc39.es/ecma262/#sec-object.issealed
  144. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_sealed)
  145. {
  146. auto argument = vm.argument(0);
  147. if (!argument.is_object())
  148. return Value(true);
  149. return Value(argument.as_object().test_integrity_level(Object::IntegrityLevel::Sealed));
  150. }
  151. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::prevent_extensions)
  152. {
  153. auto argument = vm.argument(0);
  154. if (!argument.is_object())
  155. return argument;
  156. auto status = argument.as_object().prevent_extensions();
  157. if (vm.exception())
  158. return {};
  159. if (!status) {
  160. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPreventExtensionsReturnedFalse);
  161. return {};
  162. }
  163. return argument;
  164. }
  165. // 20.1.2.6 Object.freeze, https://tc39.es/ecma262/#sec-object.freeze
  166. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::freeze)
  167. {
  168. auto argument = vm.argument(0);
  169. if (!argument.is_object())
  170. return argument;
  171. auto status = argument.as_object().set_integrity_level(Object::IntegrityLevel::Frozen);
  172. if (vm.exception())
  173. return {};
  174. if (!status) {
  175. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectFreezeFailed);
  176. return {};
  177. }
  178. return argument;
  179. }
  180. // 20.1.2.20 Object.seal, https://tc39.es/ecma262/#sec-object.seal
  181. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::seal)
  182. {
  183. auto argument = vm.argument(0);
  184. if (!argument.is_object())
  185. return argument;
  186. auto status = argument.as_object().set_integrity_level(Object::IntegrityLevel::Sealed);
  187. if (vm.exception())
  188. return {};
  189. if (!status) {
  190. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSealFailed);
  191. return {};
  192. }
  193. return argument;
  194. }
  195. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_descriptor)
  196. {
  197. auto* object = vm.argument(0).to_object(global_object);
  198. if (vm.exception())
  199. return {};
  200. auto property_key = PropertyName::from_value(global_object, vm.argument(1));
  201. if (vm.exception())
  202. return {};
  203. return object->get_own_property_descriptor_object(property_key);
  204. }
  205. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::define_property_)
  206. {
  207. if (!vm.argument(0).is_object()) {
  208. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Object argument");
  209. return {};
  210. }
  211. if (!vm.argument(2).is_object()) {
  212. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Descriptor argument");
  213. return {};
  214. }
  215. auto& object = vm.argument(0).as_object();
  216. auto property_key = StringOrSymbol::from_value(global_object, vm.argument(1));
  217. if (vm.exception())
  218. return {};
  219. auto& descriptor = vm.argument(2).as_object();
  220. if (!object.define_property(property_key, descriptor)) {
  221. if (!vm.exception()) {
  222. if (AK::is<ProxyObject>(object)) {
  223. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectDefinePropertyReturnedFalse);
  224. } else {
  225. vm.throw_exception<TypeError>(global_object, ErrorType::NonExtensibleDefine, property_key.to_display_string());
  226. }
  227. }
  228. return {};
  229. }
  230. return &object;
  231. }
  232. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is)
  233. {
  234. return Value(same_value(vm.argument(0), vm.argument(1)));
  235. }
  236. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::keys)
  237. {
  238. if (!vm.argument_count()) {
  239. vm.throw_exception<TypeError>(global_object, ErrorType::ConvertUndefinedToObject);
  240. return {};
  241. }
  242. auto* obj_arg = vm.argument(0).to_object(global_object);
  243. if (vm.exception())
  244. return {};
  245. return Array::create_from(global_object, obj_arg->get_enumerable_own_property_names(PropertyKind::Key));
  246. }
  247. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::values)
  248. {
  249. if (!vm.argument_count()) {
  250. vm.throw_exception<TypeError>(global_object, ErrorType::ConvertUndefinedToObject);
  251. return {};
  252. }
  253. auto* obj_arg = vm.argument(0).to_object(global_object);
  254. if (vm.exception())
  255. return {};
  256. return Array::create_from(global_object, obj_arg->get_enumerable_own_property_names(PropertyKind::Value));
  257. }
  258. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::entries)
  259. {
  260. if (!vm.argument_count()) {
  261. vm.throw_exception<TypeError>(global_object, ErrorType::ConvertUndefinedToObject);
  262. return {};
  263. }
  264. auto* obj_arg = vm.argument(0).to_object(global_object);
  265. if (vm.exception())
  266. return {};
  267. return Array::create_from(global_object, obj_arg->get_enumerable_own_property_names(PropertyKind::KeyAndValue));
  268. }
  269. }