ObjectConstructor.cpp 21 KB

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