OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart._internal; | 5 part of dart._internal; |
6 | 6 |
7 /** | 7 /** |
8 * Implementation of [core.Symbol]. This class uses the same name as | 8 * Implementation of [core.Symbol]. This class uses the same name as |
9 * a core class so a user can't tell the difference. | 9 * a core class so a user can't tell the difference. |
10 * | 10 * |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 * otherwise invalid names. | 105 * otherwise invalid names. |
106 */ | 106 */ |
107 const Symbol.unvalidated(this._name); | 107 const Symbol.unvalidated(this._name); |
108 | 108 |
109 // This is called by dart2js. | 109 // This is called by dart2js. |
110 Symbol.validated(String name) | 110 Symbol.validated(String name) |
111 : this._name = validatePublicSymbol(name); | 111 : this._name = validatePublicSymbol(name); |
112 | 112 |
113 bool operator ==(other) => other is Symbol && _name == other._name; | 113 bool operator ==(other) => other is Symbol && _name == other._name; |
114 | 114 |
115 int get hashCode { | 115 external int get hashCode; |
116 const arbitraryPrime = 664597; | |
117 return 0x1fffffff & (arbitraryPrime * _name.hashCode); | |
118 } | |
119 | 116 |
120 toString() => 'Symbol("$_name")'; | 117 toString() => 'Symbol("$_name")'; |
121 | 118 |
122 /// Platform-private accessor which cannot be called from user libraries. | 119 /// Platform-private accessor which cannot be called from user libraries. |
123 static String getName(Symbol symbol) => symbol._name; | 120 static String getName(Symbol symbol) => symbol._name; |
124 | 121 |
125 static String validatePublicSymbol(String name) { | 122 static String validatePublicSymbol(String name) { |
126 if (name.isEmpty || publicSymbolPattern.hasMatch(name)) return name; | 123 if (name.isEmpty || publicSymbolPattern.hasMatch(name)) return name; |
127 if (name.startsWith('_')) { | 124 if (name.startsWith('_')) { |
128 // There may be other private parts in a qualified name than the first | 125 // There may be other private parts in a qualified name than the first |
129 // one, but this is a common case that deserves a specific error | 126 // one, but this is a common case that deserves a specific error |
130 // message. | 127 // message. |
131 throw new ArgumentError('"$name" is a private identifier'); | 128 throw new ArgumentError('"$name" is a private identifier'); |
132 } | 129 } |
133 throw new ArgumentError( | 130 throw new ArgumentError( |
134 '"$name" is not a valid (qualified) symbol name'); | 131 '"$name" is not a valid (qualified) symbol name'); |
135 } | 132 } |
136 | 133 |
137 /** | 134 /** |
138 * Checks whether name is a valid symbol name. | 135 * Checks whether name is a valid symbol name. |
139 * | 136 * |
140 * This test allows both private and non-private symbols. | 137 * This test allows both private and non-private symbols. |
141 */ | 138 */ |
142 static bool isValidSymbol(String name) { | 139 static bool isValidSymbol(String name) { |
143 return (name.isEmpty || symbolPattern.hasMatch(name)); | 140 return (name.isEmpty || symbolPattern.hasMatch(name)); |
144 } | 141 } |
145 } | 142 } |
OLD | NEW |