FunctionObject.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Interpreter.h>
  7. #include <LibJS/Runtime/BoundFunction.h>
  8. #include <LibJS/Runtime/FunctionObject.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. namespace JS {
  11. FunctionObject::FunctionObject(Object& prototype)
  12. : FunctionObject({}, {}, prototype)
  13. {
  14. }
  15. FunctionObject::FunctionObject(Value bound_this, Vector<Value> bound_arguments, Object& prototype)
  16. : Object(prototype)
  17. , m_bound_this(bound_this)
  18. , m_bound_arguments(move(bound_arguments))
  19. {
  20. }
  21. FunctionObject::~FunctionObject()
  22. {
  23. }
  24. BoundFunction* FunctionObject::bind(Value bound_this_value, Vector<Value> arguments)
  25. {
  26. auto& vm = this->vm();
  27. FunctionObject& target_function = is<BoundFunction>(*this) ? static_cast<BoundFunction&>(*this).target_function() : *this;
  28. auto bound_this_object = [&vm, bound_this_value, this]() -> Value {
  29. if (!m_bound_this.is_empty())
  30. return m_bound_this;
  31. switch (bound_this_value.type()) {
  32. case Value::Type::Undefined:
  33. case Value::Type::Null:
  34. if (vm.in_strict_mode())
  35. return bound_this_value;
  36. return &global_object();
  37. default:
  38. return bound_this_value.to_object(global_object());
  39. }
  40. }();
  41. i32 computed_length = 0;
  42. auto length_property = get(vm.names.length);
  43. if (vm.exception())
  44. return nullptr;
  45. if (length_property.is_number())
  46. computed_length = max(0, length_property.as_i32() - static_cast<i32>(arguments.size()));
  47. Object* constructor_prototype = nullptr;
  48. auto prototype_property = target_function.get(vm.names.prototype);
  49. if (vm.exception())
  50. return nullptr;
  51. if (prototype_property.is_object())
  52. constructor_prototype = &prototype_property.as_object();
  53. auto all_bound_arguments = bound_arguments();
  54. all_bound_arguments.extend(move(arguments));
  55. return heap().allocate<BoundFunction>(global_object(), global_object(), target_function, bound_this_object, move(all_bound_arguments), computed_length, constructor_prototype);
  56. }
  57. void FunctionObject::add_field(StringOrSymbol property_key, FunctionObject* initializer)
  58. {
  59. m_fields.empend(property_key, initializer);
  60. }
  61. // 7.3.31 DefineField ( receiver, fieldRecord ), https://tc39.es/ecma262/#sec-definefield
  62. void FunctionObject::InstanceField::define_field(VM& vm, Object& receiver) const
  63. {
  64. Value init_value = js_undefined();
  65. if (initializer) {
  66. init_value = vm.call(*initializer, receiver.value_of());
  67. if (vm.exception())
  68. return;
  69. }
  70. receiver.create_data_property_or_throw(name, init_value);
  71. }
  72. void FunctionObject::visit_edges(Visitor& visitor)
  73. {
  74. Base::visit_edges(visitor);
  75. visitor.visit(m_home_object);
  76. visitor.visit(m_bound_this);
  77. for (auto argument : m_bound_arguments)
  78. visitor.visit(argument);
  79. for (auto& field : m_fields) {
  80. field.name.visit_edges(visitor);
  81. visitor.visit(field.initializer);
  82. }
  83. }
  84. }