ObjectConstructor.cpp 17 KB

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