ObjectConstructor.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/Function.h>
  28. #include <LibJS/Runtime/Array.h>
  29. #include <LibJS/Runtime/Error.h>
  30. #include <LibJS/Runtime/GlobalObject.h>
  31. #include <LibJS/Runtime/ObjectConstructor.h>
  32. #include <LibJS/Runtime/ProxyObject.h>
  33. #include <LibJS/Runtime/Shape.h>
  34. namespace JS {
  35. ObjectConstructor::ObjectConstructor(GlobalObject& global_object)
  36. : NativeFunction(vm().names.Object, *global_object.function_prototype())
  37. {
  38. }
  39. void ObjectConstructor::initialize(GlobalObject& global_object)
  40. {
  41. auto& vm = this->vm();
  42. NativeFunction::initialize(global_object);
  43. define_property(vm.names.prototype, global_object.object_prototype(), 0);
  44. define_property(vm.names.length, Value(1), Attribute::Configurable);
  45. u8 attr = Attribute::Writable | Attribute::Configurable;
  46. define_native_function(vm.names.defineProperty, define_property_, 3, attr);
  47. define_native_function(vm.names.defineProperties, define_properties, 2, attr);
  48. define_native_function(vm.names.is, is, 2, attr);
  49. define_native_function(vm.names.getOwnPropertyDescriptor, get_own_property_descriptor, 2, attr);
  50. define_native_function(vm.names.getOwnPropertyNames, get_own_property_names, 1, attr);
  51. define_native_function(vm.names.getPrototypeOf, get_prototype_of, 1, attr);
  52. define_native_function(vm.names.setPrototypeOf, set_prototype_of, 2, attr);
  53. define_native_function(vm.names.isExtensible, is_extensible, 1, attr);
  54. define_native_function(vm.names.isFrozen, is_frozen, 1, attr);
  55. define_native_function(vm.names.isSealed, is_sealed, 1, attr);
  56. define_native_function(vm.names.preventExtensions, prevent_extensions, 1, attr);
  57. define_native_function(vm.names.freeze, freeze, 1, attr);
  58. define_native_function(vm.names.seal, seal, 1, attr);
  59. define_native_function(vm.names.keys, keys, 1, attr);
  60. define_native_function(vm.names.values, values, 1, attr);
  61. define_native_function(vm.names.entries, entries, 1, attr);
  62. define_native_function(vm.names.create, create, 2, attr);
  63. }
  64. ObjectConstructor::~ObjectConstructor()
  65. {
  66. }
  67. Value ObjectConstructor::call()
  68. {
  69. auto value = vm().argument(0);
  70. if (value.is_nullish())
  71. return Object::create_empty(global_object());
  72. return value.to_object(global_object());
  73. }
  74. Value ObjectConstructor::construct(Function&)
  75. {
  76. return call();
  77. }
  78. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_names)
  79. {
  80. if (!vm.argument_count())
  81. return {};
  82. auto* object = vm.argument(0).to_object(global_object);
  83. if (vm.exception())
  84. return {};
  85. auto* result = Array::create(global_object);
  86. for (auto& entry : object->indexed_properties())
  87. result->indexed_properties().append(js_string(vm, String::number(entry.index())));
  88. for (auto& it : object->shape().property_table_ordered()) {
  89. if (!it.key.is_string())
  90. continue;
  91. result->indexed_properties().append(js_string(vm, it.key.as_string()));
  92. }
  93. return result;
  94. }
  95. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_prototype_of)
  96. {
  97. if (!vm.argument_count())
  98. return {};
  99. auto* object = vm.argument(0).to_object(global_object);
  100. if (vm.exception())
  101. return {};
  102. return object->prototype();
  103. }
  104. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::set_prototype_of)
  105. {
  106. if (vm.argument_count() < 2) {
  107. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSetPrototypeOfTwoArgs);
  108. return {};
  109. }
  110. auto* object = vm.argument(0).to_object(global_object);
  111. if (vm.exception())
  112. return {};
  113. auto prototype_value = vm.argument(1);
  114. Object* prototype;
  115. if (prototype_value.is_null()) {
  116. prototype = nullptr;
  117. } else if (prototype_value.is_object()) {
  118. prototype = &prototype_value.as_object();
  119. } else {
  120. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  121. return {};
  122. }
  123. if (!object->set_prototype(prototype)) {
  124. if (!vm.exception())
  125. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSetPrototypeOfReturnedFalse);
  126. return {};
  127. }
  128. return object;
  129. }
  130. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_extensible)
  131. {
  132. auto argument = vm.argument(0);
  133. if (!argument.is_object())
  134. return Value(false);
  135. return Value(argument.as_object().is_extensible());
  136. }
  137. // 20.1.2.15 Object.isFrozen, https://tc39.es/ecma262/#sec-object.isfrozen
  138. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_frozen)
  139. {
  140. auto argument = vm.argument(0);
  141. if (!argument.is_object())
  142. return Value(true);
  143. return Value(argument.as_object().test_integrity_level(Object::IntegrityLevel::Frozen));
  144. }
  145. // 20.1.2.16 Object.isSealed, https://tc39.es/ecma262/#sec-object.issealed
  146. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_sealed)
  147. {
  148. auto argument = vm.argument(0);
  149. if (!argument.is_object())
  150. return Value(true);
  151. return Value(argument.as_object().test_integrity_level(Object::IntegrityLevel::Sealed));
  152. }
  153. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::prevent_extensions)
  154. {
  155. auto argument = vm.argument(0);
  156. if (!argument.is_object())
  157. return argument;
  158. auto status = argument.as_object().prevent_extensions();
  159. if (vm.exception())
  160. return {};
  161. if (!status) {
  162. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPreventExtensionsReturnedFalse);
  163. return {};
  164. }
  165. return argument;
  166. }
  167. // 20.1.2.6 Object.freeze, https://tc39.es/ecma262/#sec-object.freeze
  168. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::freeze)
  169. {
  170. auto argument = vm.argument(0);
  171. if (!argument.is_object())
  172. return argument;
  173. auto status = argument.as_object().set_integrity_level(Object::IntegrityLevel::Frozen);
  174. if (vm.exception())
  175. return {};
  176. if (!status) {
  177. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectFreezeFailed);
  178. return {};
  179. }
  180. return argument;
  181. }
  182. // 20.1.2.20 Object.seal, https://tc39.es/ecma262/#sec-object.seal
  183. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::seal)
  184. {
  185. auto argument = vm.argument(0);
  186. if (!argument.is_object())
  187. return argument;
  188. auto status = argument.as_object().set_integrity_level(Object::IntegrityLevel::Sealed);
  189. if (vm.exception())
  190. return {};
  191. if (!status) {
  192. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSealFailed);
  193. return {};
  194. }
  195. return argument;
  196. }
  197. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_descriptor)
  198. {
  199. auto* object = vm.argument(0).to_object(global_object);
  200. if (vm.exception())
  201. return {};
  202. auto property_key = PropertyName::from_value(global_object, vm.argument(1));
  203. if (vm.exception())
  204. return {};
  205. return object->get_own_property_descriptor_object(property_key);
  206. }
  207. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::define_property_)
  208. {
  209. if (!vm.argument(0).is_object()) {
  210. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Object argument");
  211. return {};
  212. }
  213. if (!vm.argument(2).is_object()) {
  214. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Descriptor argument");
  215. return {};
  216. }
  217. auto& object = vm.argument(0).as_object();
  218. auto property_key = StringOrSymbol::from_value(global_object, vm.argument(1));
  219. if (vm.exception())
  220. return {};
  221. auto& descriptor = vm.argument(2).as_object();
  222. if (!object.define_property(property_key, descriptor)) {
  223. if (!vm.exception()) {
  224. if (AK::is<ProxyObject>(object)) {
  225. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectDefinePropertyReturnedFalse);
  226. } else {
  227. vm.throw_exception<TypeError>(global_object, ErrorType::NonExtensibleDefine, property_key.to_display_string());
  228. }
  229. }
  230. return {};
  231. }
  232. return &object;
  233. }
  234. // 20.1.2.3 Object.defineProperties, https://tc39.es/ecma262/#sec-object.defineproperties
  235. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::define_properties)
  236. {
  237. if (!vm.argument(0).is_object()) {
  238. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Object argument");
  239. return {};
  240. }
  241. auto& object = vm.argument(0).as_object();
  242. auto properties = vm.argument(1);
  243. object.define_properties(properties);
  244. if (vm.exception())
  245. return {};
  246. return &object;
  247. }
  248. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is)
  249. {
  250. return Value(same_value(vm.argument(0), vm.argument(1)));
  251. }
  252. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::keys)
  253. {
  254. if (!vm.argument_count()) {
  255. vm.throw_exception<TypeError>(global_object, ErrorType::ConvertUndefinedToObject);
  256. return {};
  257. }
  258. auto* obj_arg = vm.argument(0).to_object(global_object);
  259. if (vm.exception())
  260. return {};
  261. return Array::create_from(global_object, obj_arg->get_enumerable_own_property_names(PropertyKind::Key));
  262. }
  263. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::values)
  264. {
  265. if (!vm.argument_count()) {
  266. vm.throw_exception<TypeError>(global_object, ErrorType::ConvertUndefinedToObject);
  267. return {};
  268. }
  269. auto* obj_arg = vm.argument(0).to_object(global_object);
  270. if (vm.exception())
  271. return {};
  272. return Array::create_from(global_object, obj_arg->get_enumerable_own_property_names(PropertyKind::Value));
  273. }
  274. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::entries)
  275. {
  276. if (!vm.argument_count()) {
  277. vm.throw_exception<TypeError>(global_object, ErrorType::ConvertUndefinedToObject);
  278. return {};
  279. }
  280. auto* obj_arg = vm.argument(0).to_object(global_object);
  281. if (vm.exception())
  282. return {};
  283. return Array::create_from(global_object, obj_arg->get_enumerable_own_property_names(PropertyKind::KeyAndValue));
  284. }
  285. // 20.1.2.2 Object.create, https://tc39.es/ecma262/#sec-object.create
  286. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::create)
  287. {
  288. auto prototype_value = vm.argument(0);
  289. auto properties = vm.argument(1);
  290. Object* prototype;
  291. if (prototype_value.is_null()) {
  292. prototype = nullptr;
  293. } else if (prototype_value.is_object()) {
  294. prototype = &prototype_value.as_object();
  295. } else {
  296. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  297. return {};
  298. }
  299. auto* object = Object::create_empty(global_object);
  300. object->set_prototype(prototype);
  301. if (!properties.is_undefined()) {
  302. object->define_properties(properties);
  303. if (vm.exception())
  304. return {};
  305. }
  306. return object;
  307. }
  308. }