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 int _getHash(obj) native "Object_getHash"; | |
6 int _setHash(obj, hash) native "Object_setHash"; | |
7 | |
8 @patch | 5 @patch |
9 class Object { | 6 class Object { |
10 // The VM has its own implementation of equals. | 7 // The VM has its own implementation of equals. |
11 @patch | 8 @patch |
12 bool operator ==(other) native "Object_equals"; | 9 bool operator ==(other) native "Object_equals"; |
13 | 10 |
14 // Helpers used to implement hashCode. If a hashCode is used, we remember it | 11 // Helpers used to implement hashCode. If a hashCode is used, we remember it |
15 // in a weak table in the VM (32 bit) or in the header of the object (64 | 12 // in a weak table in the VM. A new hashCode value is calculated using a |
16 // bit). A new hashCode value is calculated using a random number generator. | 13 // number generator. |
17 static final _hashCodeRnd = new Random(); | 14 static final _hashCodeRnd = new Random(); |
18 | 15 |
19 // Shared static implementation for hashCode and _identityHashCode. | 16 // Shared static implementation for hashCode and _identityHashCode. |
| 17 static _getHash(obj) native "Object_getHash"; |
| 18 static _setHash(obj, hash) native "Object_setHash"; |
| 19 |
20 static int _objectHashCode(obj) { | 20 static int _objectHashCode(obj) { |
21 var result = _getHash(obj); | 21 var result = _getHash(obj); |
22 if (result == 0) { | 22 if (result == 0) { |
23 // We want the hash to be a Smi value greater than 0. | 23 // We want the hash to be a Smi value greater than 0. |
24 do { | 24 result = _hashCodeRnd.nextInt(0x40000000); |
| 25 while (result == 0) { |
25 result = _hashCodeRnd.nextInt(0x40000000); | 26 result = _hashCodeRnd.nextInt(0x40000000); |
26 } while (result == 0); | 27 } |
27 _setHash(obj, result); | 28 _setHash(obj, result); |
28 } | 29 } |
29 return result; | 30 return result; |
30 } | 31 } |
31 | 32 |
32 @patch | 33 @patch |
33 int get hashCode => _objectHashCode(this); | 34 int get hashCode => _objectHashCode(this); |
34 int get _identityHashCode => _objectHashCode(this); | 35 int get _identityHashCode => _objectHashCode(this); |
35 | 36 |
36 @patch | 37 @patch |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 native "Object_as"; | 73 native "Object_as"; |
73 | 74 |
74 static _symbolMapToStringMap(Map<Symbol, dynamic> map) { | 75 static _symbolMapToStringMap(Map<Symbol, dynamic> map) { |
75 var result = new Map<String, dynamic>(); | 76 var result = new Map<String, dynamic>(); |
76 map.forEach((Symbol key, value) { | 77 map.forEach((Symbol key, value) { |
77 result[internal.Symbol.getName(key)] = value; | 78 result[internal.Symbol.getName(key)] = value; |
78 }); | 79 }); |
79 return result; | 80 return result; |
80 } | 81 } |
81 } | 82 } |
OLD | NEW |