ObjectConstructor.cpp 17 KB

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