ObjectConstructor.cpp 8.2 KB

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