1/*
2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2012 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::add(ExecState* exec, const char* c)
49{
50 return add(&exec->vm(), c);
51}
52
53Ref<StringImpl> Identifier::add8(VM* vm, const UChar* s, int length)
54{
55 if (length == 1) {
56 UChar c = s[0];
57 ASSERT(c <= 0xff);
58 if (canUseSingleCharacterString(c))
59 return vm->smallStrings.singleCharacterStringRep(c);
60 }
61 if (!length)
62 return *StringImpl::empty();
63
64 return *AtomStringImpl::add(s, length);
65}
66
67Identifier Identifier::from(ExecState* exec, unsigned value)
68{
69 return Identifier(exec, exec->vm().numericStrings.add(value));
70}
71
72Identifier Identifier::from(ExecState* exec, int value)
73{
74 return Identifier(exec, exec->vm().numericStrings.add(value));
75}
76
77Identifier Identifier::from(ExecState* exec, double value)
78{
79 return Identifier(exec, exec->vm().numericStrings.add(value));
80}
81
82Identifier Identifier::from(VM* vm, unsigned value)
83{
84 return Identifier(vm, vm->numericStrings.add(value));
85}
86
87Identifier Identifier::from(VM* vm, int value)
88{
89 return Identifier(vm, vm->numericStrings.add(value));
90}
91
92Identifier Identifier::from(VM* vm, double value)
93{
94 return Identifier(vm, vm->numericStrings.add(value));
95}
96
97void Identifier::dump(PrintStream& out) const
98{
99 if (impl()) {
100 if (impl()->isSymbol()) {
101 auto* symbol = static_cast<SymbolImpl*>(impl());
102 if (symbol->isPrivate())
103 out.print("PrivateSymbol.");
104 }
105 out.print(impl());
106 } else
107 out.print("<null identifier>");
108}
109
110#ifndef NDEBUG
111
112void Identifier::checkCurrentAtomStringTable(VM* vm)
113{
114 // Check the identifier table accessible through the threadspecific matches the
115 // vm's identifier table.
116 ASSERT_UNUSED(vm, vm->atomStringTable() == Thread::current().atomStringTable());
117}
118
119void Identifier::checkCurrentAtomStringTable(ExecState* exec)
120{
121 checkCurrentAtomStringTable(&exec->vm());
122}
123
124#else
125
126// These only exists so that our exports are the same for debug and release builds.
127// This would be an RELEASE_ASSERT_NOT_REACHED(), but we're in NDEBUG only code here!
128NO_RETURN_DUE_TO_CRASH void Identifier::checkCurrentAtomStringTable(VM*) { CRASH(); }
129NO_RETURN_DUE_TO_CRASH void Identifier::checkCurrentAtomStringTable(ExecState*) { CRASH(); }
130
131#endif
132
133} // namespace JSC
134