2020-04-30 03:25:21 -03:00
|
|
|
/*
|
2021-04-22 20:53:07 -03:00
|
|
|
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
|
2023-02-11 12:51:44 -03:00
|
|
|
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
|
2020-04-30 03:25:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-30 03:25:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
#include <LibGC/Heap.h>
|
2020-04-30 03:25:21 -03:00
|
|
|
#include <LibJS/Runtime/Symbol.h>
|
2020-09-27 15:18:30 -03:00
|
|
|
#include <LibJS/Runtime/VM.h>
|
2020-04-30 03:25:21 -03:00
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_DEFINE_ALLOCATOR(Symbol);
|
2023-12-23 11:15:27 -03:00
|
|
|
|
2025-08-02 20:27:29 -03:00
|
|
|
Symbol::Symbol(Optional<Utf16String> description, bool is_global)
|
2020-04-30 03:25:21 -03:00
|
|
|
: m_description(move(description))
|
|
|
|
|
, m_is_global(is_global)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-02 20:27:29 -03:00
|
|
|
GC::Ref<Symbol> Symbol::create(VM& vm, Optional<Utf16String> description, bool is_global)
|
2020-04-30 03:25:21 -03:00
|
|
|
{
|
2024-11-13 14:13:46 -03:00
|
|
|
return vm.heap().allocate<Symbol>(move(description), is_global);
|
2020-09-22 11:18:51 -03:00
|
|
|
}
|
|
|
|
|
|
2023-02-11 12:51:44 -03:00
|
|
|
// 20.4.3.3.1 SymbolDescriptiveString ( sym ), https://tc39.es/ecma262/#sec-symboldescriptivestring
|
2025-08-02 20:27:29 -03:00
|
|
|
Utf16String Symbol::descriptive_string() const
|
2023-02-11 12:51:44 -03:00
|
|
|
{
|
|
|
|
|
// 1. Let desc be sym's [[Description]] value.
|
|
|
|
|
// 2. If desc is undefined, set desc to the empty String.
|
|
|
|
|
// 3. Assert: desc is a String.
|
2025-08-02 20:27:29 -03:00
|
|
|
auto description = m_description.value_or({});
|
2023-02-11 12:51:44 -03:00
|
|
|
|
|
|
|
|
// 4. Return the string-concatenation of "Symbol(", desc, and ")".
|
2025-08-02 20:27:29 -03:00
|
|
|
return Utf16String::formatted("Symbol({})", description);
|
2023-02-11 12:51:44 -03:00
|
|
|
}
|
|
|
|
|
|
2023-04-12 18:12:54 -03:00
|
|
|
// 20.4.5.1 KeyForSymbol ( sym ), https://tc39.es/ecma262/#sec-keyforsymbol
|
2025-08-02 20:27:29 -03:00
|
|
|
Optional<Utf16String> Symbol::key() const
|
2023-04-12 18:12:54 -03:00
|
|
|
{
|
|
|
|
|
// 1. For each element e of the GlobalSymbolRegistry List, do
|
|
|
|
|
// a. If SameValue(e.[[Symbol]], sym) is true, return e.[[Key]].
|
|
|
|
|
if (m_is_global) {
|
|
|
|
|
// NOTE: Global symbols should always have a description string
|
|
|
|
|
VERIFY(m_description.has_value());
|
|
|
|
|
return m_description;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. Assert: GlobalSymbolRegistry does not currently contain an entry for sym.
|
|
|
|
|
// 3. Return undefined.
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-30 03:25:21 -03:00
|
|
|
}
|