ObjectConstructor.cpp 6.1 KB

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