ObjectConstructor.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. }
  45. ObjectConstructor::~ObjectConstructor()
  46. {
  47. }
  48. Value ObjectConstructor::call()
  49. {
  50. auto value = vm().argument(0);
  51. if (value.is_nullish())
  52. return Object::create_empty(global_object());
  53. return value.to_object(global_object());
  54. }
  55. Value ObjectConstructor::construct(Function&)
  56. {
  57. return call();
  58. }
  59. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_names)
  60. {
  61. if (!vm.argument_count())
  62. return {};
  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_prototype_of)
  69. {
  70. if (!vm.argument_count())
  71. return {};
  72. auto* object = vm.argument(0).to_object(global_object);
  73. if (vm.exception())
  74. return {};
  75. return object->prototype();
  76. }
  77. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::set_prototype_of)
  78. {
  79. if (vm.argument_count() < 2) {
  80. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSetPrototypeOfTwoArgs);
  81. return {};
  82. }
  83. auto* object = vm.argument(0).to_object(global_object);
  84. if (vm.exception())
  85. return {};
  86. auto prototype_value = vm.argument(1);
  87. Object* prototype;
  88. if (prototype_value.is_null()) {
  89. prototype = nullptr;
  90. } else if (prototype_value.is_object()) {
  91. prototype = &prototype_value.as_object();
  92. } else {
  93. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  94. return {};
  95. }
  96. if (!object->set_prototype(prototype)) {
  97. if (!vm.exception())
  98. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSetPrototypeOfReturnedFalse);
  99. return {};
  100. }
  101. return object;
  102. }
  103. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_extensible)
  104. {
  105. auto argument = vm.argument(0);
  106. if (!argument.is_object())
  107. return Value(false);
  108. return Value(argument.as_object().is_extensible());
  109. }
  110. // 20.1.2.15 Object.isFrozen, https://tc39.es/ecma262/#sec-object.isfrozen
  111. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_frozen)
  112. {
  113. auto argument = vm.argument(0);
  114. if (!argument.is_object())
  115. return Value(true);
  116. return Value(argument.as_object().test_integrity_level(Object::IntegrityLevel::Frozen));
  117. }
  118. // 20.1.2.16 Object.isSealed, https://tc39.es/ecma262/#sec-object.issealed
  119. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_sealed)
  120. {
  121. auto argument = vm.argument(0);
  122. if (!argument.is_object())
  123. return Value(true);
  124. return Value(argument.as_object().test_integrity_level(Object::IntegrityLevel::Sealed));
  125. }
  126. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::prevent_extensions)
  127. {
  128. auto argument = vm.argument(0);
  129. if (!argument.is_object())
  130. return argument;
  131. auto status = argument.as_object().prevent_extensions();
  132. if (vm.exception())
  133. return {};
  134. if (!status) {
  135. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPreventExtensionsReturnedFalse);
  136. return {};
  137. }
  138. return argument;
  139. }
  140. // 20.1.2.6 Object.freeze, https://tc39.es/ecma262/#sec-object.freeze
  141. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::freeze)
  142. {
  143. auto argument = vm.argument(0);
  144. if (!argument.is_object())
  145. return argument;
  146. auto status = argument.as_object().set_integrity_level(Object::IntegrityLevel::Frozen);
  147. if (vm.exception())
  148. return {};
  149. if (!status) {
  150. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectFreezeFailed);
  151. return {};
  152. }
  153. return argument;
  154. }
  155. // 20.1.2.20 Object.seal, https://tc39.es/ecma262/#sec-object.seal
  156. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::seal)
  157. {
  158. auto argument = vm.argument(0);
  159. if (!argument.is_object())
  160. return argument;
  161. auto status = argument.as_object().set_integrity_level(Object::IntegrityLevel::Sealed);
  162. if (vm.exception())
  163. return {};
  164. if (!status) {
  165. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSealFailed);
  166. return {};
  167. }
  168. return argument;
  169. }
  170. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_descriptor)
  171. {
  172. auto* object = vm.argument(0).to_object(global_object);
  173. if (vm.exception())
  174. return {};
  175. auto property_key = PropertyName::from_value(global_object, vm.argument(1));
  176. if (vm.exception())
  177. return {};
  178. return object->get_own_property_descriptor_object(property_key);
  179. }
  180. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::define_property_)
  181. {
  182. if (!vm.argument(0).is_object()) {
  183. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Object argument");
  184. return {};
  185. }
  186. auto property_key = vm.argument(1).to_property_key(global_object);
  187. if (vm.exception())
  188. return {};
  189. if (!vm.argument(2).is_object()) {
  190. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Descriptor argument");
  191. return {};
  192. }
  193. auto& object = vm.argument(0).as_object();
  194. auto& descriptor = vm.argument(2).as_object();
  195. if (!object.define_property(property_key, descriptor)) {
  196. if (!vm.exception()) {
  197. if (AK::is<ProxyObject>(object)) {
  198. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectDefinePropertyReturnedFalse);
  199. } else {
  200. vm.throw_exception<TypeError>(global_object, ErrorType::NonExtensibleDefine, property_key.to_display_string());
  201. }
  202. }
  203. return {};
  204. }
  205. return &object;
  206. }
  207. // 20.1.2.3 Object.defineProperties, https://tc39.es/ecma262/#sec-object.defineproperties
  208. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::define_properties)
  209. {
  210. if (!vm.argument(0).is_object()) {
  211. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Object argument");
  212. return {};
  213. }
  214. auto& object = vm.argument(0).as_object();
  215. auto properties = vm.argument(1);
  216. object.define_properties(properties);
  217. if (vm.exception())
  218. return {};
  219. return &object;
  220. }
  221. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is)
  222. {
  223. return Value(same_value(vm.argument(0), vm.argument(1)));
  224. }
  225. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::keys)
  226. {
  227. if (!vm.argument_count()) {
  228. vm.throw_exception<TypeError>(global_object, ErrorType::ConvertUndefinedToObject);
  229. return {};
  230. }
  231. auto* obj_arg = vm.argument(0).to_object(global_object);
  232. if (vm.exception())
  233. return {};
  234. return Array::create_from(global_object, obj_arg->get_enumerable_own_property_names(PropertyKind::Key));
  235. }
  236. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::values)
  237. {
  238. if (!vm.argument_count()) {
  239. vm.throw_exception<TypeError>(global_object, ErrorType::ConvertUndefinedToObject);
  240. return {};
  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::Value));
  246. }
  247. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::entries)
  248. {
  249. if (!vm.argument_count()) {
  250. vm.throw_exception<TypeError>(global_object, ErrorType::ConvertUndefinedToObject);
  251. return {};
  252. }
  253. auto* obj_arg = vm.argument(0).to_object(global_object);
  254. if (vm.exception())
  255. return {};
  256. return Array::create_from(global_object, obj_arg->get_enumerable_own_property_names(PropertyKind::KeyAndValue));
  257. }
  258. // 20.1.2.2 Object.create, https://tc39.es/ecma262/#sec-object.create
  259. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::create)
  260. {
  261. auto prototype_value = vm.argument(0);
  262. auto properties = vm.argument(1);
  263. Object* prototype;
  264. if (prototype_value.is_null()) {
  265. prototype = nullptr;
  266. } else if (prototype_value.is_object()) {
  267. prototype = &prototype_value.as_object();
  268. } else {
  269. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  270. return {};
  271. }
  272. auto* object = Object::create_empty(global_object);
  273. object->set_prototype(prototype);
  274. if (!properties.is_undefined()) {
  275. object->define_properties(properties);
  276. if (vm.exception())
  277. return {};
  278. }
  279. return object;
  280. }
  281. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::has_own)
  282. {
  283. auto* object = vm.argument(0).to_object(global_object);
  284. if (vm.exception())
  285. return {};
  286. auto property_key = vm.argument(1).to_property_key(global_object);
  287. if (vm.exception())
  288. return {};
  289. return Value(object->has_own_property(property_key));
  290. }
  291. }