/ runtime / Identifier.cpp
Identifier.cpp
 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 "IdentifierInlines.h"
25  #include "NumericStrings.h"
26  #include <wtf/Assertions.h>
27  
28  namespace JSC {
29  
30  Ref<StringImpl> Identifier::add(VM& vm, const char* c)
31  {
32      ASSERT(c);
33      ASSERT(c[0]);
34      if (!c[1])
35          return vm.smallStrings.singleCharacterStringRep(c[0]);
36  
37      return *AtomStringImpl::add(c);
38  }
39  
40  Ref<StringImpl> Identifier::add8(VM& vm, const UChar* s, int length)
41  {
42      if (length == 1) {
43          UChar c = s[0];
44          ASSERT(isLatin1(c));
45          if (canUseSingleCharacterString(c))
46              return vm.smallStrings.singleCharacterStringRep(c);
47      }
48      if (!length)
49          return *StringImpl::empty();
50  
51      return *AtomStringImpl::add(s, length);
52  }
53  
54  Identifier Identifier::from(VM& vm, unsigned value)
55  {
56      return Identifier(vm, vm.numericStrings.add(value));
57  }
58  
59  Identifier Identifier::from(VM& vm, int value)
60  {
61      return Identifier(vm, vm.numericStrings.add(value));
62  }
63  
64  Identifier Identifier::from(VM& vm, double value)
65  {
66      return Identifier(vm, vm.numericStrings.add(value));
67  }
68  
69  void Identifier::dump(PrintStream& out) const
70  {
71      if (impl()) {
72          if (impl()->isSymbol()) {
73              auto* symbol = static_cast<SymbolImpl*>(impl());
74              if (symbol->isPrivate())
75                  out.print("PrivateSymbol.");
76          }
77          out.print(impl());
78      } else
79          out.print("<null identifier>");
80  }
81  
82  #ifndef NDEBUG
83  
84  void Identifier::checkCurrentAtomStringTable(VM& vm)
85  {
86      // Check the identifier table accessible through the threadspecific matches the
87      // vm's identifier table.
88      ASSERT_UNUSED(vm, vm.atomStringTable() == Thread::current().atomStringTable());
89  }
90  
91  #else
92  
93  // These only exists so that our exports are the same for debug and release builds.
94  // This would be an RELEASE_ASSERT_NOT_REACHED(), but we're in NDEBUG only code here!
95  NO_RETURN_DUE_TO_CRASH void Identifier::checkCurrentAtomStringTable(VM&) { CRASH(); }
96  
97  #endif
98  
99  } // namespace JSC