ObjectConstructor.cpp 18 KB

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