ObjectConstructor.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Function.h>
  27. #include <LibJS/Heap/Heap.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.is, is, 2, attr);
  48. define_native_function(vm.names.getOwnPropertyDescriptor, get_own_property_descriptor, 2, attr);
  49. define_native_function(vm.names.getOwnPropertyNames, get_own_property_names, 1, attr);
  50. define_native_function(vm.names.getPrototypeOf, get_prototype_of, 1, attr);
  51. define_native_function(vm.names.setPrototypeOf, set_prototype_of, 2, attr);
  52. define_native_function(vm.names.isExtensible, is_extensible, 1, attr);
  53. define_native_function(vm.names.preventExtensions, prevent_extensions, 1, attr);
  54. define_native_function(vm.names.keys, keys, 1, attr);
  55. define_native_function(vm.names.values, values, 1, attr);
  56. define_native_function(vm.names.entries, entries, 1, attr);
  57. }
  58. ObjectConstructor::~ObjectConstructor()
  59. {
  60. }
  61. Value ObjectConstructor::call()
  62. {
  63. auto value = vm().argument(0);
  64. if (value.is_nullish())
  65. return Object::create_empty(global_object());
  66. return value.to_object(global_object());
  67. }
  68. Value ObjectConstructor::construct(Function&)
  69. {
  70. return call();
  71. }
  72. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_names)
  73. {
  74. if (!vm.argument_count())
  75. return {};
  76. auto* object = vm.argument(0).to_object(global_object);
  77. if (vm.exception())
  78. return {};
  79. auto* result = Array::create(global_object);
  80. for (auto& entry : object->indexed_properties())
  81. result->indexed_properties().append(js_string(vm, String::number(entry.index())));
  82. for (auto& it : object->shape().property_table_ordered()) {
  83. if (!it.key.is_string())
  84. continue;
  85. result->indexed_properties().append(js_string(vm, it.key.as_string()));
  86. }
  87. return result;
  88. }
  89. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_prototype_of)
  90. {
  91. if (!vm.argument_count())
  92. return {};
  93. auto* object = vm.argument(0).to_object(global_object);
  94. if (vm.exception())
  95. return {};
  96. return object->prototype();
  97. }
  98. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::set_prototype_of)
  99. {
  100. if (vm.argument_count() < 2) {
  101. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSetPrototypeOfTwoArgs);
  102. return {};
  103. }
  104. auto* object = vm.argument(0).to_object(global_object);
  105. if (vm.exception())
  106. return {};
  107. auto prototype_value = vm.argument(1);
  108. Object* prototype;
  109. if (prototype_value.is_null()) {
  110. prototype = nullptr;
  111. } else if (prototype_value.is_object()) {
  112. prototype = &prototype_value.as_object();
  113. } else {
  114. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  115. return {};
  116. }
  117. if (!object->set_prototype(prototype)) {
  118. if (!vm.exception())
  119. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSetPrototypeOfReturnedFalse);
  120. return {};
  121. }
  122. return object;
  123. }
  124. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_extensible)
  125. {
  126. auto argument = vm.argument(0);
  127. if (!argument.is_object())
  128. return Value(false);
  129. return Value(argument.as_object().is_extensible());
  130. }
  131. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::prevent_extensions)
  132. {
  133. auto argument = vm.argument(0);
  134. if (!argument.is_object())
  135. return argument;
  136. if (!argument.as_object().prevent_extensions()) {
  137. if (!vm.exception())
  138. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPreventExtensionsReturnedFalse);
  139. return {};
  140. }
  141. return argument;
  142. }
  143. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_descriptor)
  144. {
  145. auto* object = vm.argument(0).to_object(global_object);
  146. if (vm.exception())
  147. return {};
  148. auto property_key = PropertyName::from_value(global_object, vm.argument(1));
  149. if (vm.exception())
  150. return {};
  151. return object->get_own_property_descriptor_object(property_key);
  152. }
  153. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::define_property_)
  154. {
  155. if (!vm.argument(0).is_object()) {
  156. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Object argument");
  157. return {};
  158. }
  159. if (!vm.argument(2).is_object()) {
  160. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Descriptor argument");
  161. return {};
  162. }
  163. auto& object = vm.argument(0).as_object();
  164. auto property_key = StringOrSymbol::from_value(global_object, vm.argument(1));
  165. if (vm.exception())
  166. return {};
  167. auto& descriptor = vm.argument(2).as_object();
  168. if (!object.define_property(property_key, descriptor)) {
  169. if (!vm.exception()) {
  170. if (AK::is<ProxyObject>(object)) {
  171. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectDefinePropertyReturnedFalse);
  172. } else {
  173. vm.throw_exception<TypeError>(global_object, ErrorType::NonExtensibleDefine, property_key.to_display_string());
  174. }
  175. }
  176. return {};
  177. }
  178. return &object;
  179. }
  180. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is)
  181. {
  182. return Value(same_value(vm.argument(0), vm.argument(1)));
  183. }
  184. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::keys)
  185. {
  186. if (!vm.argument_count()) {
  187. vm.throw_exception<TypeError>(global_object, ErrorType::ConvertUndefinedToObject);
  188. return {};
  189. }
  190. auto* obj_arg = vm.argument(0).to_object(global_object);
  191. if (vm.exception())
  192. return {};
  193. return obj_arg->get_own_properties(*obj_arg, PropertyKind::Key, true);
  194. }
  195. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::values)
  196. {
  197. if (!vm.argument_count()) {
  198. vm.throw_exception<TypeError>(global_object, ErrorType::ConvertUndefinedToObject);
  199. return {};
  200. }
  201. auto* obj_arg = vm.argument(0).to_object(global_object);
  202. if (vm.exception())
  203. return {};
  204. return obj_arg->get_own_properties(*obj_arg, PropertyKind::Value, true);
  205. }
  206. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::entries)
  207. {
  208. if (!vm.argument_count()) {
  209. vm.throw_exception<TypeError>(global_object, ErrorType::ConvertUndefinedToObject);
  210. return {};
  211. }
  212. auto* obj_arg = vm.argument(0).to_object(global_object);
  213. if (vm.exception())
  214. return {};
  215. return obj_arg->get_own_properties(*obj_arg, PropertyKind::KeyAndValue, true);
  216. }
  217. }