ObjectConstructor.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/Shape.h>
  33. namespace JS {
  34. ObjectConstructor::ObjectConstructor(GlobalObject& global_object)
  35. : NativeFunction("Object", *global_object.function_prototype())
  36. {
  37. }
  38. void ObjectConstructor::initialize(GlobalObject& global_object)
  39. {
  40. NativeFunction::initialize(global_object);
  41. define_property("prototype", global_object.object_prototype(), 0);
  42. define_property("length", Value(1), Attribute::Configurable);
  43. u8 attr = Attribute::Writable | Attribute::Configurable;
  44. define_native_function("defineProperty", define_property_, 3, attr);
  45. define_native_function("is", is, 2, attr);
  46. define_native_function("getOwnPropertyDescriptor", get_own_property_descriptor, 2, attr);
  47. define_native_function("getOwnPropertyNames", get_own_property_names, 1, attr);
  48. define_native_function("getPrototypeOf", get_prototype_of, 1, attr);
  49. define_native_function("setPrototypeOf", set_prototype_of, 2, attr);
  50. define_native_function("isExtensible", is_extensible, 1, attr);
  51. define_native_function("preventExtensions", prevent_extensions, 1, attr);
  52. define_native_function("keys", keys, 1, attr);
  53. define_native_function("values", values, 1, attr);
  54. define_native_function("entries", entries, 1, attr);
  55. }
  56. ObjectConstructor::~ObjectConstructor()
  57. {
  58. }
  59. Value ObjectConstructor::call()
  60. {
  61. return Object::create_empty(global_object());
  62. }
  63. Value ObjectConstructor::construct(Function&)
  64. {
  65. return call();
  66. }
  67. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_names)
  68. {
  69. if (!vm.argument_count())
  70. return {};
  71. auto* object = vm.argument(0).to_object(global_object);
  72. if (vm.exception())
  73. return {};
  74. auto* result = Array::create(global_object);
  75. for (auto& entry : object->indexed_properties())
  76. result->indexed_properties().append(js_string(vm, String::number(entry.index())));
  77. for (auto& it : object->shape().property_table_ordered()) {
  78. if (!it.key.is_string())
  79. continue;
  80. result->indexed_properties().append(js_string(vm, it.key.as_string()));
  81. }
  82. return result;
  83. }
  84. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_prototype_of)
  85. {
  86. if (!vm.argument_count())
  87. return {};
  88. auto* object = vm.argument(0).to_object(global_object);
  89. if (vm.exception())
  90. return {};
  91. return object->prototype();
  92. }
  93. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::set_prototype_of)
  94. {
  95. if (vm.argument_count() < 2) {
  96. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSetPrototypeOfTwoArgs);
  97. return {};
  98. }
  99. auto* object = vm.argument(0).to_object(global_object);
  100. if (vm.exception())
  101. return {};
  102. auto prototype_value = vm.argument(1);
  103. Object* prototype;
  104. if (prototype_value.is_null()) {
  105. prototype = nullptr;
  106. } else if (prototype_value.is_object()) {
  107. prototype = &prototype_value.as_object();
  108. } else {
  109. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
  110. return {};
  111. }
  112. if (!object->set_prototype(prototype)) {
  113. if (!vm.exception())
  114. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectSetPrototypeOfReturnedFalse);
  115. return {};
  116. }
  117. return object;
  118. }
  119. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is_extensible)
  120. {
  121. auto argument = vm.argument(0);
  122. if (!argument.is_object())
  123. return Value(false);
  124. return Value(argument.as_object().is_extensible());
  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. if (!argument.as_object().prevent_extensions()) {
  132. if (!vm.exception())
  133. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPreventExtensionsReturnedFalse);
  134. return {};
  135. }
  136. return argument;
  137. }
  138. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_descriptor)
  139. {
  140. auto* object = vm.argument(0).to_object(global_object);
  141. if (vm.exception())
  142. return {};
  143. auto property_key = PropertyName::from_value(global_object, vm.argument(1));
  144. if (vm.exception())
  145. return {};
  146. return object->get_own_property_descriptor_object(property_key);
  147. }
  148. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::define_property_)
  149. {
  150. if (!vm.argument(0).is_object()) {
  151. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Object argument");
  152. return {};
  153. }
  154. if (!vm.argument(2).is_object()) {
  155. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, "Descriptor argument");
  156. return {};
  157. }
  158. auto& object = vm.argument(0).as_object();
  159. auto property_key = StringOrSymbol::from_value(global_object, vm.argument(1));
  160. if (vm.exception())
  161. return {};
  162. auto& descriptor = vm.argument(2).as_object();
  163. if (!object.define_property(property_key, descriptor)) {
  164. if (!vm.exception()) {
  165. if (object.is_proxy_object()) {
  166. vm.throw_exception<TypeError>(global_object, ErrorType::ObjectDefinePropertyReturnedFalse);
  167. } else {
  168. vm.throw_exception<TypeError>(global_object, ErrorType::NonExtensibleDefine, property_key.to_display_string().characters());
  169. }
  170. }
  171. return {};
  172. }
  173. return &object;
  174. }
  175. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::is)
  176. {
  177. return Value(same_value(vm.argument(0), vm.argument(1)));
  178. }
  179. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::keys)
  180. {
  181. if (!vm.argument_count()) {
  182. vm.throw_exception<TypeError>(global_object, ErrorType::ConvertUndefinedToObject);
  183. return {};
  184. }
  185. auto* obj_arg = vm.argument(0).to_object(global_object);
  186. if (vm.exception())
  187. return {};
  188. return obj_arg->get_own_properties(*obj_arg, PropertyKind::Key, true);
  189. }
  190. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::values)
  191. {
  192. if (!vm.argument_count()) {
  193. vm.throw_exception<TypeError>(global_object, ErrorType::ConvertUndefinedToObject);
  194. return {};
  195. }
  196. auto* obj_arg = vm.argument(0).to_object(global_object);
  197. if (vm.exception())
  198. return {};
  199. return obj_arg->get_own_properties(*obj_arg, PropertyKind::Value, true);
  200. }
  201. JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::entries)
  202. {
  203. if (!vm.argument_count()) {
  204. vm.throw_exception<TypeError>(global_object, ErrorType::ConvertUndefinedToObject);
  205. return {};
  206. }
  207. auto* obj_arg = vm.argument(0).to_object(global_object);
  208. if (vm.exception())
  209. return {};
  210. return obj_arg->get_own_properties(*obj_arg, PropertyKind::KeyAndValue, true);
  211. }
  212. }