ObjectConstructor.cpp 19 KB

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