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

Side by Side Diff: dart/lib/compiler/implementation/lib/interceptors.dart

Issue 11193032: Use JavaScript's Array.sort. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Minor tweak to sort$0 Created 8 years, 2 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
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('dart:_interceptors'); 5 #library('dart:_interceptors');
6 6
7 #import('dart:coreimpl'); 7 #import('dart:coreimpl');
8 8
9 add$1(var receiver, var value) { 9 add$1(var receiver, var value) {
10 if (isJsArray(receiver)) { 10 if (isJsArray(receiver)) {
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 return UNINTERCEPTED(receiver.every(f)); 355 return UNINTERCEPTED(receiver.every(f));
356 } else { 356 } else {
357 return Collections.every(receiver, f); 357 return Collections.every(receiver, f);
358 } 358 }
359 } 359 }
360 360
361 // TODO(ngeoffray): Make it possible to have just one "sort" function ends 361 // TODO(ngeoffray): Make it possible to have just one "sort" function ends
362 // an optional parameter. 362 // an optional parameter.
363 363
364 sort$0(receiver) { 364 sort$0(receiver) {
365 if (!isJsArray(receiver)) return UNINTERCEPTED(receiver.sort()); 365 if (!isJsArray(receiver)) return UNINTERCEPTED(receiver.sort());
floitsch 2012/10/18 12:27:22 After this if I would call sort$1(receiver, Compar
Lasse Reichstein Nielsen 2012/10/18 13:04:00 The trick is that this is simpler because we know
floitsch 2012/10/18 13:46:51 Since the shortcut only applies when it is going t
366 checkMutable(receiver, 'sort'); 366 checkMutable(receiver, 'sort');
367 DualPivotQuicksort.sort(receiver, Comparable.compare); 367
368 int length = JS('int', '#.length', receiver);
369 if (length == 0 || length == 1) return;
Lasse Reichstein Nielsen 2012/10/18 13:04:00 <= 1 should be fine, a JS Array's length can't be
370 for (int i = 0; i < length; i++) {
371 // JavaScript's Array.prototype.sort has wierd semantics for null.
372 // It won't call compare and automatically pushes null/undefined
373 // to the end.
374 if (JS('var', '#[#]', receiver, i) == null) {
375 var other = JS('var', '#[#]', receiver, 0);
376 if (i == 0) {
377 other = JS('var', '#[#]', receiver, 1);
378 }
379 // Provoke a null error by comparing null to another element in
380 // the array.
381 Comparable.compare(null, other);
382 }
383 }
384
385 JS('void', '#.sort(#)', receiver, DART_CLOSURE_TO_JS(Comparable.compare));
368 } 386 }
369 387
370 sort$1(receiver, compare) { 388 sort$1(receiver, compare) {
371 if (!isJsArray(receiver)) return UNINTERCEPTED(receiver.sort(compare)); 389 if (!isJsArray(receiver)) return UNINTERCEPTED(receiver.sort(compare));
372 checkMutable(receiver, 'sort'); 390 checkMutable(receiver, 'sort');
373 DualPivotQuicksort.sort(receiver, compare); 391 int length = JS('int', '#.length', receiver);
floitsch 2012/10/18 12:27:22 Just tell dart2js that receiver is a JavaScript-ar
ahe 2012/11/23 08:43:42 How do I do that?
sra1 2012/11/23 10:57:33 This code is dominated by isJsArray(receiver). Why
floitsch 2012/11/23 13:00:51 It should be, and now with the new interceptor app
392 if (length == 0 || length == 1) return;
393 bool hasNull = false;
394 var array = receiver;
395 for (int i = 0; i < length; i++) {
floitsch 2012/10/18 12:27:22 Given that you already run through the array, you
sra1 2012/11/23 10:57:33 This assumes compareTo returns num, not int. I'm
396 // JavaScript's Array.prototype.sort has wierd semantics for null.
floitsch 2012/10/18 12:27:22 weird
397 // It won't call compare and automatically pushes null/undefined
398 // to the end.
399 if (JS('var', '#[#]', array, i) == null) {
400 if (!hasNull) {
401 array = JS('var', '#.slice(0)', array);
floitsch 2012/10/18 12:27:22 Why do we need a copy? To avoid the try/finally?
Lasse Reichstein Nielsen 2012/10/19 07:03:21 Because any NullSentinel in the original array wou
402 }
403 JS('var', '#[#] = #', array, i, const NullSentinel());
404 hasNull = true;
405 }
406 }
407 var jsCompare;
408 if (hasNull) {
floitsch 2012/10/18 12:27:22 I would special case for the Compare.compare: if (
409 jsCompare = DART_CLOSURE_TO_JS(compareHelperNull);
410 } else {
411 jsCompare = DART_CLOSURE_TO_JS(compareHelper);
412 }
413 // [DART_CLOSURE_TO_JS] only accepts static or top-level functions.
414 // So we use [compareHelper] and JavaScript's
415 // Function.prototype.bind to create a new closure we can safely
416 // pass to JavaScript.
417 jsCompare = JS('var', '#.bind(#, #)', jsCompare, null, compare);
Lasse Reichstein Nielsen 2012/10/18 11:54:50 Is 'bind' fast now? That would be great :) Try re
418
419 JS('void', '#.sort(#)', receiver, jsCompare);
420
421 if (hasNull) {
422 for (int i = 0; i < length; i++) {
423 var element = JS('var', '#[#]', array, i);
424 if (const NullSentinel() == element) {
425 element = null;
426 }
427 JS('var', '#[#] = #', receiver, i, element);
428 }
429 }
374 } 430 }
375 431
376 isNegative(receiver) { 432 isNegative(receiver) {
377 if (receiver is num) { 433 if (receiver is num) {
378 return (receiver == 0) ? (1 / receiver) < 0 : receiver < 0; 434 return (receiver == 0) ? (1 / receiver) < 0 : receiver < 0;
379 } else { 435 } else {
380 return UNINTERCEPTED(receiver.isNegative()); 436 return UNINTERCEPTED(receiver.isNegative());
381 } 437 }
382 } 438 }
383 439
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
677 } else { 733 } else {
678 return UNINTERCEPTED(receiver.runtimeType); 734 return UNINTERCEPTED(receiver.runtimeType);
679 } 735 }
680 } 736 }
681 737
682 // TODO(lrn): These getters should be generated automatically for all 738 // TODO(lrn): These getters should be generated automatically for all
683 // intercepted methods. 739 // intercepted methods.
684 get$toString(receiver) => () => toString(receiver); 740 get$toString(receiver) => () => toString(receiver);
685 741
686 get$hashCode(receiver) => () => hashCode(receiver); 742 get$hashCode(receiver) => () => hashCode(receiver);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698