ObjectConstructor.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 value = vm().argument(0);
  57. if (value.is_nullish())
  58. return Object::create_empty(global_object());
  59. return value.to_object(global_object());
  60. }
  61. // 20.1.1.1 Object ( [ value ] ), https://tc39.es/ecma262/#sec-object-value
  62. Value ObjectConstructor::construct(Function&)
  63. {
  64. return call();
  65. }
  66. // 20.1.2.10 Object.getOwnPropertyNames ( O ), https://tc39.es/ecma262/#sec-object.getownpropertynames
  67. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_names)
  68. {
  69. auto* object = vm.argument(0).to_object(global_object);
  70. if (vm.exception())
  71. return {};
  72. return Array::create_from(global_object, object->get_own_properties(PropertyKind::Key, false, GetOwnPropertyReturnType::StringOnly));
  73. }
  74. // 20.1.2.11 Object.getOwnPropertySymbols ( O ), https://tc39.es/ecma262/#sec-object.getownpropertysymbols
  75. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_symbols)
  76. {
  77. auto* object = vm.argument(0).to_object(global_object);
  78. if (vm.exception())
  79. return {};
  80. return Array::create_from(global_object, object->get_own_properties(PropertyKind::Key, false, GetOwnPropertyReturnType::SymbolOnly));
  81. }
  82. // 20.1.2.12 Object.getPrototypeOf ( O ), https://tc39.es/ecma262/#sec-object.getprototypeof
  83. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_prototype_of)
  84. {
  85. auto* object = vm.argument(0).to_object(global_object);
  86. if (vm.exception())
  87. return {};
  88. return object->prototype();
  89. }
  90. // 20.1.2.21 Object.setPrototypeOf ( O, proto ), https://tc39.es/ecma262/#sec-object.setprototypeof
  91. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::set_prototype_of)
  92. {
  93. auto* object = vm.argument(0).to_object(global_object);
  94. if (vm.exception())
  95. return {};
  96. auto prototype_value = vm.argument(1);
  97. Object* prototype;
  98. if (prototype_value.is_null()) {
  99. prototype = nullptr;
  100. } else if (prototype_value.is_object()) {
  101. prototype = &prototype_value.as_object();
  102. } else {
  103. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  104. return {};
  105. }
  106. if (!object->set_prototype(prototype)) {
  107. if (!vm.exception())
  108. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSetPrototypeOfReturnedFalse);
  109. return {};
  110. }
  111. return object;
  112. }
  113. // 20.1.2.14 Object.isExtensible ( O ), https://tc39.es/ecma262/#sec-object.isextensible
  114. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_extensible)
  115. {
  116. auto argument = vm.argument(0);
  117. if (!argument.is_object())
  118. return Value(false);
  119. return Value(argument.as_object().is_extensible());
  120. }
  121. // 20.1.2.15 Object.isFrozen ( O ), https://tc39.es/ecma262/#sec-object.isfrozen
  122. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_frozen)
  123. {
  124. auto argument = vm.argument(0);
  125. if (!argument.is_object())
  126. return Value(true);
  127. return Value(argument.as_object().test_integrity_level(Object::IntegrityLevel::Frozen));
  128. }
  129. // 20.1.2.16 Object.isSealed ( O ), https://tc39.es/ecma262/#sec-object.issealed
  130. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_sealed)
  131. {
  132. auto argument = vm.argument(0);
  133. if (!argument.is_object())
  134. return Value(true);
  135. return Value(argument.as_object().test_integrity_level(Object::IntegrityLevel::Sealed));
  136. }
  137. // 20.1.2.18 Object.preventExtensions ( O ), https://tc39.es/ecma262/#sec-object.preventextensions
  138. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::prevent_extensions)
  139. {
  140. auto argument = vm.argument(0);
  141. if (!argument.is_object())
  142. return argument;
  143. auto status = argument.as_object().prevent_extensions();
  144. if (vm.exception())
  145. return {};
  146. if (!status) {
  147. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPreventExtensionsReturnedFalse);
  148. return {};
  149. }
  150. return argument;
  151. }
  152. // 20.1.2.6 Object.freeze ( O ), https://tc39.es/ecma262/#sec-object.freeze
  153. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::freeze)
  154. {
  155. auto argument = vm.argument(0);
  156. if (!argument.is_object())
  157. return argument;
  158. auto status = argument.as_object().set_integrity_level(Object::IntegrityLevel::Frozen);
  159. if (vm.exception())
  160. return {};
  161. if (!status) {
  162. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectFreezeFailed);
  163. return {};
  164. }
  165. return argument;
  166. }
  167. // 20.1.2.7 Object.fromEntries ( iterable ), https://tc39.es/ecma262/#sec-object.fromentries
  168. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::from_entries)
  169. {
  170. auto iterable = require_object_coercible(global_object, vm.argument(0));
  171. if (vm.exception())
  172. return {};
  173. auto* object = Object::create_empty(global_object);
  174. object->set_prototype(global_object.object_prototype());
  175. get_iterator_values(global_object, iterable, [&](Value iterator_value) {
  176. if (vm.exception())
  177. return IterationDecision::Break;
  178. if (!iterator_value.is_object()) {
  179. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, String::formatted("Iterator value {}", iterator_value.to_string_without_side_effects()));
  180. return IterationDecision::Break;
  181. }
  182. auto key = iterator_value.as_object().get(0).value_or(js_undefined());
  183. if (vm.exception())
  184. return IterationDecision::Break;
  185. auto value = iterator_value.as_object().get(1).value_or(js_undefined());
  186. if (vm.exception())
  187. return IterationDecision::Break;
  188. auto property_key = key.to_property_key(global_object);
  189. if (vm.exception())
  190. return IterationDecision::Break;
  191. object->define_property(property_key, value);
  192. if (vm.exception())
  193. return IterationDecision::Break;
  194. return IterationDecision::Continue;
  195. });
  196. if (vm.exception())
  197. return {};
  198. return object;
  199. }
  200. // 20.1.2.20 Object.seal ( O ), https://tc39.es/ecma262/#sec-object.seal
  201. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::seal)
  202. {
  203. auto argument = vm.argument(0);
  204. if (!argument.is_object())
  205. return argument;
  206. auto status = argument.as_object().set_integrity_level(Object::IntegrityLevel::Sealed);
  207. if (vm.exception())
  208. return {};
  209. if (!status) {
  210. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSealFailed);
  211. return {};
  212. }
  213. return argument;
  214. }
  215. // 20.1.2.8 Object.getOwnPropertyDescriptor ( O, P ), https://tc39.es/ecma262/#sec-object.getownpropertydescriptor
  216. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_descriptor)
  217. {
  218. auto* object = vm.argument(0).to_object(global_object);
  219. if (vm.exception())
  220. return {};
  221. auto property_key = vm.argument(1).to_property_key(global_object);
  222. if (vm.exception())
  223. return {};
  224. return object->get_own_property_descriptor_object(property_key);
  225. }
  226. // 20.1.2.4 Object.defineProperty ( O, P, Attributes ), https://tc39.es/ecma262/#sec-object.defineproperty
  227. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::define_property_)
  228. {
  229. if (!vm.argument(0).is_object()) {
  230. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Object argument");
  231. return {};
  232. }
  233. auto property_key = vm.argument(1).to_property_key(global_object);
  234. if (vm.exception())
  235. return {};
  236. if (!vm.argument(2).is_object()) {
  237. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Descriptor argument");
  238. return {};
  239. }
  240. auto& object = vm.argument(0).as_object();
  241. auto& descriptor = vm.argument(2).as_object();
  242. if (!object.define_property(property_key, descriptor)) {
  243. if (!vm.exception()) {
  244. if (AK::is<ProxyObject>(object)) {
  245. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectDefinePropertyReturnedFalse);
  246. } else {
  247. vm.throw_exception<TypeError>(global_object, ErrorType::NonExtensibleDefine, property_key.to_display_string());
  248. }
  249. }
  250. return {};
  251. }
  252. return &object;
  253. }
  254. // 20.1.2.3 Object.defineProperties ( O, Properties ), https://tc39.es/ecma262/#sec-object.defineproperties
  255. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::define_properties)
  256. {
  257. if (!vm.argument(0).is_object()) {
  258. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Object argument");
  259. return {};
  260. }
  261. auto& object = vm.argument(0).as_object();
  262. auto properties = vm.argument(1);
  263. object.define_properties(properties);
  264. if (vm.exception())
  265. return {};
  266. return &object;
  267. }
  268. // 20.1.2.13 Object.is ( value1, value2 ), https://tc39.es/ecma262/#sec-object.is
  269. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is)
  270. {
  271. return Value(same_value(vm.argument(0), vm.argument(1)));
  272. }
  273. // 20.1.2.17 Object.keys ( O ), https://tc39.es/ecma262/#sec-object.keys
  274. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::keys)
  275. {
  276. auto* obj_arg = vm.argument(0).to_object(global_object);
  277. if (vm.exception())
  278. return {};
  279. return Array::create_from(global_object, obj_arg->get_enumerable_own_property_names(PropertyKind::Key));
  280. }
  281. // 20.1.2.22 Object.values ( O ), https://tc39.es/ecma262/#sec-object.values
  282. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::values)
  283. {
  284. auto* obj_arg = vm.argument(0).to_object(global_object);
  285. if (vm.exception())
  286. return {};
  287. return Array::create_from(global_object, obj_arg->get_enumerable_own_property_names(PropertyKind::Value));
  288. }
  289. // 20.1.2.5 Object.entries ( O ), https://tc39.es/ecma262/#sec-object.entries
  290. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::entries)
  291. {
  292. auto* obj_arg = vm.argument(0).to_object(global_object);
  293. if (vm.exception())
  294. return {};
  295. return Array::create_from(global_object, obj_arg->get_enumerable_own_property_names(PropertyKind::KeyAndValue));
  296. }
  297. // 20.1.2.2 Object.create ( O, Properties ), https://tc39.es/ecma262/#sec-object.create
  298. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::create)
  299. {
  300. auto prototype_value = vm.argument(0);
  301. auto properties = vm.argument(1);
  302. Object* prototype;
  303. if (prototype_value.is_null()) {
  304. prototype = nullptr;
  305. } else if (prototype_value.is_object()) {
  306. prototype = &prototype_value.as_object();
  307. } else {
  308. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  309. return {};
  310. }
  311. auto* object = Object::create_empty(global_object);
  312. object->set_prototype(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. }