ObjectConstructor.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. }
  47. ObjectConstructor::~ObjectConstructor()
  48. {
  49. }
  50. Value ObjectConstructor::call(Interpreter& interpreter)
  51. {
  52. return Object::create_empty(interpreter, interpreter.global_object());
  53. }
  54. Value ObjectConstructor::construct(Interpreter& interpreter)
  55. {
  56. return call(interpreter);
  57. }
  58. Value ObjectConstructor::get_own_property_names(Interpreter& interpreter)
  59. {
  60. if (!interpreter.argument_count())
  61. return {};
  62. auto* object = interpreter.argument(0).to_object(interpreter.heap());
  63. if (interpreter.exception())
  64. return {};
  65. auto* result = Array::create(interpreter.global_object());
  66. for (size_t i = 0; i < object->elements().size(); ++i) {
  67. if (!object->elements()[i].is_empty())
  68. result->elements().append(js_string(interpreter, String::number(i)));
  69. }
  70. for (auto& it : object->shape().property_table_ordered()) {
  71. result->elements().append(js_string(interpreter, it.key));
  72. }
  73. return result;
  74. }
  75. Value ObjectConstructor::get_prototype_of(Interpreter& interpreter)
  76. {
  77. if (!interpreter.argument_count())
  78. return {};
  79. auto* object = interpreter.argument(0).to_object(interpreter.heap());
  80. if (interpreter.exception())
  81. return {};
  82. return object->prototype();
  83. }
  84. Value ObjectConstructor::set_prototype_of(Interpreter& interpreter)
  85. {
  86. if (interpreter.argument_count() < 2)
  87. return {};
  88. auto* object = interpreter.argument(0).to_object(interpreter.heap());
  89. if (interpreter.exception())
  90. return {};
  91. object->set_prototype(&const_cast<Object&>(interpreter.argument(1).as_object()));
  92. return {};
  93. }
  94. Value ObjectConstructor::get_own_property_descriptor(Interpreter& interpreter)
  95. {
  96. if (interpreter.argument_count() < 2)
  97. return interpreter.throw_exception<TypeError>("Object.getOwnPropertyDescriptor() needs 2 arguments");
  98. if (!interpreter.argument(0).is_object())
  99. return interpreter.throw_exception<TypeError>("Object argument is not an object");
  100. auto& object = interpreter.argument(0).as_object();
  101. auto metadata = object.shape().lookup(interpreter.argument(1).to_string());
  102. if (!metadata.has_value())
  103. return js_undefined();
  104. auto value = object.get(interpreter.argument(1).to_string()).value_or(js_undefined());
  105. if (interpreter.exception())
  106. return {};
  107. auto* descriptor = Object::create_empty(interpreter, interpreter.global_object());
  108. descriptor->put("configurable", Value(!!(metadata.value().attributes & Attribute::Configurable)));
  109. descriptor->put("enumerable", Value(!!(metadata.value().attributes & Attribute::Enumerable)));
  110. descriptor->put("writable", Value(!!(metadata.value().attributes & Attribute::Writable)));
  111. descriptor->put("value", value);
  112. return descriptor;
  113. }
  114. Value ObjectConstructor::define_property(Interpreter& interpreter)
  115. {
  116. if (interpreter.argument_count() < 3)
  117. return interpreter.throw_exception<TypeError>("Object.defineProperty() needs 3 arguments");
  118. if (!interpreter.argument(0).is_object())
  119. return interpreter.throw_exception<TypeError>("Object argument is not an object");
  120. if (!interpreter.argument(2).is_object())
  121. return interpreter.throw_exception<TypeError>("Descriptor argument is not an object");
  122. auto& object = interpreter.argument(0).as_object();
  123. auto& descriptor = interpreter.argument(2).as_object();
  124. auto value = descriptor.get("value");
  125. u8 configurable = descriptor.get("configurable").value_or(Value(false)).to_boolean() * Attribute::Configurable;
  126. u8 enumerable = descriptor.get("enumerable").value_or(Value(false)).to_boolean() * Attribute::Enumerable;
  127. u8 writable = descriptor.get("writable").value_or(Value(false)).to_boolean() * Attribute::Writable;
  128. u8 attributes = configurable | enumerable | writable;
  129. dbg() << "Defining new property " << interpreter.argument(1).to_string() << " with descriptor { " << configurable << ", " << enumerable << ", " << writable << ", attributes=" << attributes << " }";
  130. object.put_own_property(object, interpreter.argument(1).to_string(), attributes, value, PutOwnPropertyMode::DefineProperty);
  131. return &object;
  132. }
  133. Value ObjectConstructor::is(Interpreter& interpreter)
  134. {
  135. auto value1 = interpreter.argument(0);
  136. auto value2 = interpreter.argument(1);
  137. if (value1.is_nan() && value2.is_nan())
  138. return Value(true);
  139. if (value1.is_number() && value1.as_double() == 0 && value2.is_number() && value2.as_double() == 0) {
  140. if (value1.is_positive_zero() && value2.is_positive_zero())
  141. return Value(true);
  142. if (value1.is_negative_zero() && value2.is_negative_zero())
  143. return Value(true);
  144. return Value(false);
  145. }
  146. return typed_eq(interpreter, value1, value2);
  147. }
  148. }