ObjectConstructor.cpp 15 KB

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