ObjectConstructor.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/Interpreter.h>
  29. #include <LibJS/Runtime/Array.h>
  30. #include <LibJS/Runtime/Error.h>
  31. #include <LibJS/Runtime/GlobalObject.h>
  32. #include <LibJS/Runtime/ObjectConstructor.h>
  33. #include <LibJS/Runtime/Shape.h>
  34. namespace JS {
  35. ObjectConstructor::ObjectConstructor()
  36. : NativeFunction("Object", *interpreter().global_object().function_prototype())
  37. {
  38. put("prototype", interpreter().global_object().object_prototype(), 0);
  39. u8 attr = Attribute::Writable | Attribute::Configurable;
  40. put_native_function("defineProperty", define_property, 3, attr);
  41. put_native_function("is", is, 2, attr);
  42. put_native_function("getOwnPropertyDescriptor", get_own_property_descriptor, 2, attr);
  43. put_native_function("getOwnPropertyNames", get_own_property_names, 1, attr);
  44. put_native_function("getPrototypeOf", get_prototype_of, 1, attr);
  45. put_native_function("setPrototypeOf", set_prototype_of, 2, attr);
  46. put_native_function("keys", keys, 1, attr);
  47. put_native_function("values", values, 1, attr);
  48. put_native_function("entries", entries, 1, attr);
  49. }
  50. ObjectConstructor::~ObjectConstructor()
  51. {
  52. }
  53. Value ObjectConstructor::call(Interpreter& interpreter)
  54. {
  55. return Object::create_empty(interpreter, interpreter.global_object());
  56. }
  57. Value ObjectConstructor::construct(Interpreter& interpreter)
  58. {
  59. return call(interpreter);
  60. }
  61. Value ObjectConstructor::get_own_property_names(Interpreter& interpreter)
  62. {
  63. if (!interpreter.argument_count())
  64. return {};
  65. auto* object = interpreter.argument(0).to_object(interpreter.heap());
  66. if (interpreter.exception())
  67. return {};
  68. auto* result = Array::create(interpreter.global_object());
  69. for (size_t i = 0; i < object->elements().size(); ++i) {
  70. if (!object->elements()[i].is_empty())
  71. result->elements().append(js_string(interpreter, String::number(i)));
  72. }
  73. for (auto& it : object->shape().property_table_ordered()) {
  74. result->elements().append(js_string(interpreter, it.key));
  75. }
  76. return result;
  77. }
  78. Value ObjectConstructor::get_prototype_of(Interpreter& interpreter)
  79. {
  80. if (!interpreter.argument_count())
  81. return {};
  82. auto* object = interpreter.argument(0).to_object(interpreter.heap());
  83. if (interpreter.exception())
  84. return {};
  85. return object->prototype();
  86. }
  87. Value ObjectConstructor::set_prototype_of(Interpreter& interpreter)
  88. {
  89. if (interpreter.argument_count() < 2)
  90. return {};
  91. auto* object = interpreter.argument(0).to_object(interpreter.heap());
  92. if (interpreter.exception())
  93. return {};
  94. object->set_prototype(&const_cast<Object&>(interpreter.argument(1).as_object()));
  95. return {};
  96. }
  97. Value ObjectConstructor::get_own_property_descriptor(Interpreter& interpreter)
  98. {
  99. if (interpreter.argument_count() < 2)
  100. return interpreter.throw_exception<TypeError>("Object.getOwnPropertyDescriptor() needs 2 arguments");
  101. if (!interpreter.argument(0).is_object())
  102. return interpreter.throw_exception<TypeError>("Object argument is not an object");
  103. auto& object = interpreter.argument(0).as_object();
  104. auto metadata = object.shape().lookup(interpreter.argument(1).to_string());
  105. if (!metadata.has_value())
  106. return js_undefined();
  107. auto value = object.get(interpreter.argument(1).to_string()).value_or(js_undefined());
  108. if (interpreter.exception())
  109. return {};
  110. auto* descriptor = Object::create_empty(interpreter, interpreter.global_object());
  111. descriptor->put("configurable", Value(!!(metadata.value().attributes & Attribute::Configurable)));
  112. descriptor->put("enumerable", Value(!!(metadata.value().attributes & Attribute::Enumerable)));
  113. descriptor->put("writable", Value(!!(metadata.value().attributes & Attribute::Writable)));
  114. descriptor->put("value", value);
  115. return descriptor;
  116. }
  117. Value ObjectConstructor::define_property(Interpreter& interpreter)
  118. {
  119. if (interpreter.argument_count() < 3)
  120. return interpreter.throw_exception<TypeError>("Object.defineProperty() needs 3 arguments");
  121. if (!interpreter.argument(0).is_object())
  122. return interpreter.throw_exception<TypeError>("Object argument is not an object");
  123. if (!interpreter.argument(2).is_object())
  124. return interpreter.throw_exception<TypeError>("Descriptor argument is not an object");
  125. auto& object = interpreter.argument(0).as_object();
  126. auto& descriptor = interpreter.argument(2).as_object();
  127. auto value = descriptor.get("value");
  128. u8 configurable = descriptor.get("configurable").value_or(Value(false)).to_boolean() * Attribute::Configurable;
  129. u8 enumerable = descriptor.get("enumerable").value_or(Value(false)).to_boolean() * Attribute::Enumerable;
  130. u8 writable = descriptor.get("writable").value_or(Value(false)).to_boolean() * Attribute::Writable;
  131. u8 attributes = configurable | enumerable | writable;
  132. dbg() << "Defining new property " << interpreter.argument(1).to_string() << " with descriptor { " << configurable << ", " << enumerable << ", " << writable << ", attributes=" << attributes << " }";
  133. object.put_own_property(object, interpreter.argument(1).to_string(), attributes, value, PutOwnPropertyMode::DefineProperty);
  134. return &object;
  135. }
  136. Value ObjectConstructor::is(Interpreter& interpreter)
  137. {
  138. auto value1 = interpreter.argument(0);
  139. auto value2 = interpreter.argument(1);
  140. if (value1.is_nan() && value2.is_nan())
  141. return Value(true);
  142. if (value1.is_number() && value1.as_double() == 0 && value2.is_number() && value2.as_double() == 0) {
  143. if (value1.is_positive_zero() && value2.is_positive_zero())
  144. return Value(true);
  145. if (value1.is_negative_zero() && value2.is_negative_zero())
  146. return Value(true);
  147. return Value(false);
  148. }
  149. return typed_eq(interpreter, value1, value2);
  150. }
  151. Value ObjectConstructor::keys(Interpreter& interpreter)
  152. {
  153. if (!interpreter.argument_count())
  154. return interpreter.throw_exception<TypeError>("Can't convert undefined to object");
  155. auto* obj_arg = interpreter.argument(0).to_object(interpreter.heap());
  156. if (interpreter.exception())
  157. return {};
  158. return obj_arg->get_enumerable_own_properties(*obj_arg, GetOwnPropertyMode::Key);
  159. }
  160. Value ObjectConstructor::values(Interpreter& interpreter)
  161. {
  162. if (!interpreter.argument_count())
  163. return interpreter.throw_exception<TypeError>("Can't convert undefined to object");
  164. auto* obj_arg = interpreter.argument(0).to_object(interpreter.heap());
  165. if (interpreter.exception())
  166. return {};
  167. return obj_arg->get_enumerable_own_properties(*obj_arg, GetOwnPropertyMode::Value);
  168. }
  169. Value ObjectConstructor::entries(Interpreter& interpreter)
  170. {
  171. if (!interpreter.argument_count())
  172. return interpreter.throw_exception<TypeError>("Can't convert undefined to object");
  173. auto* obj_arg = interpreter.argument(0).to_object(interpreter.heap());
  174. if (interpreter.exception())
  175. return {};
  176. return obj_arg->get_enumerable_own_properties(*obj_arg, GetOwnPropertyMode::KeyAndValue);
  177. }
  178. }