mirror of
https://github.com/dart-lang/sdk
synced 2024-09-18 22:01:19 +00:00
1a6d1eca32
Allow members of patch classes to be annotated with @patch. The VM ignores the annotation. I simply replaces the original method if the name matches, or reports an error if field names match. Adding a bit is_patched to members to do more checking remains a TODO. There are currently no unused bits available, and I don’t want to increase the size of Function objects for this. BUG= R=asiva@google.com Review URL: https://codereview.chromium.org/2230383003 .
60 lines
1.7 KiB
Dart
60 lines
1.7 KiB
Dart
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
@patch class Symbol {
|
|
@patch const Symbol(String name)
|
|
: this._name = name;
|
|
|
|
@patch toString() => 'Symbol("${getUnmangledName(this)}")';
|
|
|
|
static getUnmangledName(Symbol symbol) {
|
|
String string = Symbol.getName(symbol);
|
|
|
|
// Remove closurization hash mark
|
|
// #foo -> foo
|
|
if (string.startsWith('#')) {
|
|
string = string.substring(1);
|
|
}
|
|
// get:foo -> foo
|
|
// set:foo -> foo=
|
|
// get:_foo@xxx -> _foo
|
|
// set:_foo@xxx -> _foo=
|
|
// Class._constructor@xxx -> Class._constructor
|
|
// _Class@xxx._constructor@xxx -> _Class._constructor
|
|
// lib._S@xxx with lib._M1@xxx, lib._M2@xxx -> lib._S with lib._M1, lib._M2
|
|
StringBuffer result = new StringBuffer();
|
|
bool add_setter_suffix = false;
|
|
var pos = 0;
|
|
if (string.length >= 4 && string[3] == ':') {
|
|
// Drop 'get:' or 'set:' prefix.
|
|
pos = 4;
|
|
if (string[0] == 's') {
|
|
add_setter_suffix = true;
|
|
}
|
|
}
|
|
// Skip everything between AT and PERIOD, SPACE, COMMA or END
|
|
bool skip = false;
|
|
for (; pos < string.length; pos++) {
|
|
var char = string[pos];
|
|
if (char == '@') {
|
|
skip = true;
|
|
} else if (char == '.' || char == ' ' || char == ',') {
|
|
skip = false;
|
|
}
|
|
if (!skip) {
|
|
result.write(char);
|
|
}
|
|
}
|
|
if (add_setter_suffix) {
|
|
result.write('=');
|
|
}
|
|
return result.toString();
|
|
}
|
|
|
|
@patch int get hashCode {
|
|
const arbitraryPrime = 664597;
|
|
return 0x1fffffff & (arbitraryPrime * _name.hashCode);
|
|
}
|
|
}
|