FunctionObject.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Interpreter.h>
  8. #include <LibJS/Runtime/BoundFunction.h>
  9. #include <LibJS/Runtime/Completion.h>
  10. #include <LibJS/Runtime/FunctionObject.h>
  11. namespace JS {
  12. FunctionObject::FunctionObject(Object& prototype)
  13. : Object(prototype)
  14. {
  15. }
  16. FunctionObject::~FunctionObject()
  17. {
  18. }
  19. // 10.2.9 SetFunctionName ( F, name [ , prefix ] ), https://tc39.es/ecma262/#sec-setfunctionname
  20. void FunctionObject::set_function_name(Variant<PropertyKey, PrivateName> const& name_arg, Optional<StringView> const& prefix)
  21. {
  22. auto& vm = this->vm();
  23. // 1. Assert: F is an extensible object that does not have a "name" own property.
  24. VERIFY(m_is_extensible);
  25. VERIFY(!storage_has(vm.names.name));
  26. String name;
  27. // 2. If Type(name) is Symbol, then
  28. if (auto const* property_key = name_arg.get_pointer<PropertyKey>(); property_key && property_key->is_symbol()) {
  29. // a. Let description be name's [[Description]] value.
  30. auto const& description = property_key->as_symbol()->raw_description();
  31. // b. If description is undefined, set name to the empty String.
  32. if (!description.has_value())
  33. name = String::empty();
  34. // c. Else, set name to the string-concatenation of "[", description, and "]".
  35. else
  36. name = String::formatted("[{}]", *description);
  37. }
  38. // 3. Else if name is a Private Name, then
  39. else if (auto const* private_name = name_arg.get_pointer<PrivateName>()) {
  40. // a. Set name to name.[[Description]].
  41. name = private_name->description;
  42. }
  43. // NOTE: This is necessary as we use a different parameter name.
  44. else {
  45. name = name_arg.get<PropertyKey>().to_string();
  46. }
  47. // 4. If F has an [[InitialName]] internal slot, then
  48. if (is<NativeFunction>(this)) {
  49. // a. Set F.[[InitialName]] to name.
  50. // TODO: Remove FunctionObject::name(), implement NativeFunction::initial_name(), and then do this.
  51. }
  52. // 5. If prefix is present, then
  53. if (prefix.has_value()) {
  54. // a. Set name to the string-concatenation of prefix, the code unit 0x0020 (SPACE), and name.
  55. name = String::formatted("{} {}", *prefix, name);
  56. // b. If F has an [[InitialName]] internal slot, then
  57. if (is<NativeFunction>(this)) {
  58. // i. Optionally, set F.[[InitialName]] to name.
  59. // TODO: See above.
  60. }
  61. }
  62. // 6. Return ! DefinePropertyOrThrow(F, "name", PropertyDescriptor { [[Value]]: name, [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }).
  63. MUST(define_property_or_throw(vm.names.name, PropertyDescriptor { .value = js_string(vm, move(name)), .writable = false, .enumerable = false, .configurable = true }));
  64. }
  65. // 10.2.10 SetFunctionLength ( F, length ), https://tc39.es/ecma262/#sec-setfunctionlength
  66. void FunctionObject::set_function_length(double length)
  67. {
  68. auto& vm = this->vm();
  69. // "length (a non-negative integer or +∞)"
  70. VERIFY(trunc(length) == length || __builtin_isinf_sign(length) == 1);
  71. // 1. Assert: F is an extensible object that does not have a "length" own property.
  72. VERIFY(m_is_extensible);
  73. VERIFY(!storage_has(vm.names.length));
  74. // 2. Return ! DefinePropertyOrThrow(F, "length", PropertyDescriptor { [[Value]]: 𝔽(length), [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }).
  75. MUST(define_property_or_throw(vm.names.length, PropertyDescriptor { .value = Value { length }, .writable = false, .enumerable = false, .configurable = true }));
  76. }
  77. ThrowCompletionOr<BoundFunction*> FunctionObject::bind(Value bound_this_value, Vector<Value> arguments)
  78. {
  79. auto& vm = this->vm();
  80. FunctionObject& target_function = is<BoundFunction>(*this) ? static_cast<BoundFunction&>(*this).bound_target_function() : *this;
  81. auto get_bound_this_object = [&vm, bound_this_value, this]() -> ThrowCompletionOr<Value> {
  82. if (is<BoundFunction>(*this) && !static_cast<BoundFunction&>(*this).bound_this().is_empty())
  83. return static_cast<BoundFunction&>(*this).bound_this();
  84. switch (bound_this_value.type()) {
  85. case Value::Type::Undefined:
  86. case Value::Type::Null:
  87. if (vm.in_strict_mode())
  88. return bound_this_value;
  89. return &global_object();
  90. default:
  91. return TRY(bound_this_value.to_object(global_object()));
  92. }
  93. };
  94. auto bound_this_object = TRY(get_bound_this_object());
  95. i32 computed_length = 0;
  96. auto length_property = TRY(get(vm.names.length));
  97. if (length_property.is_number())
  98. computed_length = max(0, length_property.as_i32() - static_cast<i32>(arguments.size()));
  99. Object* constructor_prototype = nullptr;
  100. auto prototype_property = TRY(target_function.get(vm.names.prototype));
  101. if (prototype_property.is_object())
  102. constructor_prototype = &prototype_property.as_object();
  103. Vector<Value> all_bound_arguments;
  104. if (is<BoundFunction>(*this))
  105. all_bound_arguments.extend(static_cast<BoundFunction&>(*this).bound_arguments());
  106. all_bound_arguments.extend(move(arguments));
  107. return heap().allocate<BoundFunction>(global_object(), global_object(), target_function, bound_this_object, move(all_bound_arguments), computed_length, constructor_prototype);
  108. }
  109. }