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. // 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. static_cast<NativeFunction&>(*this).set_initial_name({}, name);
  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. static_cast<NativeFunction&>(*this).set_initial_name({}, name);
  60. }
  61. }
  62. // 6. Perform ! 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. // 7. Return unused.
  65. }
  66. // 10.2.10 SetFunctionLength ( F, length ), https://tc39.es/ecma262/#sec-setfunctionlength
  67. void FunctionObject::set_function_length(double length)
  68. {
  69. auto& vm = this->vm();
  70. // "length (a non-negative integer or +∞)"
  71. VERIFY(trunc(length) == length || __builtin_isinf_sign(length) == 1);
  72. // 1. Assert: F is an extensible object that does not have a "length" own property.
  73. VERIFY(m_is_extensible);
  74. VERIFY(!storage_has(vm.names.length));
  75. // 2. Perform ! DefinePropertyOrThrow(F, "length", PropertyDescriptor { [[Value]]: 𝔽(length), [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }).
  76. MUST(define_property_or_throw(vm.names.length, PropertyDescriptor { .value = Value { length }, .writable = false, .enumerable = false, .configurable = true }));
  77. // 3. Return unused.
  78. }
  79. }