OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 patch class Object { | 5 patch class Object { |
6 | 6 |
7 // Helpers used to implement hashCode. If a hashCode is used, we remember it | 7 // Helpers used to implement hashCode. If a hashCode is used, we remember it |
8 // in a weak table in the VM. A new hashCode value is calculated using a | 8 // in a weak table in the VM. A new hashCode value is calculated using a |
9 // number generator. | 9 // number generator. |
10 static final _hashCodeRnd = new Random(); | 10 static final _hashCodeRnd = new Random(); |
11 | 11 |
12 static _getHash(obj) native "Object_getHash"; | 12 static _getHash(obj) native "Object_getHash"; |
13 static _setHash(obj, hash) native "Object_setHash"; | 13 static _setHash(obj, hash) native "Object_setHash"; |
14 | 14 |
15 /* patch */ int get hashCode { | 15 /* patch */ int get hashCode => _hashCodeOf(this); |
16 var result = _getHash(this); | 16 |
17 /* patch */ static int hashCodeOf(Object object) { | |
18 // Num doesn't use identity hash for hashCode, and this method must be | |
Ivan Posva
2013/09/24 06:22:11
I have read this comment several times and it stil
| |
19 // consistent with `identical`. | |
20 // Bool and Null overrides hashCode only in a patch. | |
21 if (object is num || object is bool || object == null) { | |
22 return object.hashCode; | |
23 } | |
24 return _hashCodeOf(object); | |
25 } | |
26 | |
27 static int _hashCodeOf(Object object) { | |
Ivan Posva
2013/09/24 06:22:11
Please leave this as a private dynamic method on O
| |
28 var result = _getHash(object); | |
17 if (result == 0) { | 29 if (result == 0) { |
18 // We want the hash to be a Smi value greater than 0. | 30 // We want the hash to be a Smi value greater than 0. |
19 result = _hashCodeRnd.nextInt(0x40000000); | 31 result = _hashCodeRnd.nextInt(0x40000000); |
20 while (result == 0) { | 32 while (result == 0) { |
21 result = _hashCodeRnd.nextInt(0x40000000); | 33 result = _hashCodeRnd.nextInt(0x40000000); |
22 } | 34 } |
23 _setHash(this, result); | 35 _setHash(object, result); |
24 } | 36 } |
25 return result; | 37 return result; |
26 } | 38 } |
27 | 39 |
28 /* patch */ String toString() native "Object_toString"; | 40 /* patch */ String toString() native "Object_toString"; |
29 // A statically dispatched version of Object.toString. | 41 // A statically dispatched version of Object.toString. |
30 static String _toString(obj) native "Object_toString"; | 42 static String _toString(obj) native "Object_toString"; |
31 | 43 |
32 _noSuchMethod(bool isMethod, | 44 _noSuchMethod(bool isMethod, |
33 String memberName, | 45 String memberName, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 }); | 77 }); |
66 return result; | 78 return result; |
67 } | 79 } |
68 | 80 |
69 int get _cid native "Object_cid"; | 81 int get _cid native "Object_cid"; |
70 | 82 |
71 _leftShiftWithMask32(count, mask) { | 83 _leftShiftWithMask32(count, mask) { |
72 return (this << count) & mask; | 84 return (this << count) & mask; |
73 } | 85 } |
74 } | 86 } |
OLD | NEW |