ObjectConstructor.cpp 12 KB

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