Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(845)

Side by Side Diff: dart/frog/leg/lib/js_helper.dart

Issue 9490003: Implement String.hashCode. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Improve performance by avoiding bailout. Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | dart/tests/co19/co19-leg.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #library('js_helper'); 5 #library('js_helper');
6 6
7 #import('coreimpl.dart'); 7 #import('coreimpl.dart');
8 8
9 #source('date_helper.dart'); 9 #source('date_helper.dart');
10 #source('regexp_helper.dart'); 10 #source('regexp_helper.dart');
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 return value; 352 return value;
353 } 353 }
354 } 354 }
355 355
356 builtin$charCodeAt$1(var receiver, int index) { 356 builtin$charCodeAt$1(var receiver, int index) {
357 checkNull(receiver); 357 checkNull(receiver);
358 if (receiver is String) { 358 if (receiver is String) {
359 if (index is !num) throw new IllegalArgumentException(index); 359 if (index is !num) throw new IllegalArgumentException(index);
360 if (index < 0) throw new IndexOutOfRangeException(index); 360 if (index < 0) throw new IndexOutOfRangeException(index);
361 if (index >= receiver.length) throw new IndexOutOfRangeException(index); 361 if (index >= receiver.length) throw new IndexOutOfRangeException(index);
362 return JS("string", @"$0.charCodeAt($1)", receiver, index); 362 return JS("int", @"$0.charCodeAt($1)", receiver, index);
363 } else { 363 } else {
364 return UNINTERCEPTED(receiver.charCodeAt(index)); 364 return UNINTERCEPTED(receiver.charCodeAt(index));
365 } 365 }
366 } 366 }
367 367
368 builtin$isEmpty$0(receiver) { 368 builtin$isEmpty$0(receiver) {
369 checkNull(receiver); 369 checkNull(receiver);
370 if (receiver is String || isJsArray(receiver)) { 370 if (receiver is String || isJsArray(receiver)) {
371 return JS("bool", @"$0.length === 0", receiver); 371 return JS("bool", @"$0.length === 0", receiver);
372 } 372 }
(...skipping 800 matching lines...) Expand 10 before | Expand all | Expand 10 after
1173 1173
1174 static num pow(num value, num exponent) { 1174 static num pow(num value, num exponent) {
1175 checkNum(value); 1175 checkNum(value);
1176 checkNum(exponent); 1176 checkNum(exponent);
1177 return JS("num", @"Math.pow($0, $1)", value, exponent); 1177 return JS("num", @"Math.pow($0, $1)", value, exponent);
1178 } 1178 }
1179 1179
1180 static double random() => JS("double", @"Math.random()"); 1180 static double random() => JS("double", @"Math.random()");
1181 } 1181 }
1182 1182
1183 /**
1184 * This is the [Jenkins hash function][1], but always using XOR
1185 * instead of addition to keep the hash value in 32bit. This was
1186 * inspired by jmesserly's work in Frog. However, using XOR is 40%
1187 * faster than the original version in Frog.
1188 *
1189 * [1]: http://en.wikipedia.org/wiki/Jenkins_hash_function
1190 */
1183 builtin$hashCode$0(receiver) { 1191 builtin$hashCode$0(receiver) {
1184 if (receiver is num) return receiver & 0x1FFFFFFF; 1192 if (receiver is num) return JS('int', @'$0 & 0x1FFFFFFF', receiver);
ngeoffray 2012/02/28 10:13:25 Please add a TODO to make sure we change the code
ahe 2012/02/28 17:26:10 Done.
1185 if (receiver is String) { 1193 if (receiver is !String) return UNINTERCEPTED(receiver.hashCode());
1186 throw 'String.hashCode is not implemented'; 1194 int hash = 0;
1195 int length = JS('int', @'$0.length', receiver);
1196 for (int i = 0; i < length; i++) {
1197 hash ^= JS('int', @'$0.charCodeAt($1)', receiver, i);
1198 hash ^= JS("int", @"$0 << $1", hash, 10);
1199 hash ^= hash >> 6;
1187 } 1200 }
1188 if (isJsArray(receiver)) { 1201 hash ^= JS("int", @"$0 << $1", hash, 3);
1189 throw 'List.hashCode is not implemented'; 1202 hash ^= hash >> 11;
1190 } 1203 return 0x1fffffff & (hash ^ JS("int", @"$0 << $1", hash, 15));
1191 return UNINTERCEPTED(receiver.hashCode());
1192 } 1204 }
1193 1205
1194 // TODO(ahe): Dynamic may be overridden. 1206 // TODO(ahe): Dynamic may be overridden.
1195 builtin$get$dynamic(receiver) => receiver; 1207 builtin$get$dynamic(receiver) => receiver;
1196 1208
1197 /** 1209 /**
1198 * Called by generated code to capture the stacktrace before throwing 1210 * Called by generated code to capture the stacktrace before throwing
1199 * an exception. 1211 * an exception.
1200 */ 1212 */
1201 captureStackTrace(ex) { 1213 captureStackTrace(ex) {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
1263 } 1275 }
1264 } 1276 }
1265 } else if (JS('bool', @'$0 instanceof RangeError', ex)) { 1277 } else if (JS('bool', @'$0 instanceof RangeError', ex)) {
1266 var message = JS('String', @'$0.message', ex); 1278 var message = JS('String', @'$0.message', ex);
1267 if (message.contains('call stack')) { 1279 if (message.contains('call stack')) {
1268 return new StackOverflowException(); 1280 return new StackOverflowException();
1269 } 1281 }
1270 } 1282 }
1271 return ex; 1283 return ex;
1272 } 1284 }
OLDNEW
« no previous file with comments | « no previous file | dart/tests/co19/co19-leg.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698