ObjectConstructor.cpp 19 KB

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