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 // using an Expando object. A new hashCode value is calculated using a Random | 8 // in a weak table in the VM. A new hashCode value is calculated using a |
9 // number generator. | 9 // number generator. |
10 static Expando _hashCodeExp = new Expando("Object.hashCode"); | 10 static final _hashCodeRnd = new Random(); |
11 static Random _hashCodeRnd = new Random(); | 11 |
| 12 static _getHash(obj) native "Object_getHash"; |
| 13 static _setHash(obj, hash) native "Object_setHash"; |
12 | 14 |
13 /* patch */ int get hashCode { | 15 /* patch */ int get hashCode { |
14 if (this == null) { | 16 if (this == null) { |
15 return 2011; // The year Dart was announced and a prime. | 17 return 2011; // The year Dart was announced and a prime. |
16 } | 18 } |
17 var result = _hashCodeExp[this]; | 19 var result = _getHash(this); |
18 if (result == null) { | 20 if (result == 0) { |
19 result = _hashCodeRnd.nextInt(0x40000000); // Stay in Smi range. | 21 // We want the hash to be a Smi value greater than 0. |
20 _hashCodeExp[this] = result; | 22 result = _hashCodeRnd.nextInt(0x40000000); |
| 23 while (result == 0) { |
| 24 result = _hashCodeRnd.nextInt(0x40000000); |
| 25 } |
| 26 _setHash(this, result); |
21 } | 27 } |
22 return result; | 28 return result; |
23 } | 29 } |
24 | 30 |
25 /* patch */ String toString() native "Object_toString"; | 31 /* patch */ String toString() native "Object_toString"; |
26 // A statically dispatched version of Object.toString. | 32 // A statically dispatched version of Object.toString. |
27 static String _toString(obj) native "Object_toString"; | 33 static String _toString(obj) native "Object_toString"; |
28 | 34 |
29 _noSuchMethod(bool isMethod, | 35 _noSuchMethod(bool isMethod, |
30 String memberName, | 36 String memberName, |
(...skipping 27 matching lines...) Expand all Loading... |
58 static _symbolMapToStringMap(Map<Symbol, dynamic> map) { | 64 static _symbolMapToStringMap(Map<Symbol, dynamic> map) { |
59 var result = new Map<String, dynamic>(); | 65 var result = new Map<String, dynamic>(); |
60 map.forEach((Symbol key, value) { | 66 map.forEach((Symbol key, value) { |
61 result[_collection_dev.Symbol.getName(key)] = value; | 67 result[_collection_dev.Symbol.getName(key)] = value; |
62 }); | 68 }); |
63 return result; | 69 return result; |
64 } | 70 } |
65 | 71 |
66 int get _cid native "Object_cid"; | 72 int get _cid native "Object_cid"; |
67 } | 73 } |
OLD | NEW |