Function.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <LibJS/Interpreter.h>
  27. #include <LibJS/Runtime/BoundFunction.h>
  28. #include <LibJS/Runtime/Function.h>
  29. #include <LibJS/Runtime/GlobalObject.h>
  30. namespace JS {
  31. Function::Function(Object& prototype)
  32. : Function(prototype, {}, {})
  33. {
  34. }
  35. Function::Function(Object& prototype, Value bound_this, Vector<Value> bound_arguments)
  36. : Object(&prototype)
  37. , m_bound_this(bound_this)
  38. , m_bound_arguments(move(bound_arguments))
  39. {
  40. }
  41. BoundFunction* Function::bind(Value bound_this_value, Vector<Value> arguments)
  42. {
  43. Function& target_function = is_bound_function() ? static_cast<BoundFunction&>(*this).target_function() : *this;
  44. auto bound_this_object = [bound_this_value, this]() -> Value {
  45. if (!m_bound_this.is_empty())
  46. return m_bound_this;
  47. switch (bound_this_value.type()) {
  48. case Value::Type::Undefined:
  49. case Value::Type::Null:
  50. // FIXME: Null or undefined should be passed through in strict mode.
  51. return &interpreter().global_object();
  52. default:
  53. return bound_this_value.to_object(interpreter().heap());
  54. }
  55. }();
  56. i32 computed_length = 0;
  57. auto length_property = get("length");
  58. if (interpreter().exception()) {
  59. return nullptr;
  60. }
  61. if (length_property.is_number()) {
  62. computed_length = max(0, length_property.to_i32() - static_cast<i32>(arguments.size()));
  63. }
  64. Object* constructor_prototype = nullptr;
  65. auto prototype_property = target_function.get("prototype");
  66. if (interpreter().exception()) {
  67. return nullptr;
  68. }
  69. if (prototype_property.is_object()) {
  70. constructor_prototype = &prototype_property.as_object();
  71. }
  72. auto all_bound_arguments = bound_arguments();
  73. all_bound_arguments.append(move(arguments));
  74. return interpreter().heap().allocate<BoundFunction>(target_function, bound_this_object, move(all_bound_arguments), computed_length, constructor_prototype);
  75. }
  76. void Function::visit_children(Visitor& visitor)
  77. {
  78. Object::visit_children(visitor);
  79. visitor.visit(m_bound_this);
  80. for (auto argument : m_bound_arguments) {
  81. visitor.visit(argument);
  82. }
  83. }
  84. Function::~Function()
  85. {
  86. }
  87. }