FunctionObject.cpp 3.7 KB

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