ObjectConstructor.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Function.h>
  8. #include <LibJS/Runtime/AbstractOperations.h>
  9. #include <LibJS/Runtime/Array.h>
  10. #include <LibJS/Runtime/Error.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. #include <LibJS/Runtime/IteratorOperations.h>
  13. #include <LibJS/Runtime/ObjectConstructor.h>
  14. #include <LibJS/Runtime/ProxyObject.h>
  15. #include <LibJS/Runtime/Shape.h>
  16. namespace JS {
  17. ObjectConstructor::ObjectConstructor(GlobalObject& global_object)
  18. : NativeFunction(vm().names.Object.as_string(), *global_object.function_prototype())
  19. {
  20. }
  21. void ObjectConstructor::initialize(GlobalObject& global_object)
  22. {
  23. auto& vm = this->vm();
  24. NativeFunction::initialize(global_object);
  25. // 20.1.2.19 Object.prototype, https://tc39.es/ecma262/#sec-object.prototype
  26. define_direct_property(vm.names.prototype, global_object.object_prototype(), 0);
  27. u8 attr = Attribute::Writable | Attribute::Configurable;
  28. define_native_function(vm.names.defineProperty, define_property, 3, attr);
  29. define_native_function(vm.names.defineProperties, define_properties, 2, attr);
  30. define_native_function(vm.names.is, is, 2, attr);
  31. define_native_function(vm.names.getOwnPropertyDescriptor, get_own_property_descriptor, 2, attr);
  32. define_native_function(vm.names.getOwnPropertyDescriptors, get_own_property_descriptors, 1, attr);
  33. define_native_function(vm.names.getOwnPropertyNames, get_own_property_names, 1, attr);
  34. define_native_function(vm.names.getOwnPropertySymbols, get_own_property_symbols, 1, attr);
  35. define_native_function(vm.names.getPrototypeOf, get_prototype_of, 1, attr);
  36. define_native_function(vm.names.setPrototypeOf, set_prototype_of, 2, attr);
  37. define_native_function(vm.names.isExtensible, is_extensible, 1, attr);
  38. define_native_function(vm.names.isFrozen, is_frozen, 1, attr);
  39. define_native_function(vm.names.isSealed, is_sealed, 1, attr);
  40. define_native_function(vm.names.preventExtensions, prevent_extensions, 1, attr);
  41. define_native_function(vm.names.freeze, freeze, 1, attr);
  42. define_native_function(vm.names.fromEntries, from_entries, 1, attr);
  43. define_native_function(vm.names.seal, seal, 1, attr);
  44. define_native_function(vm.names.keys, keys, 1, attr);
  45. define_native_function(vm.names.values, values, 1, attr);
  46. define_native_function(vm.names.entries, entries, 1, attr);
  47. define_native_function(vm.names.create, create, 2, attr);
  48. define_native_function(vm.names.hasOwn, has_own, 2, attr);
  49. define_native_function(vm.names.assign, assign, 2, attr);
  50. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  51. }
  52. // 20.1.1.1 Object ( [ value ] ), https://tc39.es/ecma262/#sec-object-value
  53. ThrowCompletionOr<Value> ObjectConstructor::call()
  54. {
  55. return TRY(construct(*this));
  56. }
  57. // 20.1.1.1 Object ( [ value ] ), https://tc39.es/ecma262/#sec-object-value
  58. ThrowCompletionOr<Object*> ObjectConstructor::construct(FunctionObject& new_target)
  59. {
  60. auto& vm = this->vm();
  61. auto& global_object = this->global_object();
  62. if (&new_target != this)
  63. return TRY(ordinary_create_from_constructor<Object>(global_object, new_target, &GlobalObject::object_prototype));
  64. auto value = vm.argument(0);
  65. if (value.is_nullish())
  66. return Object::create(global_object, global_object.object_prototype());
  67. return value.to_object(global_object);
  68. }
  69. enum class GetOwnPropertyKeysType {
  70. String,
  71. Symbol,
  72. };
  73. // 20.1.2.11.1 GetOwnPropertyKeys ( O, type ), https://tc39.es/ecma262/#sec-getownpropertykeys
  74. static ThrowCompletionOr<MarkedVector<Value>> get_own_property_keys(GlobalObject& global_object, Value value, GetOwnPropertyKeysType type)
  75. {
  76. auto& vm = global_object.vm();
  77. // 1. Let obj be ? ToObject(O).
  78. auto* object = TRY(value.to_object(global_object));
  79. // 2. Let keys be ? obj.[[OwnPropertyKeys]]().
  80. auto keys = TRY(object->internal_own_property_keys());
  81. // 3. Let nameList be a new empty List.
  82. auto name_list = MarkedVector<Value> { vm.heap() };
  83. // 4. For each element nextKey of keys, do
  84. for (auto& next_key : keys) {
  85. // a. If Type(nextKey) is Symbol and type is symbol or Type(nextKey) is String and type is string, then
  86. if ((next_key.is_symbol() && type == GetOwnPropertyKeysType::Symbol) || (next_key.is_string() && type == GetOwnPropertyKeysType::String)) {
  87. // i. Append nextKey as the last element of nameList.
  88. name_list.append(next_key);
  89. }
  90. }
  91. // 5. Return nameList.
  92. return { move(name_list) };
  93. }
  94. // 20.1.2.10 Object.getOwnPropertyNames ( O ), https://tc39.es/ecma262/#sec-object.getownpropertynames
  95. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_names)
  96. {
  97. // 1. Return CreateArrayFromList(? GetOwnPropertyKeys(O, string)).
  98. return Array::create_from(global_object, TRY(get_own_property_keys(global_object, vm.argument(0), GetOwnPropertyKeysType::String)));
  99. }
  100. // 20.1.2.11 Object.getOwnPropertySymbols ( O ), https://tc39.es/ecma262/#sec-object.getownpropertysymbols
  101. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_symbols)
  102. {
  103. // 1. Return CreateArrayFromList(? GetOwnPropertyKeys(O, symbol)).
  104. return Array::create_from(global_object, TRY(get_own_property_keys(global_object, vm.argument(0), GetOwnPropertyKeysType::Symbol)));
  105. }
  106. // 20.1.2.12 Object.getPrototypeOf ( O ), https://tc39.es/ecma262/#sec-object.getprototypeof
  107. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_prototype_of)
  108. {
  109. // 1. Let obj be ? ToObject(O).
  110. auto* object = TRY(vm.argument(0).to_object(global_object));
  111. // 2. Return ? obj.[[GetPrototypeOf]]().
  112. return TRY(object->internal_get_prototype_of());
  113. }
  114. // 20.1.2.22 Object.setPrototypeOf ( O, proto ), https://tc39.es/ecma262/#sec-object.setprototypeof
  115. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::set_prototype_of)
  116. {
  117. auto proto = vm.argument(1);
  118. // 1. Set O to ? RequireObjectCoercible(O).
  119. auto object = TRY(require_object_coercible(global_object, vm.argument(0)));
  120. // 2. If Type(proto) is neither Object nor Null, throw a TypeError exception.
  121. if (!proto.is_object() && !proto.is_null())
  122. return vm.throw_completion<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  123. // 3. If Type(O) is not Object, return O.
  124. if (!object.is_object())
  125. return object;
  126. // 4. Let status be ? O.[[SetPrototypeOf]](proto).
  127. auto status = TRY(object.as_object().internal_set_prototype_of(proto.is_null() ? nullptr : &proto.as_object()));
  128. // 5. If status is false, throw a TypeError exception.
  129. if (!status) {
  130. // FIXME: Improve/contextualize error message
  131. return vm.throw_completion<TypeError>(global_object, ErrorType::ObjectSetPrototypeOfReturnedFalse);
  132. }
  133. // 6. Return O.
  134. return object;
  135. }
  136. // 20.1.2.15 Object.isExtensible ( O ), https://tc39.es/ecma262/#sec-object.isextensible
  137. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_extensible)
  138. {
  139. auto argument = vm.argument(0);
  140. if (!argument.is_object())
  141. return Value(false);
  142. return Value(TRY(argument.as_object().is_extensible()));
  143. }
  144. // 20.1.2.16 Object.isFrozen ( O ), https://tc39.es/ecma262/#sec-object.isfrozen
  145. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_frozen)
  146. {
  147. auto argument = vm.argument(0);
  148. if (!argument.is_object())
  149. return Value(true);
  150. return Value(TRY(argument.as_object().test_integrity_level(Object::IntegrityLevel::Frozen)));
  151. }
  152. // 20.1.2.17 Object.isSealed ( O ), https://tc39.es/ecma262/#sec-object.issealed
  153. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_sealed)
  154. {
  155. auto argument = vm.argument(0);
  156. if (!argument.is_object())
  157. return Value(true);
  158. return Value(TRY(argument.as_object().test_integrity_level(Object::IntegrityLevel::Sealed)));
  159. }
  160. // 20.1.2.19 Object.preventExtensions ( O ), https://tc39.es/ecma262/#sec-object.preventextensions
  161. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::prevent_extensions)
  162. {
  163. auto argument = vm.argument(0);
  164. if (!argument.is_object())
  165. return argument;
  166. auto status = TRY(argument.as_object().internal_prevent_extensions());
  167. if (!status) {
  168. // FIXME: Improve/contextualize error message
  169. return vm.throw_completion<TypeError>(global_object, ErrorType::ObjectPreventExtensionsReturnedFalse);
  170. }
  171. return argument;
  172. }
  173. // 20.1.2.6 Object.freeze ( O ), https://tc39.es/ecma262/#sec-object.freeze
  174. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::freeze)
  175. {
  176. auto argument = vm.argument(0);
  177. if (!argument.is_object())
  178. return argument;
  179. auto status = TRY(argument.as_object().set_integrity_level(Object::IntegrityLevel::Frozen));
  180. if (!status)
  181. return vm.throw_completion<TypeError>(global_object, ErrorType::ObjectFreezeFailed);
  182. return argument;
  183. }
  184. // 20.1.2.7 Object.fromEntries ( iterable ), https://tc39.es/ecma262/#sec-object.fromentries
  185. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::from_entries)
  186. {
  187. auto iterable = TRY(require_object_coercible(global_object, vm.argument(0)));
  188. auto* object = Object::create(global_object, global_object.object_prototype());
  189. (void)TRY(get_iterator_values(global_object, iterable, [&](Value iterator_value) -> Optional<Completion> {
  190. if (!iterator_value.is_object())
  191. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, String::formatted("Iterator value {}", iterator_value.to_string_without_side_effects()));
  192. auto key = TRY(iterator_value.as_object().get(0));
  193. auto value = TRY(iterator_value.as_object().get(1));
  194. auto property_key = TRY(key.to_property_key(global_object));
  195. MUST(object->create_data_property_or_throw(property_key, value));
  196. return {};
  197. }));
  198. return object;
  199. }
  200. // 20.1.2.21 Object.seal ( O ), https://tc39.es/ecma262/#sec-object.seal
  201. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::seal)
  202. {
  203. auto argument = vm.argument(0);
  204. if (!argument.is_object())
  205. return argument;
  206. auto status = TRY(argument.as_object().set_integrity_level(Object::IntegrityLevel::Sealed));
  207. if (!status)
  208. return vm.throw_completion<TypeError>(global_object, ErrorType::ObjectSealFailed);
  209. return argument;
  210. }
  211. // 20.1.2.8 Object.getOwnPropertyDescriptor ( O, P ), https://tc39.es/ecma262/#sec-object.getownpropertydescriptor
  212. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_descriptor)
  213. {
  214. auto* object = TRY(vm.argument(0).to_object(global_object));
  215. auto key = TRY(vm.argument(1).to_property_key(global_object));
  216. auto descriptor = TRY(object->internal_get_own_property(key));
  217. return from_property_descriptor(global_object, descriptor);
  218. }
  219. // 20.1.2.9 Object.getOwnPropertyDescriptors ( O ), https://tc39.es/ecma262/#sec-object.getownpropertydescriptors
  220. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_descriptors)
  221. {
  222. // 1. Let obj be ? ToObject(O).
  223. auto* object = TRY(vm.argument(0).to_object(global_object));
  224. // 2. Let ownKeys be ? obj.[[OwnPropertyKeys]]().
  225. auto own_keys = TRY(object->internal_own_property_keys());
  226. // 3. Let descriptors be OrdinaryObjectCreate(%Object.prototype%).
  227. auto* descriptors = Object::create(global_object, global_object.object_prototype());
  228. // 4. For each element key of ownKeys, do
  229. for (auto& key : own_keys) {
  230. auto property_key = MUST(PropertyKey::from_value(global_object, key));
  231. // a. Let desc be ? obj.[[GetOwnProperty]](key).
  232. auto desc = TRY(object->internal_get_own_property(property_key));
  233. // b. Let descriptor be FromPropertyDescriptor(desc).
  234. auto descriptor = from_property_descriptor(global_object, desc);
  235. // c. If descriptor is not undefined, perform ! CreateDataPropertyOrThrow(descriptors, key, descriptor).
  236. if (!descriptor.is_undefined())
  237. MUST(descriptors->create_data_property_or_throw(property_key, descriptor));
  238. }
  239. // 5. Return descriptors.
  240. return descriptors;
  241. }
  242. // 20.1.2.4 Object.defineProperty ( O, P, Attributes ), https://tc39.es/ecma262/#sec-object.defineproperty
  243. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::define_property)
  244. {
  245. if (!vm.argument(0).is_object())
  246. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, vm.argument(0).to_string_without_side_effects());
  247. auto key = TRY(vm.argument(1).to_property_key(global_object));
  248. auto descriptor = TRY(to_property_descriptor(global_object, vm.argument(2)));
  249. TRY(vm.argument(0).as_object().define_property_or_throw(key, descriptor));
  250. return vm.argument(0);
  251. }
  252. // 20.1.2.3 Object.defineProperties ( O, Properties ), https://tc39.es/ecma262/#sec-object.defineproperties
  253. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::define_properties)
  254. {
  255. auto object = vm.argument(0);
  256. auto properties = vm.argument(1);
  257. // 1. If Type(O) is not Object, throw a TypeError exception.
  258. if (!object.is_object())
  259. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, "Object argument");
  260. // 2. Return ? ObjectDefineProperties(O, Properties).
  261. return TRY(object.as_object().define_properties(properties));
  262. }
  263. // 20.1.2.14 Object.is ( value1, value2 ), https://tc39.es/ecma262/#sec-object.is
  264. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is)
  265. {
  266. return Value(same_value(vm.argument(0), vm.argument(1)));
  267. }
  268. // 20.1.2.18 Object.keys ( O ), https://tc39.es/ecma262/#sec-object.keys
  269. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::keys)
  270. {
  271. auto* object = TRY(vm.argument(0).to_object(global_object));
  272. auto name_list = TRY(object->enumerable_own_property_names(PropertyKind::Key));
  273. return Array::create_from(global_object, name_list);
  274. }
  275. // 20.1.2.23 Object.values ( O ), https://tc39.es/ecma262/#sec-object.values
  276. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::values)
  277. {
  278. auto* object = TRY(vm.argument(0).to_object(global_object));
  279. auto name_list = TRY(object->enumerable_own_property_names(PropertyKind::Value));
  280. return Array::create_from(global_object, name_list);
  281. }
  282. // 20.1.2.5 Object.entries ( O ), https://tc39.es/ecma262/#sec-object.entries
  283. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::entries)
  284. {
  285. auto* object = TRY(vm.argument(0).to_object(global_object));
  286. auto name_list = TRY(object->enumerable_own_property_names(PropertyKind::KeyAndValue));
  287. return Array::create_from(global_object, name_list);
  288. }
  289. // 20.1.2.2 Object.create ( O, Properties ), https://tc39.es/ecma262/#sec-object.create
  290. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::create)
  291. {
  292. auto proto = vm.argument(0);
  293. auto properties = vm.argument(1);
  294. // 1. If Type(O) is neither Object nor Null, throw a TypeError exception.
  295. if (!proto.is_object() && !proto.is_null())
  296. return vm.throw_completion<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  297. // 2. Let obj be OrdinaryObjectCreate(O).
  298. auto* object = Object::create(global_object, proto.is_null() ? nullptr : &proto.as_object());
  299. // 3. If Properties is not undefined, then
  300. if (!properties.is_undefined()) {
  301. // a. Return ? ObjectDefineProperties(obj, Properties).
  302. return TRY(object->define_properties(properties));
  303. }
  304. // 4. Return obj.
  305. return object;
  306. }
  307. // 20.1.2.13 Object.hasOwn ( O, P ), https://tc39.es/ecma262/#sec-object.hasown
  308. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::has_own)
  309. {
  310. // 1. Let obj be ? ToObject(O).
  311. auto* object = TRY(vm.argument(0).to_object(global_object));
  312. // 2. Let key be ? ToPropertyKey(P).
  313. auto key = TRY(vm.argument(1).to_property_key(global_object));
  314. // 3. Return ? HasOwnProperty(obj, key).
  315. return Value(TRY(object->has_own_property(key)));
  316. }
  317. // 20.1.2.1 Object.assign ( target, ...sources ), https://tc39.es/ecma262/#sec-object.assign
  318. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::assign)
  319. {
  320. // 1. Let to be ? ToObject(target).
  321. auto* to = TRY(vm.argument(0).to_object(global_object));
  322. // 2. If only one argument was passed, return to.
  323. if (vm.argument_count() == 1)
  324. return to;
  325. // 3. For each element nextSource of sources, do
  326. for (size_t i = 1; i < vm.argument_count(); ++i) {
  327. auto next_source = vm.argument(i);
  328. // a. If nextSource is neither undefined nor null, then
  329. if (next_source.is_nullish())
  330. continue;
  331. // i. Let from be ! ToObject(nextSource).
  332. auto* from = MUST(next_source.to_object(global_object));
  333. // ii. Let keys be ? from.[[OwnPropertyKeys]]().
  334. auto keys = TRY(from->internal_own_property_keys());
  335. // iii. For each element nextKey of keys, do
  336. for (auto& next_key : keys) {
  337. auto property_key = MUST(PropertyKey::from_value(global_object, next_key));
  338. // 1. Let desc be ? from.[[GetOwnProperty]](nextKey).
  339. auto desc = TRY(from->internal_get_own_property(property_key));
  340. // 2. If desc is not undefined and desc.[[Enumerable]] is true, then
  341. if (!desc.has_value() || !*desc->enumerable)
  342. continue;
  343. // a. Let propValue be ? Get(from, nextKey).
  344. auto prop_value = TRY(from->get(property_key));
  345. // b. Perform ? Set(to, nextKey, propValue, true).
  346. TRY(to->set(property_key, prop_value, Object::ShouldThrowExceptions::Yes));
  347. }
  348. }
  349. // 4. Return to.
  350. return to;
  351. }
  352. }