ObjectConstructor.cpp 17 KB

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