FunctionObject.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <AK/TypeCasts.h>
  8. #include <LibJS/Runtime/Completion.h>
  9. #include <LibJS/Runtime/FunctionObject.h>
  10. #include <LibJS/Runtime/NativeFunction.h>
  11. #include <LibJS/Runtime/VM.h>
  12. namespace JS {
  13. FunctionObject::FunctionObject(Realm& realm, Object* prototype)
  14. : Object(realm, prototype)
  15. {
  16. }
  17. FunctionObject::FunctionObject(Object& prototype)
  18. : Object(ConstructWithPrototypeTag::Tag, prototype)
  19. {
  20. }
  21. // 10.2.9 SetFunctionName ( F, name [ , prefix ] ), https://tc39.es/ecma262/#sec-setfunctionname
  22. void FunctionObject::set_function_name(Variant<PropertyKey, PrivateName> const& name_arg, Optional<StringView> const& prefix)
  23. {
  24. auto& vm = this->vm();
  25. // 1. Assert: F is an extensible object that does not have a "name" own property.
  26. VERIFY(m_is_extensible);
  27. VERIFY(!storage_has(vm.names.name));
  28. DeprecatedString name;
  29. // 2. If Type(name) is Symbol, then
  30. if (auto const* property_key = name_arg.get_pointer<PropertyKey>(); property_key && property_key->is_symbol()) {
  31. // a. Let description be name's [[Description]] value.
  32. auto const& description = property_key->as_symbol()->description();
  33. // b. If description is undefined, set name to the empty String.
  34. if (!description.has_value())
  35. name = DeprecatedString::empty();
  36. // c. Else, set name to the string-concatenation of "[", description, and "]".
  37. else
  38. name = DeprecatedString::formatted("[{}]", *description);
  39. }
  40. // 3. Else if name is a Private Name, then
  41. else if (auto const* private_name = name_arg.get_pointer<PrivateName>()) {
  42. // a. Set name to name.[[Description]].
  43. name = private_name->description;
  44. }
  45. // NOTE: This is necessary as we use a different parameter name.
  46. else {
  47. name = name_arg.get<PropertyKey>().to_string();
  48. }
  49. // 4. If F has an [[InitialName]] internal slot, then
  50. if (is<NativeFunction>(this)) {
  51. // a. Set F.[[InitialName]] to name.
  52. static_cast<NativeFunction&>(*this).set_initial_name({}, name);
  53. }
  54. // 5. If prefix is present, then
  55. if (prefix.has_value()) {
  56. // a. Set name to the string-concatenation of prefix, the code unit 0x0020 (SPACE), and name.
  57. name = DeprecatedString::formatted("{} {}", *prefix, name);
  58. // b. If F has an [[InitialName]] internal slot, then
  59. if (is<NativeFunction>(this)) {
  60. // i. Optionally, set F.[[InitialName]] to name.
  61. static_cast<NativeFunction&>(*this).set_initial_name({}, name);
  62. }
  63. }
  64. // 6. Perform ! DefinePropertyOrThrow(F, "name", PropertyDescriptor { [[Value]]: name, [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }).
  65. MUST(define_property_or_throw(vm.names.name, PropertyDescriptor { .value = PrimitiveString::create(vm, move(name)), .writable = false, .enumerable = false, .configurable = true }));
  66. // 7. Return unused.
  67. }
  68. // 10.2.10 SetFunctionLength ( F, length ), https://tc39.es/ecma262/#sec-setfunctionlength
  69. void FunctionObject::set_function_length(double length)
  70. {
  71. auto& vm = this->vm();
  72. // "length (a non-negative integer or +∞)"
  73. VERIFY(trunc(length) == length || __builtin_isinf_sign(length) == 1);
  74. // 1. Assert: F is an extensible object that does not have a "length" own property.
  75. VERIFY(m_is_extensible);
  76. VERIFY(!storage_has(vm.names.length));
  77. // 2. Perform ! DefinePropertyOrThrow(F, "length", PropertyDescriptor { [[Value]]: 𝔽(length), [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }).
  78. MUST(define_property_or_throw(vm.names.length, PropertyDescriptor { .value = Value { length }, .writable = false, .enumerable = false, .configurable = true }));
  79. // 3. Return unused.
  80. }
  81. }