Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: pkg/dev_compiler/tool/input_sdk/lib/internal/symbol.dart

Issue 2535273002: Better mirrors support for mixins and private fields (Closed)
Patch Set: Address comments Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 *
11 * The purpose of this class is to hide [_name] from user code, but 11 * The purpose of this class is to hide [_name] from user code, but
12 * make it accessible to Dart platform code via the static method 12 * make it accessible to Dart platform code via the static method
13 * [getName]. 13 * [getName].
14 */ 14 */
15 class Symbol implements core.Symbol { 15 class Symbol implements core.Symbol {
16 final String _name; 16 final String _name;
17 17
18 // Used internally by DDC to map ES6 symbols to Dart.
19 final dynamic _nativeSymbol;
20
18 /** 21 /**
19 * Source of RegExp matching Dart reserved words. 22 * Source of RegExp matching Dart reserved words.
20 * 23 *
21 * Reserved words are not allowed as identifiers. 24 * Reserved words are not allowed as identifiers.
22 */ 25 */
23 static const String reservedWordRE = 26 static const String reservedWordRE =
24 r'(?:assert|break|c(?:a(?:se|tch)|lass|on(?:st|tinue))|d(?:efault|o)|' 27 r'(?:assert|break|c(?:a(?:se|tch)|lass|on(?:st|tinue))|d(?:efault|o)|'
25 r'e(?:lse|num|xtends)|f(?:alse|inal(?:ly)?|or)|i[fns]|n(?:ew|ull)|' 28 r'e(?:lse|num|xtends)|f(?:alse|inal(?:ly)?|or)|i[fns]|n(?:ew|ull)|'
26 r'ret(?:hrow|urn)|s(?:uper|witch)|t(?:h(?:is|row)|r(?:ue|y))|' 29 r'ret(?:hrow|urn)|s(?:uper|witch)|t(?:h(?:is|row)|r(?:ue|y))|'
27 r'v(?:ar|oid)|w(?:hile|ith))'; 30 r'v(?:ar|oid)|w(?:hile|ith))';
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 // which matches any identifier. 91 // which matches any identifier.
89 92
90 /** 93 /**
91 * RegExp that validates a non-empty symbol. 94 * RegExp that validates a non-empty symbol.
92 * 95 *
93 * Private symbols are accepted. 96 * Private symbols are accepted.
94 * 97 *
95 * The empty symbol is handled before this regexp is used, and is not 98 * The empty symbol is handled before this regexp is used, and is not
96 * accepted. 99 * accepted.
97 */ 100 */
98 static final RegExp symbolPattern = new RegExp( 101 static final RegExp symbolPattern =
99 '^(?:$operatorRE\$|$identifierRE(?:=?\$|[.](?!\$)))+?\$'); 102 new RegExp('^(?:$operatorRE\$|$identifierRE(?:=?\$|[.](?!\$)))+?\$');
100 103
101 external const Symbol(String name); 104 external const Symbol(String name);
102 105
106 external const Symbol.es6(String name, nativeSymbol);
107
103 /** 108 /**
104 * Platform-private method used by the mirror system to create 109 * Platform-private method used by the mirror system to create
105 * otherwise invalid names. 110 * otherwise invalid names.
106 */ 111 */
107 const Symbol.unvalidated(this._name); 112 const Symbol.unvalidated(this._name) : this._nativeSymbol = null;
108 113
109 // This is called by dart2js. 114 // This is called by dart2js.
110 Symbol.validated(String name) 115 Symbol.validated(String name)
111 : this._name = validatePublicSymbol(name); 116 : this._name = validatePublicSymbol(name),
117 this._nativeSymbol = null;
112 118
113 bool operator ==(Object other) => other is Symbol && _name == other._name; 119 bool operator ==(Object other) =>
120 other is Symbol &&
121 _name == other._name &&
122 _nativeSymbol == other._nativeSymbol;
114 123
115 external int get hashCode; 124 external int get hashCode;
116 125
117 toString() => 'Symbol("$_name")'; 126 toString() => 'Symbol("$_name")';
118 127
119 /// Platform-private accessor which cannot be called from user libraries. 128 /// Platform-private accessor which cannot be called from user libraries.
120 static String getName(Symbol symbol) => symbol._name; 129 static String getName(Symbol symbol) => symbol._name;
121 130
131 static dynamic getNativeSymbol(Symbol symbol) => symbol._nativeSymbol;
132
122 static String validatePublicSymbol(String name) { 133 static String validatePublicSymbol(String name) {
123 if (name.isEmpty || publicSymbolPattern.hasMatch(name)) return name; 134 if (name.isEmpty || publicSymbolPattern.hasMatch(name)) return name;
124 if (name.startsWith('_')) { 135 if (name.startsWith('_')) {
125 // There may be other private parts in a qualified name than the first 136 // There may be other private parts in a qualified name than the first
126 // one, but this is a common case that deserves a specific error 137 // one, but this is a common case that deserves a specific error
127 // message. 138 // message.
128 throw new ArgumentError('"$name" is a private identifier'); 139 throw new ArgumentError('"$name" is a private identifier');
129 } 140 }
130 throw new ArgumentError( 141 throw new ArgumentError('"$name" is not a valid (qualified) symbol name');
131 '"$name" is not a valid (qualified) symbol name');
132 } 142 }
133 143
134 /** 144 /**
135 * Checks whether name is a valid symbol name. 145 * Checks whether name is a valid symbol name.
136 * 146 *
137 * This test allows both private and non-private symbols. 147 * This test allows both private and non-private symbols.
138 */ 148 */
139 static bool isValidSymbol(String name) { 149 static bool isValidSymbol(String name) {
140 return (name.isEmpty || symbolPattern.hasMatch(name)); 150 return (name.isEmpty || symbolPattern.hasMatch(name));
141 } 151 }
142 } 152 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698