1/*
2 * Copyright (C) 2003-2019 Apple Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21#include "config.h"
22#include "Identifier.h"
23
24#include "CallFrame.h"
25#include "JSObject.h"
26#include "JSScope.h"
27#include "NumericStrings.h"
28#include "JSCInlines.h"
29#include <new>
30#include <string.h>
31#include <wtf/Assertions.h>
32#include <wtf/FastMalloc.h>
33#include <wtf/text/ASCIIFastPath.h>
34#include <wtf/text/StringHash.h>
35
36namespace JSC {
37
38Ref<StringImpl> Identifier::add(VM& vm, const char* c)
39{
40 ASSERT(c);
41 ASSERT(c[0]);
42 if (!c[1])
43 return vm.smallStrings.singleCharacterStringRep(c[0]);
44
45 return *AtomStringImpl::add(c);
46}
47
48Ref<StringImpl> Identifier::add8(VM& vm, const UChar* s, int length)
49{
50 if (length == 1) {
51 UChar c = s[0];
52 ASSERT(isLatin1(c));
53 if (canUseSingleCharacterString(c))
54 return vm.smallStrings.singleCharacterStringRep(c);
55 }
56 if (!length)
57 return *StringImpl::empty();
58
59 return *AtomStringImpl::add(s, length);
60}
61
62Identifier Identifier::from(VM& vm, unsigned value)
63{
64 return Identifier(vm, vm.numericStrings.add(value));
65}
66
67Identifier Identifier::from(VM& vm, int value)
68{
69 return Identifier(vm, vm.numericStrings.add(value));
70}
71
72Identifier Identifier::from(VM& vm, double value)
73{
74 return Identifier(vm, vm.numericStrings.add(value));
75}
76
77void Identifier::dump(PrintStream& out) const
78{
79 if (impl()) {
80 if (impl()->isSymbol()) {
81 auto* symbol = static_cast<SymbolImpl*>(impl());
82 if (symbol->isPrivate())
83 out.print("PrivateSymbol.");
84 }
85 out.print(impl());
86 } else
87 out.print("<null identifier>");
88}
89
90#ifndef NDEBUG
91
92void Identifier::checkCurrentAtomStringTable(VM& vm)
93{
94 // Check the identifier table accessible through the threadspecific matches the
95 // vm's identifier table.
96 ASSERT_UNUSED(vm, vm.atomStringTable() == Thread::current().atomStringTable());
97}
98
99#else
100
101// These only exists so that our exports are the same for debug and release builds.
102// This would be an RELEASE_ASSERT_NOT_REACHED(), but we're in NDEBUG only code here!
103NO_RETURN_DUE_TO_CRASH void Identifier::checkCurrentAtomStringTable(VM&) { CRASH(); }
104
105#endif
106
107} // namespace JSC
108