ObjectConstructor.cpp 17 KB

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