Chromium Code Reviews| Index: dart/frog/leg/lib/js_helper.dart |
| diff --git a/dart/frog/leg/lib/js_helper.dart b/dart/frog/leg/lib/js_helper.dart |
| index d730fa097f52c06284dd16c8f6629a26c3af305f..bdc43cc5f4a3b090884d1d88e9676b7751ea58b7 100644 |
| --- a/dart/frog/leg/lib/js_helper.dart |
| +++ b/dart/frog/leg/lib/js_helper.dart |
| @@ -359,7 +359,7 @@ builtin$charCodeAt$1(var receiver, int index) { |
| if (index is !num) throw new IllegalArgumentException(index); |
| if (index < 0) throw new IndexOutOfRangeException(index); |
| if (index >= receiver.length) throw new IndexOutOfRangeException(index); |
| - return JS("string", @"$0.charCodeAt($1)", receiver, index); |
| + return JS("int", @"$0.charCodeAt($1)", receiver, index); |
| } else { |
| return UNINTERCEPTED(receiver.charCodeAt(index)); |
| } |
| @@ -1180,15 +1180,26 @@ class MathNatives { |
| static double random() => JS("double", @"Math.random()"); |
| } |
| +/** |
| + * This is the [Jenkins hash function][1], but always using XOR |
|
kasperl
2012/02/28 13:58:42
Update comment to match reality.
ahe
2012/02/28 17:26:10
Done.
|
| + * instead of addition to keep the hash value in 32bit. This was |
| + * inspired by jmesserly's work in Frog. |
| + * |
| + * [1]: http://en.wikipedia.org/wiki/Jenkins_hash_function |
| + */ |
| builtin$hashCode$0(receiver) { |
| - if (receiver is num) return receiver & 0x1FFFFFFF; |
| - if (receiver is String) { |
| - throw 'String.hashCode is not implemented'; |
| - } |
| - if (isJsArray(receiver)) { |
| - throw 'List.hashCode is not implemented'; |
| - } |
| - return UNINTERCEPTED(receiver.hashCode()); |
| + if (receiver is num) return JS('int', @'$0 & 0x1FFFFFFF', receiver); |
|
Lasse Reichstein Nielsen
2012/02/28 14:12:16
That's a somewhat bad hash for doubles (specifical
ahe
2012/02/28 17:26:10
What do you suggest I use instead?
|
| + if (receiver is !String) return UNINTERCEPTED(receiver.hashCode()); |
|
Lasse Reichstein Nielsen
2012/02/28 14:12:16
What about arrays and booleans?
ahe
2012/02/28 17:26:10
I checked, neither List nor bool are hashable.
|
| + int hash = 0; |
| + int length = JS('int', @'$0.length', receiver); |
| + for (int i = 0; i < length; i++) { |
| + hash = 0x1fffffff & (hash + JS('int', @'$0.charCodeAt($1)', receiver, i)); |
| + hash = 0x1fffffff & (hash + JS("int", @"$0 << $1", 0x0007ffff & hash, 10)); |
| + hash ^= hash >> 6; |
| + } |
| + hash = 0x1fffffff & (hash + JS("int", @"$0 << $1", 0x03ffffff & hash, 3)); |
| + hash ^= hash >> 11; |
| + return 0x1fffffff & (hash + JS("int", @"$0 << $1", 0x00003fff & hash, 15)); |
| } |
| // TODO(ahe): Dynamic may be overridden. |