| 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 10 matching lines...) Expand all Loading... |
| 21 bool useWrap1 = false; | 21 bool useWrap1 = false; |
| 22 bool useIsolates = false; | 22 bool useIsolates = false; |
| 23 | 23 |
| 24 // These helpers had to switch to a new pattern, because they can be generated | 24 // These helpers had to switch to a new pattern, because they can be generated |
| 25 // after everything else. | 25 // after everything else. |
| 26 bool _generatedTypeNameOf = false; | 26 bool _generatedTypeNameOf = false; |
| 27 bool _generatedDynamicProto = false; | 27 bool _generatedDynamicProto = false; |
| 28 bool _generatedDynamicSetMetadata = false; | 28 bool _generatedDynamicSetMetadata = false; |
| 29 bool _generatedInherits = false; | 29 bool _generatedInherits = false; |
| 30 bool _generatedDefProp = false; | 30 bool _generatedDefProp = false; |
| 31 bool _generatedBind = false; |
| 31 | 32 |
| 32 | 33 |
| 33 Map<String, String> _usedOperators; | 34 Map<String, String> _usedOperators; |
| 34 | 35 |
| 35 CodeWriter writer; | 36 CodeWriter writer; |
| 36 | 37 |
| 37 CoreJs(): _usedOperators = {}, writer = new CodeWriter(); | 38 CoreJs(): _usedOperators = {}, writer = new CodeWriter(); |
| 38 | 39 |
| 39 /** | 40 /** |
| 40 * Generates the special operator method, e.g. $add. | 41 * Generates the special operator method, e.g. $add. |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 writer.writeln(_INHERITS_FUNCTION); | 122 writer.writeln(_INHERITS_FUNCTION); |
| 122 } | 123 } |
| 123 | 124 |
| 124 /** Generates the $defProp function when it's first used. */ | 125 /** Generates the $defProp function when it's first used. */ |
| 125 void ensureDefProp() { | 126 void ensureDefProp() { |
| 126 if (_generatedDefProp) return; | 127 if (_generatedDefProp) return; |
| 127 _generatedDefProp = true; | 128 _generatedDefProp = true; |
| 128 writer.writeln(_DEF_PROP_FUNCTION); | 129 writer.writeln(_DEF_PROP_FUNCTION); |
| 129 } | 130 } |
| 130 | 131 |
| 132 void ensureBind() { |
| 133 if (_generatedBind) return; |
| 134 _generatedBind = true; |
| 135 writer.writeln(_BIND_CODE); |
| 136 } |
| 137 |
| 131 void generate(CodeWriter w) { | 138 void generate(CodeWriter w) { |
| 132 // Write any stuff we had queued up, then replace our writer with the one | 139 // Write any stuff we had queued up, then replace our writer with the one |
| 133 // in WorldGenerator so anything we discover that we need later on will be | 140 // in WorldGenerator so anything we discover that we need later on will be |
| 134 // generated on-demand. | 141 // generated on-demand. |
| 135 w.write(writer.text); | 142 w.write(writer.text); |
| 136 writer = w; | 143 writer = w; |
| 137 | 144 |
| 138 if (useNotNullBool) { | 145 if (useNotNullBool) { |
| 139 useThrow = true; | 146 useThrow = true; |
| 140 w.writeln(_NOTNULL_BOOL_FUNCTION); | 147 w.writeln(_NOTNULL_BOOL_FUNCTION); |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 // prototype. TODO(jmesserly): make this go away by handling index more | 429 // prototype. TODO(jmesserly): make this go away by handling index more |
| 423 // like a normal method. | 430 // like a normal method. |
| 424 final String _INDEX_OPERATORS = @""" | 431 final String _INDEX_OPERATORS = @""" |
| 425 $defProp(Object.prototype, '$index', function(i) { | 432 $defProp(Object.prototype, '$index', function(i) { |
| 426 var proto = Object.getPrototypeOf(this); | 433 var proto = Object.getPrototypeOf(this); |
| 427 if (proto !== Object) { | 434 if (proto !== Object) { |
| 428 proto.$index = function(i) { return this[i]; } | 435 proto.$index = function(i) { return this[i]; } |
| 429 } | 436 } |
| 430 return this[i]; | 437 return this[i]; |
| 431 }); | 438 }); |
| 432 $defProp(Array.prototype, '$index', function(i) { | 439 $defProp(Array.prototype, '$index', function(i) { |
| 433 return this[i]; | 440 return this[i]; |
| 434 }); | 441 }); |
| 435 $defProp(String.prototype, '$index', function(i) { | 442 $defProp(String.prototype, '$index', function(i) { |
| 436 return this[i]; | 443 return this[i]; |
| 437 });"""; | 444 });"""; |
| 438 | 445 |
| 439 final String _CHECKED_INDEX_OPERATORS = @""" | 446 final String _CHECKED_INDEX_OPERATORS = @""" |
| 440 $defProp(Object.prototype, '$index', function(i) { | 447 $defProp(Object.prototype, '$index', function(i) { |
| 441 var proto = Object.getPrototypeOf(this); | 448 var proto = Object.getPrototypeOf(this); |
| 442 if (proto !== Object) { | 449 if (proto !== Object) { |
| 443 proto.$index = function(i) { return this[i]; } | 450 proto.$index = function(i) { return this[i]; } |
| 444 } | 451 } |
| 445 return this[i]; | 452 return this[i]; |
| 446 }); | 453 }); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 525 | 532 |
| 526 /** Snippet for `$wrap_call$1`, in case it was not necessary. */ | 533 /** Snippet for `$wrap_call$1`, in case it was not necessary. */ |
| 527 final String _EMPTY_WRAP_CALL1_FUNCTION = | 534 final String _EMPTY_WRAP_CALL1_FUNCTION = |
| 528 @"function $wrap_call$1(fn) { return fn; }"; | 535 @"function $wrap_call$1(fn) { return fn; }"; |
| 529 | 536 |
| 530 /** Snippet that initializes the isolates state. */ | 537 /** Snippet that initializes the isolates state. */ |
| 531 final String _ISOLATE_INIT_CODE = @""" | 538 final String _ISOLATE_INIT_CODE = @""" |
| 532 var $globalThis = this; | 539 var $globalThis = this; |
| 533 var $globals = null; | 540 var $globals = null; |
| 534 var $globalState = null;"""; | 541 var $globalState = null;"""; |
| 542 |
| 543 |
| 544 /** Snippet that initializes Function.prototype.bind. */ |
| 545 final String _BIND_CODE = @""" |
| 546 Function.prototype.bind = Function.prototype.bind || |
| 547 function(thisObj, args) { |
| 548 var func = this; |
| 549 if (typeof args !== 'undefined') { |
| 550 var boundArgs = Array.prototype.slice.call(arguments, 3); |
| 551 return function() { |
| 552 // Prepend the bound arguments to the current arguments. |
| 553 var newArgs = Array.prototype.slice.call(arguments); |
| 554 Array.prototype.unshift.apply(newArgs, boundArgs); |
| 555 return func.apply(thisObj, newArgs); |
| 556 }; |
| 557 } else { |
| 558 return function() { |
| 559 return func.apply(thisObj, arguments); |
| 560 }; |
| 561 } |
| 562 };"""; |
| OLD | NEW |