| 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 /** | 43 /** |
| 44 * Generates the special operator method, e.g. $add. | 44 * Generates the special operator method, e.g. $add. |
| 45 * We want to do $add(x, y) instead of x.$add(y) so it doesn't box. | 45 * We want to do $add(x, y) instead of x.$add(y) so it doesn't box. |
| 46 * Same idea for the other methods. | 46 * Same idea for the other methods. |
| 47 */ | 47 */ |
| 48 void useOperator(String name) { | 48 void useOperator(String name) { |
| 49 if (_usedOperators[name] != null) return; | 49 if (_usedOperators[name] != null) return; |
| 50 | 50 |
| 51 var code; | 51 var code; |
| 52 switch (name) { | 52 switch (name) { |
| 53 case '\$ne': | 53 case ':ne': |
| 54 code = @""" | 54 code = @""" |
| 55 function $ne(x, y) { | 55 function $ne(x, y) { |
| 56 if (x == null) return y != null; | 56 if (x == null) return y != null; |
| 57 return (typeof(x) == 'number' && typeof(y) == 'number') || | 57 return (typeof(x) == 'number' && typeof(y) == 'number') || |
| 58 (typeof(x) == 'boolean' && typeof(y) == 'boolean') || | 58 (typeof(x) == 'boolean' && typeof(y) == 'boolean') || |
| 59 (typeof(x) == 'string' && typeof(y) == 'string') | 59 (typeof(x) == 'string' && typeof(y) == 'string') |
| 60 ? x != y : !x.$eq(y); | 60 ? x != y : !x.$eq(y); |
| 61 }"""; | 61 }"""; |
| 62 break; | 62 break; |
| 63 | 63 |
| 64 case '\$eq': | 64 case ':eq': |
| 65 code = @""" | 65 code = @""" |
| 66 function $eq(x, y) { | 66 function $eq(x, y) { |
| 67 if (x == null) return y == null; | 67 if (x == null) return y == null; |
| 68 return (typeof(x) == 'number' && typeof(y) == 'number') || | 68 return (typeof(x) == 'number' && typeof(y) == 'number') || |
| 69 (typeof(x) == 'boolean' && typeof(y) == 'boolean') || | 69 (typeof(x) == 'boolean' && typeof(y) == 'boolean') || |
| 70 (typeof(x) == 'string' && typeof(y) == 'string') | 70 (typeof(x) == 'string' && typeof(y) == 'string') |
| 71 ? x == y : x.$eq(y); | 71 ? x == y : x.$eq(y); |
| 72 } | 72 } |
| 73 // TODO(jimhug): Should this or should it not match equals? | 73 // TODO(jimhug): Should this or should it not match equals? |
| 74 Object.prototype.$eq = function(other) { return this === other; }"""; | 74 Object.prototype.$eq = function(other) { return this === other; }"""; |
| 75 break; | 75 break; |
| 76 | 76 |
| 77 case '\$bit_not': | 77 case ':bit_not': |
| 78 code = @""" | 78 code = @""" |
| 79 function $bit_not(x) { | 79 function $bit_not(x) { |
| 80 return (typeof(x) == 'number') ? ~x : x.$bit_not(); | 80 return (typeof(x) == 'number') ? ~x : x.$bit_not(); |
| 81 }"""; | 81 }"""; |
| 82 break; | 82 break; |
| 83 | 83 |
| 84 case '\$negate': | 84 case ':negate': |
| 85 code = @""" | 85 code = @""" |
| 86 function $negate(x) { | 86 function $negate(x) { |
| 87 return (typeof(x) == 'number') ? -x : x.$negate(); | 87 return (typeof(x) == 'number') ? -x : x.$negate(); |
| 88 }"""; | 88 }"""; |
| 89 break; | 89 break; |
| 90 | 90 |
| 91 // This relies on JS's string "+" to match Dart's. | 91 // This relies on JS's string "+" to match Dart's. |
| 92 case '\$add': | 92 case ':add': |
| 93 code = @""" | 93 code = @""" |
| 94 function $add(x, y) { | 94 function $add(x, y) { |
| 95 return ((typeof(x) == 'number' && typeof(y) == 'number') || | 95 return ((typeof(x) == 'number' && typeof(y) == 'number') || |
| 96 (typeof(x) == 'string')) | 96 (typeof(x) == 'string')) |
| 97 ? x + y : x.$add(y); | 97 ? x + y : x.$add(y); |
| 98 }"""; | 98 }"""; |
| 99 break; | 99 break; |
| 100 | 100 |
| 101 case '\$truncdiv': | 101 case ':truncdiv': |
| 102 useThrow = true; | 102 useThrow = true; |
| 103 code = @""" | 103 code = @""" |
| 104 function $truncdiv(x, y) { | 104 function $truncdiv(x, y) { |
| 105 if (typeof(x) == 'number' && typeof(y) == 'number') { | 105 if (typeof(x) == 'number' && typeof(y) == 'number') { |
| 106 if (y == 0) $throw(new IntegerDivisionByZeroException()); | 106 if (y == 0) $throw(new IntegerDivisionByZeroException()); |
| 107 var tmp = x / y; | 107 var tmp = x / y; |
| 108 return (tmp < 0) ? Math.ceil(tmp) : Math.floor(tmp); | 108 return (tmp < 0) ? Math.ceil(tmp) : Math.floor(tmp); |
| 109 } else { | 109 } else { |
| 110 return x.$truncdiv(y); | 110 return x.$truncdiv(y); |
| 111 } | 111 } |
| 112 }"""; | 112 }"""; |
| 113 break; | 113 break; |
| 114 | 114 |
| 115 case '\$mod': | 115 case ':mod': |
| 116 code = @""" | 116 code = @""" |
| 117 function $mod(x, y) { | 117 function $mod(x, y) { |
| 118 if (typeof(x) == 'number' && typeof(y) == 'number') { | 118 if (typeof(x) == 'number' && typeof(y) == 'number') { |
| 119 var result = x % y; | 119 var result = x % y; |
| 120 if (result == 0) { | 120 if (result == 0) { |
| 121 return 0; // Make sure we don't return -0.0. | 121 return 0; // Make sure we don't return -0.0. |
| 122 } else if (result < 0) { | 122 } else if (result < 0) { |
| 123 if (y < 0) { | 123 if (y < 0) { |
| 124 return result - y; | 124 return result - y; |
| 125 } else { | 125 } else { |
| 126 return result + y; | 126 return result + y; |
| 127 } | 127 } |
| 128 } | 128 } |
| 129 return result; | 129 return result; |
| 130 } else { | 130 } else { |
| 131 return x.$mod(y); | 131 return x.$mod(y); |
| 132 } | 132 } |
| 133 }"""; | 133 }"""; |
| 134 break; | 134 break; |
| 135 | 135 |
| 136 default: | 136 default: |
| 137 // All of the other helpers are generated the same way | 137 // All of the other helpers are generated the same way |
| 138 var op = TokenKind.rawOperatorFromMethod(name); | 138 var op = TokenKind.rawOperatorFromMethod(name); |
| 139 var jsname = world.toJsIdentifier(name); |
| 139 code = """ | 140 code = """ |
| 140 function ${name}(x, y) { | 141 function $jsname(x, y) { |
| 141 return (typeof(x) == 'number' && typeof(y) == 'number') | 142 return (typeof(x) == 'number' && typeof(y) == 'number') |
| 142 ? x ${op} y : x.${name}(y); | 143 ? x $op y : x.$jsname(y); |
| 143 }"""; | 144 }"""; |
| 144 break; | 145 break; |
| 145 } | 146 } |
| 146 | 147 |
| 147 _usedOperators[name] = code; | 148 _usedOperators[name] = code; |
| 148 } | 149 } |
| 149 | 150 |
| 150 // NOTE: some helpers can't be generated when we generate corelib, | 151 // NOTE: some helpers can't be generated when we generate corelib, |
| 151 // because we don't discover that we need them until later. | 152 // because we don't discover that we need them until later. |
| 152 // Generate on-demand instead | 153 // Generate on-demand instead |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 w.writeln(@"function $wrap_call$1(fn) { return fn; }"); | 447 w.writeln(@"function $wrap_call$1(fn) { return fn; }"); |
| 447 } | 448 } |
| 448 } | 449 } |
| 449 | 450 |
| 450 // Write operator helpers | 451 // Write operator helpers |
| 451 for (var opImpl in orderValuesByKeys(_usedOperators)) { | 452 for (var opImpl in orderValuesByKeys(_usedOperators)) { |
| 452 w.writeln(opImpl); | 453 w.writeln(opImpl); |
| 453 } | 454 } |
| 454 } | 455 } |
| 455 } | 456 } |
| OLD | NEW |