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