]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/Identifier.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / Identifier.cpp
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/HashSet.h>
34 #include <wtf/text/ASCIIFastPath.h>
35 #include <wtf/text/StringHash.h>
36
37 using WTF::ThreadSpecific;
38
39 namespace JSC {
40
41 Ref<StringImpl> Identifier::add(VM* vm, const char* c)
42 {
43 ASSERT(c);
44 ASSERT(c[0]);
45 if (!c[1])
46 return *vm->smallStrings.singleCharacterStringRep(c[0]);
47
48 return *AtomicStringImpl::add(c);
49 }
50
51 Ref<StringImpl> Identifier::add(ExecState* exec, const char* c)
52 {
53 return add(&exec->vm(), c);
54 }
55
56 Ref<StringImpl> Identifier::add8(VM* vm, const UChar* s, int length)
57 {
58 if (length == 1) {
59 UChar c = s[0];
60 ASSERT(c <= 0xff);
61 if (canUseSingleCharacterString(c))
62 return *vm->smallStrings.singleCharacterStringRep(c);
63 }
64 if (!length)
65 return *StringImpl::empty();
66
67 return *AtomicStringImpl::add(s, length);
68 }
69
70 Identifier Identifier::from(ExecState* exec, unsigned value)
71 {
72 return Identifier(exec, exec->vm().numericStrings.add(value));
73 }
74
75 Identifier Identifier::from(ExecState* exec, int value)
76 {
77 return Identifier(exec, exec->vm().numericStrings.add(value));
78 }
79
80 Identifier Identifier::from(ExecState* exec, double value)
81 {
82 return Identifier(exec, exec->vm().numericStrings.add(value));
83 }
84
85 Identifier Identifier::from(VM* vm, unsigned value)
86 {
87 return Identifier(vm, vm->numericStrings.add(value));
88 }
89
90 Identifier Identifier::from(VM* vm, int value)
91 {
92 return Identifier(vm, vm->numericStrings.add(value));
93 }
94
95 Identifier Identifier::from(VM* vm, double value)
96 {
97 return Identifier(vm, vm->numericStrings.add(value));
98 }
99
100 void Identifier::dump(PrintStream& out) const
101 {
102 if (impl())
103 out.print(impl());
104 else
105 out.print("<null identifier>");
106 }
107
108 #ifndef NDEBUG
109
110 void Identifier::checkCurrentAtomicStringTable(VM* vm)
111 {
112 // Check the identifier table accessible through the threadspecific matches the
113 // vm's identifier table.
114 ASSERT_UNUSED(vm, vm->atomicStringTable() == wtfThreadData().atomicStringTable());
115 }
116
117 void Identifier::checkCurrentAtomicStringTable(ExecState* exec)
118 {
119 checkCurrentAtomicStringTable(&exec->vm());
120 }
121
122 #else
123
124 // These only exists so that our exports are the same for debug and release builds.
125 // This would be an RELEASE_ASSERT_NOT_REACHED(), but we're in NDEBUG only code here!
126 NO_RETURN_DUE_TO_CRASH void Identifier::checkCurrentAtomicStringTable(VM*) { CRASH(); }
127 NO_RETURN_DUE_TO_CRASH void Identifier::checkCurrentAtomicStringTable(ExecState*) { CRASH(); }
128
129 #endif
130
131 } // namespace JSC