Symbol.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Heap/Heap.h>
  8. #include <LibJS/Runtime/Symbol.h>
  9. #include <LibJS/Runtime/VM.h>
  10. namespace JS {
  11. Symbol::Symbol(Optional<String> description, bool is_global)
  12. : m_description(move(description))
  13. , m_is_global(is_global)
  14. {
  15. }
  16. NonnullGCPtr<Symbol> Symbol::create(VM& vm, Optional<String> description, bool is_global)
  17. {
  18. return vm.heap().allocate_without_realm<Symbol>(move(description), is_global);
  19. }
  20. // 20.4.3.3.1 SymbolDescriptiveString ( sym ), https://tc39.es/ecma262/#sec-symboldescriptivestring
  21. ErrorOr<String> Symbol::descriptive_string() const
  22. {
  23. // 1. Let desc be sym's [[Description]] value.
  24. // 2. If desc is undefined, set desc to the empty String.
  25. // 3. Assert: desc is a String.
  26. auto description = m_description.value_or(String {});
  27. // 4. Return the string-concatenation of "Symbol(", desc, and ")".
  28. return String::formatted("Symbol({})", description);
  29. }
  30. }