OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 /** | 5 /** |
6 * Generates JS helpers for dart:core. This used to be in a file "core.js". | 6 * Generates JS helpers for dart:core. This used to be in a file "core.js". |
7 * Having them in Dart code means we can easily control which are generated. | 7 * Having them in Dart code means we can easily control which are generated. |
8 */ | 8 */ |
9 // TODO(jmesserly): one idea to make this cleaner: put these as private "native" | 9 // TODO(jmesserly): one idea to make this cleaner: put these as private "native" |
10 // methods somewhere in a library that we import. This would be rather elegant | 10 // methods somewhere in a library that we import. This would be rather elegant |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 _INDEX_OPERATORS : _CHECKED_INDEX_OPERATORS); | 170 _INDEX_OPERATORS : _CHECKED_INDEX_OPERATORS); |
171 } | 171 } |
172 | 172 |
173 if (useSetIndex) { | 173 if (useSetIndex) { |
174 markCorelibTypeUsed('NoSuchMethodException'); | 174 markCorelibTypeUsed('NoSuchMethodException'); |
175 ensureDefProp(); | 175 ensureDefProp(); |
176 w.writeln(options.disableBoundsChecks ? | 176 w.writeln(options.disableBoundsChecks ? |
177 _SETINDEX_OPERATORS : _CHECKED_SETINDEX_OPERATORS); | 177 _SETINDEX_OPERATORS : _CHECKED_SETINDEX_OPERATORS); |
178 } | 178 } |
179 | 179 |
180 if (useIsolates) { | 180 if (!useIsolates) { |
181 if (useWrap0) { | |
182 w.writeln(_WRAP_CALL0_FUNCTION); | |
183 } | |
184 if (useWrap1) { | |
185 w.writeln(_WRAP_CALL1_FUNCTION); | |
186 } | |
187 w.writeln(_ISOLATE_INIT_CODE); | |
188 } else { | |
189 if (useWrap0) { | 181 if (useWrap0) { |
190 w.writeln(_EMPTY_WRAP_CALL0_FUNCTION); | 182 w.writeln(_EMPTY_WRAP_CALL0_FUNCTION); |
191 } | 183 } |
192 if (useWrap1) { | 184 if (useWrap1) { |
193 w.writeln(_EMPTY_WRAP_CALL1_FUNCTION); | 185 w.writeln(_EMPTY_WRAP_CALL1_FUNCTION); |
194 } | 186 } |
195 } | 187 } |
196 | 188 |
197 // Write operator helpers | 189 // Write operator helpers |
198 for (var opImpl in orderValuesByKeys(_usedOperators)) { | 190 for (var opImpl in orderValuesByKeys(_usedOperators)) { |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
530 $defProp(Array.prototype, '$setindex', function(index, value) { | 522 $defProp(Array.prototype, '$setindex', function(index, value) { |
531 var i = index | 0; | 523 var i = index | 0; |
532 if (i !== index) { | 524 if (i !== index) { |
533 throw new IllegalArgumentException('index is not int'); | 525 throw new IllegalArgumentException('index is not int'); |
534 } else if (i < 0 || i >= this.length) { | 526 } else if (i < 0 || i >= this.length) { |
535 throw new IndexOutOfRangeException(index); | 527 throw new IndexOutOfRangeException(index); |
536 } | 528 } |
537 return this[i] = value; | 529 return this[i] = value; |
538 });"""; | 530 });"""; |
539 | 531 |
540 /** Snippet for `$wrap_call$0`. */ | |
541 final String _WRAP_CALL0_FUNCTION = @""" | |
542 // Wrap a 0-arg dom-callback to bind it with the current isolate: | |
543 function $wrap_call$0(fn) { return fn && fn.wrap$call$0(); } | |
544 Function.prototype.wrap$call$0 = function() { | |
545 var isolateContext = $globalState.currentContext; | |
546 var self = this; | |
547 this.wrap$0 = function() { | |
548 isolateContext.eval(self); | |
549 $globalState.topEventLoop.run(); | |
550 }; | |
551 this.wrap$call$0 = function() { return this.wrap$0; }; | |
552 return this.wrap$0; | |
553 }"""; | |
554 | |
555 /** Snippet for `$wrap_call$0`, in case it was not necessary. */ | 532 /** Snippet for `$wrap_call$0`, in case it was not necessary. */ |
556 final String _EMPTY_WRAP_CALL0_FUNCTION = | 533 final String _EMPTY_WRAP_CALL0_FUNCTION = |
557 @"function $wrap_call$0(fn) { return fn; }"; | 534 @"function $wrap_call$0(fn) { return fn; }"; |
558 | 535 |
559 /** Snippet for `$wrap_call$1`. */ | |
560 final String _WRAP_CALL1_FUNCTION = @""" | |
561 // Wrap a 1-arg dom-callback to bind it with the current isolate: | |
562 function $wrap_call$1(fn) { return fn && fn.wrap$call$1(); } | |
563 Function.prototype.wrap$call$1 = function() { | |
564 var isolateContext = $globalState.currentContext; | |
565 var self = this; | |
566 this.wrap$1 = function(arg) { | |
567 isolateContext.eval(function() { self(arg); }); | |
568 $globalState.topEventLoop.run(); | |
569 }; | |
570 this.wrap$call$1 = function() { return this.wrap$1; }; | |
571 return this.wrap$1; | |
572 }"""; | |
573 | |
574 /** Snippet for `$wrap_call$1`, in case it was not necessary. */ | 536 /** Snippet for `$wrap_call$1`, in case it was not necessary. */ |
575 final String _EMPTY_WRAP_CALL1_FUNCTION = | 537 final String _EMPTY_WRAP_CALL1_FUNCTION = |
576 @"function $wrap_call$1(fn) { return fn; }"; | 538 @"function $wrap_call$1(fn) { return fn; }"; |
577 | 539 |
578 /** Snippet that initializes the isolates state. */ | |
579 final String _ISOLATE_INIT_CODE = @""" | |
580 var $globalThis = this; | |
581 var $globals = null; | |
582 var $globalState = null;"""; | |
583 | |
584 | |
585 /** Snippet that initializes Function.prototype.bind. */ | 540 /** Snippet that initializes Function.prototype.bind. */ |
586 final String _BIND_CODE = @""" | 541 final String _BIND_CODE = @""" |
587 Function.prototype.bind = Function.prototype.bind || | 542 Function.prototype.bind = Function.prototype.bind || |
588 function(thisObj) { | 543 function(thisObj) { |
589 var func = this; | 544 var func = this; |
590 var funcLength = func.$length || func.length; | 545 var funcLength = func.$length || func.length; |
591 var argsLength = arguments.length; | 546 var argsLength = arguments.length; |
592 if (argsLength > 1) { | 547 if (argsLength > 1) { |
593 var boundArgs = Array.prototype.slice.call(arguments, 1); | 548 var boundArgs = Array.prototype.slice.call(arguments, 1); |
594 var bound = function() { | 549 var bound = function() { |
595 // Prepend the bound arguments to the current arguments. | 550 // Prepend the bound arguments to the current arguments. |
596 var newArgs = Array.prototype.slice.call(arguments); | 551 var newArgs = Array.prototype.slice.call(arguments); |
597 Array.prototype.unshift.apply(newArgs, boundArgs); | 552 Array.prototype.unshift.apply(newArgs, boundArgs); |
598 return func.apply(thisObj, newArgs); | 553 return func.apply(thisObj, newArgs); |
599 }; | 554 }; |
600 bound.$length = Math.max(0, funcLength - (argsLength - 1)); | 555 bound.$length = Math.max(0, funcLength - (argsLength - 1)); |
601 return bound; | 556 return bound; |
602 } else { | 557 } else { |
603 var bound = function() { | 558 var bound = function() { |
604 return func.apply(thisObj, arguments); | 559 return func.apply(thisObj, arguments); |
605 }; | 560 }; |
606 bound.$length = funcLength; | 561 bound.$length = funcLength; |
607 return bound; | 562 return bound; |
608 } | 563 } |
609 };"""; | 564 };"""; |
OLD | NEW |