File tree Expand file tree Collapse file tree 6 files changed +18
-10
lines changed
sdk/lib/_internal/wasm/lib Expand file tree Collapse file tree 6 files changed +18
-10
lines changed Original file line number Diff line number Diff line change @@ -732,14 +732,14 @@ class Intrinsifier {
732
732
// dart:_object_helper static functions.
733
733
if (node.target.enclosingLibrary.name == 'dart._object_helper' ) {
734
734
switch (name) {
735
- case "getHash " :
735
+ case "getIdentityHashField " :
736
736
Expression arg = node.arguments.positional[0 ];
737
737
w.ValueType objectType = translator.objectInfo.nonNullableType;
738
738
codeGen.wrap (arg, objectType);
739
739
b.struct_get (translator.objectInfo.struct, FieldIndex .identityHash);
740
740
b.i64_extend_i32_u ();
741
741
return w.NumType .i64;
742
- case "setHash " :
742
+ case "setIdentityHashField " :
743
743
Expression arg = node.arguments.positional[0 ];
744
744
Expression hash = node.arguments.positional[1 ];
745
745
w.ValueType objectType = translator.objectInfo.nonNullableType;
Original file line number Diff line number Diff line change @@ -606,10 +606,17 @@ final class JSStringImpl implements String {
606
606
}
607
607
}
608
608
609
- /// This must be kept in sync with `StringBase.hashCode` in string_patch.dart.
610
- /// TODO(joshualitt): Find some way to cache the hash code.
611
609
@override
612
610
int get hashCode {
611
+ int hash = getIdentityHashField (this );
612
+ if (hash != 0 ) return hash;
613
+ hash = _computeHashCode ();
614
+ setIdentityHashField (this , hash);
615
+ return hash;
616
+ }
617
+
618
+ /// This must be kept in sync with `StringBase.hashCode` in string_patch.dart.
619
+ int _computeHashCode () {
613
620
int hash = 0 ;
614
621
final length = this .length;
615
622
for (int i = 0 ; i < length; i++ ) {
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ library dart._js_types;
16
16
import 'dart:_error_utils' ;
17
17
import 'dart:_internal' ;
18
18
import 'dart:_js_helper' as js;
19
+ import 'dart:_object_helper' ;
19
20
import 'dart:_string_helper' ;
20
21
import 'dart:_wasm' ;
21
22
import 'dart:collection' ;
Original file line number Diff line number Diff line change 5
5
library dart._object_helper;
6
6
7
7
// Access hidden identity hash code field.
8
- external int getHash (Object obj);
9
- external void setHash (Object obj, int hash);
8
+ external int getIdentityHashField (Object obj);
9
+ external void setIdentityHashField (Object obj, int hash);
Original file line number Diff line number Diff line change @@ -13,14 +13,14 @@ class Object {
13
13
static final _hashCodeRnd = new Random ();
14
14
15
15
static int _objectHashCode (Object obj) {
16
- var result = getHash (obj);
16
+ var result = getIdentityHashField (obj);
17
17
if (result == 0 ) {
18
18
// We want the hash to be a Smi value greater than 0.
19
19
do {
20
20
result = _hashCodeRnd.nextInt (0x40000000 );
21
21
} while (result == 0 );
22
22
23
- setHash (obj, result);
23
+ setIdentityHashField (obj, result);
24
24
return result;
25
25
}
26
26
return result;
Original file line number Diff line number Diff line change @@ -96,10 +96,10 @@ abstract final class StringBase extends WasmStringBase {
96
96
static const int _maxUnsignedSmiBits = 63 ;
97
97
98
98
int get hashCode {
99
- int hash = getHash (this );
99
+ int hash = getIdentityHashField (this );
100
100
if (hash != 0 ) return hash;
101
101
hash = _computeHashCode ();
102
- setHash (this , hash);
102
+ setIdentityHashField (this , hash);
103
103
return hash;
104
104
}
105
105
You can’t perform that action at this time.
0 commit comments