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..e106600d623d3c12bcb4bf97a2a331ba2719d29d 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)); |
| } |
| @@ -1181,16 +1181,36 @@ class MathNatives { |
| } |
| builtin$hashCode$0(receiver) { |
| + if (receiver is String) return stringHashCodeUnchecked(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()); |
| } |
| +/** |
| + * This is the [Jenkins hash function][1], but always using XOR |
| + * instead of addition to keep the hash value in 32bit. This was |
| + * inspired by jmesserly's work in Frog. However, using XOR is 40% |
| + * faster than the original version in Frog. |
| + * |
| + * Caveat: As long as String.hashCode isn't optimized, Frog will |
| + * out-perform Leg because of the overhead of calling |
| + * [builtin$hashCode$0]. |
| + * |
| + * [1]: http://en.wikipedia.org/wiki/Jenkins_hash_function |
| + */ |
| +stringHashCodeUnchecked(string) { |
| + var hash = 0; |
|
kasperl
2012/02/28 09:37:08
int hash?
ahe
2012/02/28 10:09:35
Done.
|
| + var length = JS('int', @'$0.length', string); |
| + for (var i = 0; i < length; i++) { |
|
kasperl
2012/02/28 09:37:08
int i?
ahe
2012/02/28 10:09:35
Done.
|
| + hash ^= JS('int', @'$0.charCodeAt($1)', string, i); |
| + hash ^= JS("int", @"$0 << $1", hash, 10); |
|
ngeoffray
2012/02/28 10:13:25
Cobnsistency -> use ' instead of "
ahe
2012/02/28 17:26:10
Done.
|
| + hash ^= hash >> 6; |
| + } |
| + hash ^= JS("int", @"$0 << $1", hash, 3); |
|
ngeoffray
2012/02/28 10:13:25
ditto
ahe
2012/02/28 17:26:10
Done.
|
| + hash ^= hash >> 11; |
| + return 0x1fffffff & (hash ^ JS("int", @"$0 << $1", hash, 15)); |
| +} |
| + |
| // TODO(ahe): Dynamic may be overridden. |
| builtin$get$dynamic(receiver) => receiver; |