ObjectConstructor.cpp 6.2 KB

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