Function.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. Function& target_function = is_bound_function() ? static_cast<BoundFunction&>(*this).target_function() : *this;
  47. auto bound_this_object = [bound_this_value, this]() -> Value {
  48. if (!m_bound_this.is_empty())
  49. return m_bound_this;
  50. switch (bound_this_value.type()) {
  51. case Value::Type::Undefined:
  52. case Value::Type::Null:
  53. if (interpreter().in_strict_mode())
  54. return bound_this_value;
  55. return &interpreter().global_object();
  56. default:
  57. return bound_this_value.to_object(interpreter());
  58. }
  59. }();
  60. i32 computed_length = 0;
  61. auto length_property = get("length");
  62. if (interpreter().exception())
  63. return nullptr;
  64. if (length_property.is_number())
  65. computed_length = max(0, length_property.as_i32() - static_cast<i32>(arguments.size()));
  66. Object* constructor_prototype = nullptr;
  67. auto prototype_property = target_function.get("prototype");
  68. if (interpreter().exception())
  69. return nullptr;
  70. if (prototype_property.is_object())
  71. constructor_prototype = &prototype_property.as_object();
  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. }