ObjectConstructor.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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/Iterator.h>
  13. #include <LibJS/Runtime/ObjectConstructor.h>
  14. #include <LibJS/Runtime/ProxyObject.h>
  15. #include <LibJS/Runtime/Shape.h>
  16. namespace JS {
  17. JS_DEFINE_ALLOCATOR(ObjectConstructor);
  18. ObjectConstructor::ObjectConstructor(Realm& realm)
  19. : NativeFunction(realm.vm().names.Object.as_string(), realm.intrinsics().function_prototype())
  20. {
  21. }
  22. void ObjectConstructor::initialize(Realm& realm)
  23. {
  24. auto& vm = this->vm();
  25. Base::initialize(realm);
  26. // 20.1.2.21 Object.prototype, https://tc39.es/ecma262/#sec-object.prototype
  27. define_direct_property(vm.names.prototype, realm.intrinsics().object_prototype(), 0);
  28. u8 attr = Attribute::Writable | Attribute::Configurable;
  29. define_native_function(realm, vm.names.defineProperty, define_property, 3, attr);
  30. define_native_function(realm, vm.names.defineProperties, define_properties, 2, attr);
  31. define_native_function(realm, vm.names.is, is, 2, attr);
  32. define_native_function(realm, vm.names.getOwnPropertyDescriptor, get_own_property_descriptor, 2, attr);
  33. define_native_function(realm, vm.names.getOwnPropertyDescriptors, get_own_property_descriptors, 1, attr);
  34. define_native_function(realm, vm.names.getOwnPropertyNames, get_own_property_names, 1, attr);
  35. define_native_function(realm, vm.names.getOwnPropertySymbols, get_own_property_symbols, 1, attr);
  36. define_native_function(realm, vm.names.getPrototypeOf, get_prototype_of, 1, attr);
  37. define_native_function(realm, vm.names.groupBy, group_by, 2, attr);
  38. define_native_function(realm, vm.names.setPrototypeOf, set_prototype_of, 2, attr);
  39. define_native_function(realm, vm.names.isExtensible, is_extensible, 1, attr);
  40. define_native_function(realm, vm.names.isFrozen, is_frozen, 1, attr);
  41. define_native_function(realm, vm.names.isSealed, is_sealed, 1, attr);
  42. define_native_function(realm, vm.names.preventExtensions, prevent_extensions, 1, attr);
  43. define_native_function(realm, vm.names.freeze, freeze, 1, attr);
  44. define_native_function(realm, vm.names.fromEntries, from_entries, 1, attr);
  45. define_native_function(realm, vm.names.seal, seal, 1, attr);
  46. define_native_function(realm, vm.names.keys, keys, 1, attr);
  47. define_native_function(realm, vm.names.values, values, 1, attr);
  48. define_native_function(realm, vm.names.entries, entries, 1, attr);
  49. define_native_function(realm, vm.names.create, create, 2, attr);
  50. define_native_function(realm, vm.names.hasOwn, has_own, 2, attr);
  51. define_native_function(realm, vm.names.assign, assign, 2, attr);
  52. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  53. }
  54. // 20.1.1.1 Object ( [ value ] ), https://tc39.es/ecma262/#sec-object-value
  55. ThrowCompletionOr<Value> ObjectConstructor::call()
  56. {
  57. return TRY(construct(*this));
  58. }
  59. // 20.1.1.1 Object ( [ value ] ), https://tc39.es/ecma262/#sec-object-value
  60. ThrowCompletionOr<NonnullGCPtr<Object>> ObjectConstructor::construct(FunctionObject& new_target)
  61. {
  62. auto& vm = this->vm();
  63. auto& realm = *vm.current_realm();
  64. auto value = vm.argument(0);
  65. // 1. If NewTarget is neither undefined nor the active function object, then
  66. if (&new_target != this) {
  67. // a. Return ? OrdinaryCreateFromConstructor(NewTarget, "%Object.prototype%").
  68. return TRY(ordinary_create_from_constructor<Object>(vm, new_target, &Intrinsics::object_prototype, ConstructWithPrototypeTag::Tag));
  69. }
  70. // 2. If value is either undefined or null, return OrdinaryObjectCreate(%Object.prototype%).
  71. if (value.is_nullish())
  72. return Object::create(realm, realm.intrinsics().object_prototype());
  73. // 3. Return ! ToObject(value).
  74. return MUST(value.to_object(vm));
  75. }
  76. enum class GetOwnPropertyKeysType {
  77. String,
  78. Symbol,
  79. };
  80. // 20.1.2.11.1 GetOwnPropertyKeys ( O, type ), https://tc39.es/ecma262/#sec-getownpropertykeys
  81. static ThrowCompletionOr<MarkedVector<Value>> get_own_property_keys(VM& vm, Value value, GetOwnPropertyKeysType type)
  82. {
  83. // 1. Let obj be ? ToObject(O).
  84. auto object = TRY(value.to_object(vm));
  85. // 2. Let keys be ? obj.[[OwnPropertyKeys]]().
  86. auto keys = TRY(object->internal_own_property_keys());
  87. // 3. Let nameList be a new empty List.
  88. auto name_list = MarkedVector<Value> { vm.heap() };
  89. // 4. For each element nextKey of keys, do
  90. for (auto& next_key : keys) {
  91. // a. If Type(nextKey) is Symbol and type is symbol or Type(nextKey) is String and type is string, then
  92. if ((next_key.is_symbol() && type == GetOwnPropertyKeysType::Symbol) || (next_key.is_string() && type == GetOwnPropertyKeysType::String)) {
  93. // i. Append nextKey as the last element of nameList.
  94. name_list.append(next_key);
  95. }
  96. }
  97. // 5. Return nameList.
  98. return { move(name_list) };
  99. }
  100. // 20.1.2.1 Object.assign ( target, ...sources ), https://tc39.es/ecma262/#sec-object.assign
  101. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::assign)
  102. {
  103. // 1. Let to be ? ToObject(target).
  104. auto to = TRY(vm.argument(0).to_object(vm));
  105. // 2. If only one argument was passed, return to.
  106. if (vm.argument_count() == 1)
  107. return to;
  108. // 3. For each element nextSource of sources, do
  109. for (size_t i = 1; i < vm.argument_count(); ++i) {
  110. auto next_source = vm.argument(i);
  111. // a. If nextSource is neither undefined nor null, then
  112. if (next_source.is_nullish())
  113. continue;
  114. // i. Let from be ! ToObject(nextSource).
  115. auto from = MUST(next_source.to_object(vm));
  116. // ii. Let keys be ? from.[[OwnPropertyKeys]]().
  117. auto keys = TRY(from->internal_own_property_keys());
  118. // iii. For each element nextKey of keys, do
  119. for (auto& next_key : keys) {
  120. auto property_key = MUST(PropertyKey::from_value(vm, next_key));
  121. // 1. Let desc be ? from.[[GetOwnProperty]](nextKey).
  122. auto desc = TRY(from->internal_get_own_property(property_key));
  123. // 2. If desc is not undefined and desc.[[Enumerable]] is true, then
  124. if (!desc.has_value() || !*desc->enumerable)
  125. continue;
  126. // a. Let propValue be ? Get(from, nextKey).
  127. auto prop_value = TRY(from->get(property_key));
  128. // b. Perform ? Set(to, nextKey, propValue, true).
  129. TRY(to->set(property_key, prop_value, Object::ShouldThrowExceptions::Yes));
  130. }
  131. }
  132. // 4. Return to.
  133. return to;
  134. }
  135. // 20.1.2.2 Object.create ( O, Properties ), https://tc39.es/ecma262/#sec-object.create
  136. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::create)
  137. {
  138. auto& realm = *vm.current_realm();
  139. auto proto = vm.argument(0);
  140. auto properties = vm.argument(1);
  141. // 1. If Type(O) is neither Object nor Null, throw a TypeError exception.
  142. if (!proto.is_object() && !proto.is_null())
  143. return vm.throw_completion<TypeError>(ErrorType::ObjectPrototypeWrongType);
  144. // 2. Let obj be OrdinaryObjectCreate(O).
  145. auto object = Object::create(realm, proto.is_null() ? nullptr : &proto.as_object());
  146. // 3. If Properties is not undefined, then
  147. if (!properties.is_undefined()) {
  148. // a. Return ? ObjectDefineProperties(obj, Properties).
  149. return TRY(object->define_properties(properties));
  150. }
  151. // 4. Return obj.
  152. return object;
  153. }
  154. // 20.1.2.3 Object.defineProperties ( O, Properties ), https://tc39.es/ecma262/#sec-object.defineproperties
  155. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::define_properties)
  156. {
  157. auto object = vm.argument(0);
  158. auto properties = vm.argument(1);
  159. // 1. If Type(O) is not Object, throw a TypeError exception.
  160. if (!object.is_object())
  161. return vm.throw_completion<TypeError>(ErrorType::NotAnObject, "Object argument");
  162. // 2. Return ? ObjectDefineProperties(O, Properties).
  163. return TRY(object.as_object().define_properties(properties));
  164. }
  165. // 20.1.2.4 Object.defineProperty ( O, P, Attributes ), https://tc39.es/ecma262/#sec-object.defineproperty
  166. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::define_property)
  167. {
  168. // 1. If O is not an Object, throw a TypeError exception.
  169. if (!vm.argument(0).is_object())
  170. return vm.throw_completion<TypeError>(ErrorType::NotAnObject, vm.argument(0).to_string_without_side_effects());
  171. auto object = MUST(vm.argument(0).to_object(vm));
  172. // 2. Let key be ? ToPropertyKey(P).
  173. auto key = TRY(vm.argument(1).to_property_key(vm));
  174. // 3. Let desc be ? ToPropertyDescriptor(Attributes).
  175. auto descriptor = TRY(to_property_descriptor(vm, vm.argument(2)));
  176. // 4. Perform ? DefinePropertyOrThrow(O, key, desc).
  177. TRY(object->define_property_or_throw(key, descriptor));
  178. // 5. Return O.
  179. return object;
  180. }
  181. // 20.1.2.5 Object.entries ( O ), https://tc39.es/ecma262/#sec-object.entries
  182. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::entries)
  183. {
  184. auto& realm = *vm.current_realm();
  185. // 1. Let obj be ? ToObject(O).
  186. auto object = TRY(vm.argument(0).to_object(vm));
  187. // 2. Let entryList be ? EnumerableOwnProperties(obj, key+value).
  188. auto name_list = TRY(object->enumerable_own_property_names(PropertyKind::KeyAndValue));
  189. // 3. Return CreateArrayFromList(entryList).
  190. return Array::create_from(realm, name_list);
  191. }
  192. // 20.1.2.6 Object.freeze ( O ), https://tc39.es/ecma262/#sec-object.freeze
  193. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::freeze)
  194. {
  195. auto argument = vm.argument(0);
  196. // 1. If O is not an Object, return O.
  197. if (!argument.is_object())
  198. return argument;
  199. // 2. Let status be ? SetIntegrityLevel(O, frozen).
  200. auto status = TRY(argument.as_object().set_integrity_level(Object::IntegrityLevel::Frozen));
  201. // 3. If status is false, throw a TypeError exception.
  202. if (!status)
  203. return vm.throw_completion<TypeError>(ErrorType::ObjectFreezeFailed);
  204. // 4. Return O.
  205. return argument;
  206. }
  207. // 20.1.2.7 Object.fromEntries ( iterable ), https://tc39.es/ecma262/#sec-object.fromentries
  208. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::from_entries)
  209. {
  210. auto& realm = *vm.current_realm();
  211. // 1. Perform ? RequireObjectCoercible(iterable).
  212. auto iterable = TRY(require_object_coercible(vm, vm.argument(0)));
  213. // 2. Let obj be OrdinaryObjectCreate(%Object.prototype%).
  214. auto object = Object::create(realm, realm.intrinsics().object_prototype());
  215. // 3. Assert: obj is an extensible ordinary object with no own properties.
  216. // 4. Let closure be a new Abstract Closure with parameters (key, value) that captures obj and performs the following steps when called:
  217. // 5. Let adder be CreateBuiltinFunction(closure, 2, "", « »).
  218. // 6. Return ? AddEntriesFromIterable(obj, iterable, adder).
  219. (void)TRY(get_iterator_values(vm, iterable, [&](Value iterator_value) -> Optional<Completion> {
  220. if (!iterator_value.is_object())
  221. return vm.throw_completion<TypeError>(ErrorType::NotAnObject, ByteString::formatted("Iterator value {}", iterator_value.to_string_without_side_effects()));
  222. auto key = TRY(iterator_value.as_object().get(0));
  223. auto value = TRY(iterator_value.as_object().get(1));
  224. // a. Let propertyKey be ? ToPropertyKey(key).
  225. auto property_key = TRY(key.to_property_key(vm));
  226. // b. Perform ! CreateDataPropertyOrThrow(obj, propertyKey, value).
  227. MUST(object->create_data_property_or_throw(property_key, value));
  228. // c. Return undefined.
  229. return {};
  230. }));
  231. return object;
  232. }
  233. // 20.1.2.8 Object.getOwnPropertyDescriptor ( O, P ), https://tc39.es/ecma262/#sec-object.getownpropertydescriptor
  234. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_descriptor)
  235. {
  236. // 1. Let obj be ? ToObject(O).
  237. auto object = TRY(vm.argument(0).to_object(vm));
  238. // 2. Let key be ? ToPropertyKey(P).
  239. auto key = TRY(vm.argument(1).to_property_key(vm));
  240. // 3. Let desc be ? obj.[[GetOwnProperty]](key).
  241. auto descriptor = TRY(object->internal_get_own_property(key));
  242. // 4. Return FromPropertyDescriptor(desc).
  243. return from_property_descriptor(vm, descriptor);
  244. }
  245. // 20.1.2.9 Object.getOwnPropertyDescriptors ( O ), https://tc39.es/ecma262/#sec-object.getownpropertydescriptors
  246. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_descriptors)
  247. {
  248. auto& realm = *vm.current_realm();
  249. // 1. Let obj be ? ToObject(O).
  250. auto object = TRY(vm.argument(0).to_object(vm));
  251. // 2. Let ownKeys be ? obj.[[OwnPropertyKeys]]().
  252. auto own_keys = TRY(object->internal_own_property_keys());
  253. // 3. Let descriptors be OrdinaryObjectCreate(%Object.prototype%).
  254. auto descriptors = Object::create(realm, realm.intrinsics().object_prototype());
  255. // 4. For each element key of ownKeys, do
  256. for (auto& key : own_keys) {
  257. auto property_key = MUST(PropertyKey::from_value(vm, key));
  258. // a. Let desc be ? obj.[[GetOwnProperty]](key).
  259. auto desc = TRY(object->internal_get_own_property(property_key));
  260. // b. Let descriptor be FromPropertyDescriptor(desc).
  261. auto descriptor = from_property_descriptor(vm, desc);
  262. // c. If descriptor is not undefined, perform ! CreateDataPropertyOrThrow(descriptors, key, descriptor).
  263. if (!descriptor.is_undefined())
  264. MUST(descriptors->create_data_property_or_throw(property_key, descriptor));
  265. }
  266. // 5. Return descriptors.
  267. return descriptors;
  268. }
  269. // 20.1.2.10 Object.getOwnPropertyNames ( O ), https://tc39.es/ecma262/#sec-object.getownpropertynames
  270. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_names)
  271. {
  272. auto& realm = *vm.current_realm();
  273. // 1. Return CreateArrayFromList(? GetOwnPropertyKeys(O, string)).
  274. return Array::create_from(realm, TRY(get_own_property_keys(vm, vm.argument(0), GetOwnPropertyKeysType::String)));
  275. }
  276. // 20.1.2.11 Object.getOwnPropertySymbols ( O ), https://tc39.es/ecma262/#sec-object.getownpropertysymbols
  277. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_symbols)
  278. {
  279. auto& realm = *vm.current_realm();
  280. // 1. Return CreateArrayFromList(? GetOwnPropertyKeys(O, symbol)).
  281. return Array::create_from(realm, TRY(get_own_property_keys(vm, vm.argument(0), GetOwnPropertyKeysType::Symbol)));
  282. }
  283. // 20.1.2.12 Object.getPrototypeOf ( O ), https://tc39.es/ecma262/#sec-object.getprototypeof
  284. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_prototype_of)
  285. {
  286. // 1. Let obj be ? ToObject(O).
  287. auto object = TRY(vm.argument(0).to_object(vm));
  288. // 2. Return ? obj.[[GetPrototypeOf]]().
  289. return TRY(object->internal_get_prototype_of());
  290. }
  291. // 20.1.2.13 Object.groupBy ( items, callbackfn ), https://tc39.es/ecma262/#sec-object.groupby
  292. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::group_by)
  293. {
  294. auto& realm = *vm.current_realm();
  295. auto items = vm.argument(0);
  296. auto callback_function = vm.argument(1);
  297. // 1. Let groups be ? GroupBy(items, callbackfn, property).
  298. auto groups = TRY((JS::group_by<OrderedHashMap<PropertyKey, MarkedVector<Value>>, PropertyKey>(vm, items, callback_function)));
  299. // 2. Let obj be OrdinaryObjectCreate(null).
  300. auto object = Object::create(realm, nullptr);
  301. // 3. For each Record { [[Key]], [[Elements]] } g of groups, do
  302. for (auto& group : groups) {
  303. // a. Let elements be CreateArrayFromList(g.[[Elements]]).
  304. auto elements = Array::create_from(realm, group.value);
  305. // b. Perform ! CreateDataPropertyOrThrow(obj, g.[[Key]], elements).
  306. MUST(object->create_data_property_or_throw(group.key, elements));
  307. }
  308. // 4. Return obj.
  309. return object;
  310. }
  311. // 20.1.2.14 Object.hasOwn ( O, P ), https://tc39.es/ecma262/#sec-object.hasown
  312. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::has_own)
  313. {
  314. // 1. Let obj be ? ToObject(O).
  315. auto object = TRY(vm.argument(0).to_object(vm));
  316. // 2. Let key be ? ToPropertyKey(P).
  317. auto key = TRY(vm.argument(1).to_property_key(vm));
  318. // 3. Return ? HasOwnProperty(obj, key).
  319. return Value(TRY(object->has_own_property(key)));
  320. }
  321. // 20.1.2.15 Object.is ( value1, value2 ), https://tc39.es/ecma262/#sec-object.is
  322. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is)
  323. {
  324. // 1. Return SameValue(value1, value2).
  325. return Value(same_value(vm.argument(0), vm.argument(1)));
  326. }
  327. // 20.1.2.16 Object.isExtensible ( O ), https://tc39.es/ecma262/#sec-object.isextensible
  328. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_extensible)
  329. {
  330. auto argument = vm.argument(0);
  331. // 1. If O is not an Object, return false.
  332. if (!argument.is_object())
  333. return Value(false);
  334. // 2. Return ? IsExtensible(O).
  335. return Value(TRY(argument.as_object().is_extensible()));
  336. }
  337. // 20.1.2.17 Object.isFrozen ( O ), https://tc39.es/ecma262/#sec-object.isfrozen
  338. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_frozen)
  339. {
  340. auto argument = vm.argument(0);
  341. // 1. If O is not an Object, return true.
  342. if (!argument.is_object())
  343. return Value(true);
  344. // 2. Return ? TestIntegrityLevel(O, frozen).
  345. return Value(TRY(argument.as_object().test_integrity_level(Object::IntegrityLevel::Frozen)));
  346. }
  347. // 20.1.2.18 Object.isSealed ( O ), https://tc39.es/ecma262/#sec-object.issealed
  348. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_sealed)
  349. {
  350. auto argument = vm.argument(0);
  351. // 1. If O is not an Object, return true.
  352. if (!argument.is_object())
  353. return Value(true);
  354. // 2. Return ? TestIntegrityLevel(O, sealed).
  355. return Value(TRY(argument.as_object().test_integrity_level(Object::IntegrityLevel::Sealed)));
  356. }
  357. // 20.1.2.19 Object.keys ( O ), https://tc39.es/ecma262/#sec-object.keys
  358. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::keys)
  359. {
  360. auto& realm = *vm.current_realm();
  361. // 1. Let obj be ? ToObject(O).
  362. auto object = TRY(vm.argument(0).to_object(vm));
  363. // 2. Let keyList be ? EnumerableOwnProperties(obj, key).
  364. auto name_list = TRY(object->enumerable_own_property_names(PropertyKind::Key));
  365. // 3. Return CreateArrayFromList(keyList).
  366. return Array::create_from(realm, name_list);
  367. }
  368. // 20.1.2.20 Object.preventExtensions ( O ), https://tc39.es/ecma262/#sec-object.preventextensions
  369. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::prevent_extensions)
  370. {
  371. auto argument = vm.argument(0);
  372. // 1. If O is not an Object, return O.
  373. if (!argument.is_object())
  374. return argument;
  375. // 2. Let status be ? O.[[PreventExtensions]]().
  376. auto status = TRY(argument.as_object().internal_prevent_extensions());
  377. // 3. If status is false, throw a TypeError exception.
  378. if (!status) {
  379. // FIXME: Improve/contextualize error message
  380. return vm.throw_completion<TypeError>(ErrorType::ObjectPreventExtensionsReturnedFalse);
  381. }
  382. // 4. Return O.
  383. return argument;
  384. }
  385. // 20.1.2.22 Object.seal ( O ), https://tc39.es/ecma262/#sec-object.seal
  386. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::seal)
  387. {
  388. auto argument = vm.argument(0);
  389. // 1. If O is not an Object, return O.
  390. if (!argument.is_object())
  391. return argument;
  392. // 2. Let status be ? SetIntegrityLevel(O, sealed).
  393. auto status = TRY(argument.as_object().set_integrity_level(Object::IntegrityLevel::Sealed));
  394. // 3. If status is false, throw a TypeError exception.
  395. if (!status)
  396. return vm.throw_completion<TypeError>(ErrorType::ObjectSealFailed);
  397. // 4. Return O.
  398. return argument;
  399. }
  400. // 20.1.2.23 Object.setPrototypeOf ( O, proto ), https://tc39.es/ecma262/#sec-object.setprototypeof
  401. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::set_prototype_of)
  402. {
  403. auto proto = vm.argument(1);
  404. // 1. Set O to ? RequireObjectCoercible(O).
  405. auto object = TRY(require_object_coercible(vm, vm.argument(0)));
  406. // 2. If Type(proto) is neither Object nor Null, throw a TypeError exception.
  407. if (!proto.is_object() && !proto.is_null())
  408. return vm.throw_completion<TypeError>(ErrorType::ObjectPrototypeWrongType);
  409. // 3. If Type(O) is not Object, return O.
  410. if (!object.is_object())
  411. return object;
  412. // 4. Let status be ? O.[[SetPrototypeOf]](proto).
  413. auto status = TRY(object.as_object().internal_set_prototype_of(proto.is_null() ? nullptr : &proto.as_object()));
  414. // 5. If status is false, throw a TypeError exception.
  415. if (!status) {
  416. // FIXME: Improve/contextualize error message
  417. return vm.throw_completion<TypeError>(ErrorType::ObjectSetPrototypeOfReturnedFalse);
  418. }
  419. // 6. Return O.
  420. return object;
  421. }
  422. // 20.1.2.24 Object.values ( O ), https://tc39.es/ecma262/#sec-object.values
  423. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::values)
  424. {
  425. auto& realm = *vm.current_realm();
  426. // 1. Let obj be ? ToObject(O).
  427. auto object = TRY(vm.argument(0).to_object(vm));
  428. // 2. Let valueList be ? EnumerableOwnProperties(obj, value).
  429. auto name_list = TRY(object->enumerable_own_property_names(PropertyKind::Value));
  430. // 3. Return CreateArrayFromList(valueList).
  431. return Array::create_from(realm, name_list);
  432. }
  433. }