FunctionObject.cpp 3.7 KB

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