ObjectConstructor.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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/Array.h>
  9. #include <LibJS/Runtime/Error.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/IteratorOperations.h>
  12. #include <LibJS/Runtime/ObjectConstructor.h>
  13. #include <LibJS/Runtime/ProxyObject.h>
  14. #include <LibJS/Runtime/Shape.h>
  15. namespace JS {
  16. ObjectConstructor::ObjectConstructor(GlobalObject& global_object)
  17. : NativeFunction(vm().names.Object, *global_object.function_prototype())
  18. {
  19. }
  20. void ObjectConstructor::initialize(GlobalObject& global_object)
  21. {
  22. auto& vm = this->vm();
  23. NativeFunction::initialize(global_object);
  24. // 20.1.2.19 Object.prototype, https://tc39.es/ecma262/#sec-object.prototype
  25. define_property(vm.names.prototype, global_object.object_prototype(), 0);
  26. define_property(vm.names.length, Value(1), Attribute::Configurable);
  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.getOwnPropertyNames, get_own_property_names, 1, attr);
  33. define_native_function(vm.names.getOwnPropertySymbols, get_own_property_symbols, 1, attr);
  34. define_native_function(vm.names.getPrototypeOf, get_prototype_of, 1, attr);
  35. define_native_function(vm.names.setPrototypeOf, set_prototype_of, 2, attr);
  36. define_native_function(vm.names.isExtensible, is_extensible, 1, attr);
  37. define_native_function(vm.names.isFrozen, is_frozen, 1, attr);
  38. define_native_function(vm.names.isSealed, is_sealed, 1, attr);
  39. define_native_function(vm.names.preventExtensions, prevent_extensions, 1, attr);
  40. define_native_function(vm.names.freeze, freeze, 1, attr);
  41. define_native_function(vm.names.fromEntries, from_entries, 1, attr);
  42. define_native_function(vm.names.seal, seal, 1, attr);
  43. define_native_function(vm.names.keys, keys, 1, attr);
  44. define_native_function(vm.names.values, values, 1, attr);
  45. define_native_function(vm.names.entries, entries, 1, attr);
  46. define_native_function(vm.names.create, create, 2, attr);
  47. define_native_function(vm.names.hasOwn, has_own, 2, attr);
  48. define_native_function(vm.names.assign, assign, 2, attr);
  49. }
  50. ObjectConstructor::~ObjectConstructor()
  51. {
  52. }
  53. // 20.1.1.1 Object ( [ value ] ), https://tc39.es/ecma262/#sec-object-value
  54. Value ObjectConstructor::call()
  55. {
  56. auto& vm = this->vm();
  57. auto& global_object = this->global_object();
  58. auto value = vm.argument(0);
  59. if (value.is_nullish())
  60. return Object::create(global_object, global_object.object_prototype());
  61. return value.to_object(global_object);
  62. }
  63. // 20.1.1.1 Object ( [ value ] ), https://tc39.es/ecma262/#sec-object-value
  64. Value ObjectConstructor::construct(Function&)
  65. {
  66. return call();
  67. }
  68. // 20.1.2.10 Object.getOwnPropertyNames ( O ), https://tc39.es/ecma262/#sec-object.getownpropertynames
  69. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_names)
  70. {
  71. auto* object = vm.argument(0).to_object(global_object);
  72. if (vm.exception())
  73. return {};
  74. return Array::create_from(global_object, object->get_own_properties(PropertyKind::Key, false, GetOwnPropertyReturnType::StringOnly));
  75. }
  76. // 20.1.2.11 Object.getOwnPropertySymbols ( O ), https://tc39.es/ecma262/#sec-object.getownpropertysymbols
  77. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_symbols)
  78. {
  79. auto* object = vm.argument(0).to_object(global_object);
  80. if (vm.exception())
  81. return {};
  82. return Array::create_from(global_object, object->get_own_properties(PropertyKind::Key, false, GetOwnPropertyReturnType::SymbolOnly));
  83. }
  84. // 20.1.2.12 Object.getPrototypeOf ( O ), https://tc39.es/ecma262/#sec-object.getprototypeof
  85. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_prototype_of)
  86. {
  87. auto* object = vm.argument(0).to_object(global_object);
  88. if (vm.exception())
  89. return {};
  90. return object->prototype();
  91. }
  92. // 20.1.2.21 Object.setPrototypeOf ( O, proto ), https://tc39.es/ecma262/#sec-object.setprototypeof
  93. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::set_prototype_of)
  94. {
  95. auto* object = vm.argument(0).to_object(global_object);
  96. if (vm.exception())
  97. return {};
  98. auto prototype_value = vm.argument(1);
  99. Object* prototype;
  100. if (prototype_value.is_null()) {
  101. prototype = nullptr;
  102. } else if (prototype_value.is_object()) {
  103. prototype = &prototype_value.as_object();
  104. } else {
  105. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  106. return {};
  107. }
  108. if (!object->set_prototype(prototype)) {
  109. if (!vm.exception())
  110. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSetPrototypeOfReturnedFalse);
  111. return {};
  112. }
  113. return object;
  114. }
  115. // 20.1.2.14 Object.isExtensible ( O ), https://tc39.es/ecma262/#sec-object.isextensible
  116. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_extensible)
  117. {
  118. auto argument = vm.argument(0);
  119. if (!argument.is_object())
  120. return Value(false);
  121. return Value(argument.as_object().is_extensible());
  122. }
  123. // 20.1.2.15 Object.isFrozen ( O ), https://tc39.es/ecma262/#sec-object.isfrozen
  124. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_frozen)
  125. {
  126. auto argument = vm.argument(0);
  127. if (!argument.is_object())
  128. return Value(true);
  129. return Value(argument.as_object().test_integrity_level(Object::IntegrityLevel::Frozen));
  130. }
  131. // 20.1.2.16 Object.isSealed ( O ), https://tc39.es/ecma262/#sec-object.issealed
  132. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_sealed)
  133. {
  134. auto argument = vm.argument(0);
  135. if (!argument.is_object())
  136. return Value(true);
  137. return Value(argument.as_object().test_integrity_level(Object::IntegrityLevel::Sealed));
  138. }
  139. // 20.1.2.18 Object.preventExtensions ( O ), https://tc39.es/ecma262/#sec-object.preventextensions
  140. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::prevent_extensions)
  141. {
  142. auto argument = vm.argument(0);
  143. if (!argument.is_object())
  144. return argument;
  145. auto status = argument.as_object().prevent_extensions();
  146. if (vm.exception())
  147. return {};
  148. if (!status) {
  149. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPreventExtensionsReturnedFalse);
  150. return {};
  151. }
  152. return argument;
  153. }
  154. // 20.1.2.6 Object.freeze ( O ), https://tc39.es/ecma262/#sec-object.freeze
  155. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::freeze)
  156. {
  157. auto argument = vm.argument(0);
  158. if (!argument.is_object())
  159. return argument;
  160. auto status = argument.as_object().set_integrity_level(Object::IntegrityLevel::Frozen);
  161. if (vm.exception())
  162. return {};
  163. if (!status) {
  164. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectFreezeFailed);
  165. return {};
  166. }
  167. return argument;
  168. }
  169. // 20.1.2.7 Object.fromEntries ( iterable ), https://tc39.es/ecma262/#sec-object.fromentries
  170. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::from_entries)
  171. {
  172. auto iterable = require_object_coercible(global_object, vm.argument(0));
  173. if (vm.exception())
  174. return {};
  175. auto* object = Object::create(global_object, global_object.object_prototype());
  176. get_iterator_values(global_object, iterable, [&](Value iterator_value) {
  177. if (vm.exception())
  178. return IterationDecision::Break;
  179. if (!iterator_value.is_object()) {
  180. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, String::formatted("Iterator value {}", iterator_value.to_string_without_side_effects()));
  181. return IterationDecision::Break;
  182. }
  183. auto key = iterator_value.as_object().get(0).value_or(js_undefined());
  184. if (vm.exception())
  185. return IterationDecision::Break;
  186. auto value = iterator_value.as_object().get(1).value_or(js_undefined());
  187. if (vm.exception())
  188. return IterationDecision::Break;
  189. auto property_key = key.to_property_key(global_object);
  190. if (vm.exception())
  191. return IterationDecision::Break;
  192. object->define_property(property_key, value);
  193. if (vm.exception())
  194. return IterationDecision::Break;
  195. return IterationDecision::Continue;
  196. });
  197. if (vm.exception())
  198. return {};
  199. return object;
  200. }
  201. // 20.1.2.20 Object.seal ( O ), https://tc39.es/ecma262/#sec-object.seal
  202. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::seal)
  203. {
  204. auto argument = vm.argument(0);
  205. if (!argument.is_object())
  206. return argument;
  207. auto status = argument.as_object().set_integrity_level(Object::IntegrityLevel::Sealed);
  208. if (vm.exception())
  209. return {};
  210. if (!status) {
  211. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSealFailed);
  212. return {};
  213. }
  214. return argument;
  215. }
  216. // 20.1.2.8 Object.getOwnPropertyDescriptor ( O, P ), https://tc39.es/ecma262/#sec-object.getownpropertydescriptor
  217. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_descriptor)
  218. {
  219. auto* object = vm.argument(0).to_object(global_object);
  220. if (vm.exception())
  221. return {};
  222. auto property_key = vm.argument(1).to_property_key(global_object);
  223. if (vm.exception())
  224. return {};
  225. return object->get_own_property_descriptor_object(property_key);
  226. }
  227. // 20.1.2.4 Object.defineProperty ( O, P, Attributes ), https://tc39.es/ecma262/#sec-object.defineproperty
  228. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::define_property_)
  229. {
  230. if (!vm.argument(0).is_object()) {
  231. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Object argument");
  232. return {};
  233. }
  234. auto property_key = vm.argument(1).to_property_key(global_object);
  235. if (vm.exception())
  236. return {};
  237. if (!vm.argument(2).is_object()) {
  238. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Descriptor argument");
  239. return {};
  240. }
  241. auto& object = vm.argument(0).as_object();
  242. auto& descriptor = vm.argument(2).as_object();
  243. if (!object.define_property(property_key, descriptor)) {
  244. if (!vm.exception()) {
  245. if (AK::is<ProxyObject>(object)) {
  246. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectDefinePropertyReturnedFalse);
  247. } else {
  248. vm.throw_exception<TypeError>(global_object, ErrorType::NonExtensibleDefine, property_key.to_display_string());
  249. }
  250. }
  251. return {};
  252. }
  253. return &object;
  254. }
  255. // 20.1.2.3 Object.defineProperties ( O, Properties ), https://tc39.es/ecma262/#sec-object.defineproperties
  256. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::define_properties)
  257. {
  258. if (!vm.argument(0).is_object()) {
  259. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Object argument");
  260. return {};
  261. }
  262. auto& object = vm.argument(0).as_object();
  263. auto properties = vm.argument(1);
  264. object.define_properties(properties);
  265. if (vm.exception())
  266. return {};
  267. return &object;
  268. }
  269. // 20.1.2.13 Object.is ( value1, value2 ), https://tc39.es/ecma262/#sec-object.is
  270. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is)
  271. {
  272. return Value(same_value(vm.argument(0), vm.argument(1)));
  273. }
  274. // 20.1.2.17 Object.keys ( O ), https://tc39.es/ecma262/#sec-object.keys
  275. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::keys)
  276. {
  277. auto* obj_arg = vm.argument(0).to_object(global_object);
  278. if (vm.exception())
  279. return {};
  280. return Array::create_from(global_object, obj_arg->get_enumerable_own_property_names(PropertyKind::Key));
  281. }
  282. // 20.1.2.22 Object.values ( O ), https://tc39.es/ecma262/#sec-object.values
  283. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::values)
  284. {
  285. auto* obj_arg = vm.argument(0).to_object(global_object);
  286. if (vm.exception())
  287. return {};
  288. return Array::create_from(global_object, obj_arg->get_enumerable_own_property_names(PropertyKind::Value));
  289. }
  290. // 20.1.2.5 Object.entries ( O ), https://tc39.es/ecma262/#sec-object.entries
  291. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::entries)
  292. {
  293. auto* obj_arg = vm.argument(0).to_object(global_object);
  294. if (vm.exception())
  295. return {};
  296. return Array::create_from(global_object, obj_arg->get_enumerable_own_property_names(PropertyKind::KeyAndValue));
  297. }
  298. // 20.1.2.2 Object.create ( O, Properties ), https://tc39.es/ecma262/#sec-object.create
  299. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::create)
  300. {
  301. auto prototype_value = vm.argument(0);
  302. auto properties = vm.argument(1);
  303. Object* prototype;
  304. if (prototype_value.is_null()) {
  305. prototype = nullptr;
  306. } else if (prototype_value.is_object()) {
  307. prototype = &prototype_value.as_object();
  308. } else {
  309. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  310. return {};
  311. }
  312. auto* object = Object::create(global_object, prototype);
  313. if (!properties.is_undefined()) {
  314. object->define_properties(properties);
  315. if (vm.exception())
  316. return {};
  317. }
  318. return object;
  319. }
  320. // 1 Object.hasOwn ( O, P ), https://tc39.es/proposal-accessible-object-hasownproperty/#sec-object.hasown
  321. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::has_own)
  322. {
  323. auto* object = vm.argument(0).to_object(global_object);
  324. if (vm.exception())
  325. return {};
  326. auto property_key = vm.argument(1).to_property_key(global_object);
  327. if (vm.exception())
  328. return {};
  329. return Value(object->has_own_property(property_key));
  330. }
  331. // 20.1.2.1 Object.assign ( target, ...sources ), https://tc39.es/ecma262/#sec-object.assign
  332. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::assign)
  333. {
  334. auto* to = vm.argument(0).to_object(global_object);
  335. if (vm.exception())
  336. return {};
  337. if (vm.argument_count() == 1)
  338. return to;
  339. for (size_t i = 1; i < vm.argument_count(); ++i) {
  340. auto next_source = vm.argument(i);
  341. if (next_source.is_nullish())
  342. continue;
  343. auto from = next_source.to_object(global_object);
  344. VERIFY(!vm.exception());
  345. auto keys = from->get_own_properties(PropertyKind::Key);
  346. if (vm.exception())
  347. return {};
  348. for (auto& key : keys) {
  349. auto property_name = PropertyName::from_value(global_object, key);
  350. auto property_descriptor = from->get_own_property_descriptor(property_name);
  351. if (!property_descriptor.has_value() || !property_descriptor->attributes.is_enumerable())
  352. continue;
  353. auto value = from->get(property_name);
  354. if (vm.exception())
  355. return {};
  356. to->put(property_name, value);
  357. if (vm.exception())
  358. return {};
  359. }
  360. }
  361. return to;
  362. }
  363. }