ObjectConstructor.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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.getOwnPropertySymbols, get_own_property_symbols, 1, attr);
  32. define_native_function(vm.names.getPrototypeOf, get_prototype_of, 1, attr);
  33. define_native_function(vm.names.setPrototypeOf, set_prototype_of, 2, attr);
  34. define_native_function(vm.names.isExtensible, is_extensible, 1, attr);
  35. define_native_function(vm.names.isFrozen, is_frozen, 1, attr);
  36. define_native_function(vm.names.isSealed, is_sealed, 1, attr);
  37. define_native_function(vm.names.preventExtensions, prevent_extensions, 1, attr);
  38. define_native_function(vm.names.freeze, freeze, 1, attr);
  39. define_native_function(vm.names.seal, seal, 1, attr);
  40. define_native_function(vm.names.keys, keys, 1, attr);
  41. define_native_function(vm.names.values, values, 1, attr);
  42. define_native_function(vm.names.entries, entries, 1, attr);
  43. define_native_function(vm.names.create, create, 2, attr);
  44. define_native_function(vm.names.hasOwn, has_own, 2, attr);
  45. define_native_function(vm.names.assign, assign, 2, attr);
  46. }
  47. ObjectConstructor::~ObjectConstructor()
  48. {
  49. }
  50. Value ObjectConstructor::call()
  51. {
  52. auto value = vm().argument(0);
  53. if (value.is_nullish())
  54. return Object::create_empty(global_object());
  55. return value.to_object(global_object());
  56. }
  57. Value ObjectConstructor::construct(Function&)
  58. {
  59. return call();
  60. }
  61. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_names)
  62. {
  63. auto* object = vm.argument(0).to_object(global_object);
  64. if (vm.exception())
  65. return {};
  66. return Array::create_from(global_object, object->get_own_properties(PropertyKind::Key, false, GetOwnPropertyReturnType::StringOnly));
  67. }
  68. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_symbols)
  69. {
  70. auto* object = vm.argument(0).to_object(global_object);
  71. if (vm.exception())
  72. return {};
  73. return Array::create_from(global_object, object->get_own_properties(PropertyKind::Key, false, GetOwnPropertyReturnType::SymbolOnly));
  74. }
  75. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_prototype_of)
  76. {
  77. auto* object = vm.argument(0).to_object(global_object);
  78. if (vm.exception())
  79. return {};
  80. return object->prototype();
  81. }
  82. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::set_prototype_of)
  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. auto* obj_arg = vm.argument(0).to_object(global_object);
  229. if (vm.exception())
  230. return {};
  231. return Array::create_from(global_object, obj_arg->get_enumerable_own_property_names(PropertyKind::Key));
  232. }
  233. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::values)
  234. {
  235. auto* obj_arg = vm.argument(0).to_object(global_object);
  236. if (vm.exception())
  237. return {};
  238. return Array::create_from(global_object, obj_arg->get_enumerable_own_property_names(PropertyKind::Value));
  239. }
  240. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::entries)
  241. {
  242. auto* obj_arg = vm.argument(0).to_object(global_object);
  243. if (vm.exception())
  244. return {};
  245. return Array::create_from(global_object, obj_arg->get_enumerable_own_property_names(PropertyKind::KeyAndValue));
  246. }
  247. // 20.1.2.2 Object.create, https://tc39.es/ecma262/#sec-object.create
  248. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::create)
  249. {
  250. auto prototype_value = vm.argument(0);
  251. auto properties = vm.argument(1);
  252. Object* prototype;
  253. if (prototype_value.is_null()) {
  254. prototype = nullptr;
  255. } else if (prototype_value.is_object()) {
  256. prototype = &prototype_value.as_object();
  257. } else {
  258. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  259. return {};
  260. }
  261. auto* object = Object::create_empty(global_object);
  262. object->set_prototype(prototype);
  263. if (!properties.is_undefined()) {
  264. object->define_properties(properties);
  265. if (vm.exception())
  266. return {};
  267. }
  268. return object;
  269. }
  270. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::has_own)
  271. {
  272. auto* object = vm.argument(0).to_object(global_object);
  273. if (vm.exception())
  274. return {};
  275. auto property_key = vm.argument(1).to_property_key(global_object);
  276. if (vm.exception())
  277. return {};
  278. return Value(object->has_own_property(property_key));
  279. }
  280. // 20.1.2.1 Object.assign, https://tc39.es/ecma262/#sec-object.assign
  281. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::assign)
  282. {
  283. auto* to = vm.argument(0).to_object(global_object);
  284. if (vm.exception())
  285. return {};
  286. if (vm.argument_count() == 1)
  287. return to;
  288. for (size_t i = 1; i < vm.argument_count(); ++i) {
  289. auto next_source = vm.argument(i);
  290. if (next_source.is_nullish())
  291. continue;
  292. auto from = next_source.to_object(global_object);
  293. VERIFY(!vm.exception());
  294. auto keys = from->get_own_properties(PropertyKind::Key);
  295. if (vm.exception())
  296. return {};
  297. for (auto& key : keys) {
  298. auto property_name = PropertyName::from_value(global_object, key);
  299. auto property_descriptor = from->get_own_property_descriptor(property_name);
  300. if (!property_descriptor.has_value() || !property_descriptor->attributes.is_enumerable())
  301. continue;
  302. auto value = from->get(property_name);
  303. if (vm.exception())
  304. return {};
  305. to->put(property_name, value);
  306. if (vm.exception())
  307. return {};
  308. }
  309. }
  310. return to;
  311. }
  312. }