ObjectConstructor.cpp 6.0 KB

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