ObjectConstructor.cpp 10 KB

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