Function.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. Function::~Function()
  42. {
  43. }
  44. BoundFunction* Function::bind(Value bound_this_value, Vector<Value> arguments)
  45. {
  46. auto& vm = this->vm();
  47. Function& target_function = is_bound_function() ? static_cast<BoundFunction&>(*this).target_function() : *this;
  48. auto bound_this_object = [&vm, bound_this_value, this]() -> Value {
  49. if (!m_bound_this.is_empty())
  50. return m_bound_this;
  51. switch (bound_this_value.type()) {
  52. case Value::Type::Undefined:
  53. case Value::Type::Null:
  54. if (vm.in_strict_mode())
  55. return bound_this_value;
  56. return &global_object();
  57. default:
  58. return bound_this_value.to_object(global_object());
  59. }
  60. }();
  61. i32 computed_length = 0;
  62. auto length_property = get(vm.names.length);
  63. if (vm.exception())
  64. return nullptr;
  65. if (length_property.is_number())
  66. computed_length = max(0, length_property.as_i32() - static_cast<i32>(arguments.size()));
  67. Object* constructor_prototype = nullptr;
  68. auto prototype_property = target_function.get(vm.names.prototype);
  69. if (vm.exception())
  70. return nullptr;
  71. if (prototype_property.is_object())
  72. constructor_prototype = &prototype_property.as_object();
  73. auto all_bound_arguments = bound_arguments();
  74. all_bound_arguments.append(move(arguments));
  75. return heap().allocate<BoundFunction>(global_object(), global_object(), target_function, bound_this_object, move(all_bound_arguments), computed_length, constructor_prototype);
  76. }
  77. void Function::visit_edges(Visitor& visitor)
  78. {
  79. Object::visit_edges(visitor);
  80. visitor.visit(m_bound_this);
  81. for (auto argument : m_bound_arguments)
  82. visitor.visit(argument);
  83. }
  84. }