OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 dev_compiler.src.codegen.js_codegen; | 5 library dev_compiler.src.codegen.js_codegen; |
6 | 6 |
7 import 'dart:collection' show HashSet, HashMap, SplayTreeSet; | 7 import 'dart:collection' show HashSet, HashMap, SplayTreeSet; |
8 | 8 |
9 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; | 9 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; |
10 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; | 10 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; |
(...skipping 1271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1282 | 1282 |
1283 @override | 1283 @override |
1284 JS.Statement visitFunctionDeclarationStatement( | 1284 JS.Statement visitFunctionDeclarationStatement( |
1285 FunctionDeclarationStatement node) { | 1285 FunctionDeclarationStatement node) { |
1286 var func = node.functionDeclaration; | 1286 var func = node.functionDeclaration; |
1287 if (func.isGetter || func.isSetter) { | 1287 if (func.isGetter || func.isSetter) { |
1288 return js.comment('Unimplemented function get/set statement: $node'); | 1288 return js.comment('Unimplemented function get/set statement: $node'); |
1289 } | 1289 } |
1290 | 1290 |
1291 var fn = _visit(func.functionExpression); | 1291 var fn = _visit(func.functionExpression); |
1292 var jsThis = new _JsThisFinder(); | |
1293 fn.accept(jsThis); | |
1294 | 1292 |
1295 var name = new JS.Identifier(func.name.name); | 1293 var name = new JS.Identifier(func.name.name); |
1296 JS.Statement declareFn; | 1294 JS.Statement declareFn; |
1297 if (jsThis.found) { | 1295 if (JS.This.foundIn(fn)) { |
1298 declareFn = js.statement('let # = #.bind(this);', [name, fn]); | 1296 declareFn = js.statement('let # = #.bind(this);', [name, fn]); |
1299 } else { | 1297 } else { |
1300 declareFn = new JS.FunctionDeclaration(name, fn); | 1298 declareFn = new JS.FunctionDeclaration(name, fn); |
1301 } | 1299 } |
1302 | 1300 |
1303 return new JS.Block([ | 1301 return new JS.Block([ |
1304 declareFn, | 1302 declareFn, |
1305 _emitFunctionTagged(name, func.element.type).toStatement() | 1303 _emitFunctionTagged(name, func.element.type).toStatement() |
1306 ]); | 1304 ]); |
1307 } | 1305 } |
(...skipping 1509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2817 | 2815 |
2818 String generateLibrary(LibraryUnit unit) { | 2816 String generateLibrary(LibraryUnit unit) { |
2819 // Clone the AST first, so we can mutate it. | 2817 // Clone the AST first, so we can mutate it. |
2820 unit = unit.clone(); | 2818 unit = unit.clone(); |
2821 var library = unit.library.element.library; | 2819 var library = unit.library.element.library; |
2822 var fields = findFieldsNeedingStorage(unit); | 2820 var fields = findFieldsNeedingStorage(unit); |
2823 var codegen = | 2821 var codegen = |
2824 new JSCodegenVisitor(compiler, library, _extensionTypes, fields); | 2822 new JSCodegenVisitor(compiler, library, _extensionTypes, fields); |
2825 var module = codegen.emitLibrary(unit); | 2823 var module = codegen.emitLibrary(unit); |
2826 var out = compiler.getOutputPath(library.source.uri); | 2824 var out = compiler.getOutputPath(library.source.uri); |
2827 return writeJsLibrary(module, out, emitSourceMaps: options.emitSourceMaps); | 2825 return writeJsLibrary(module, out, |
| 2826 emitSourceMaps: options.emitSourceMaps, |
| 2827 arrowFnBindThisWorkaround: options.arrowFnBindThisWorkaround); |
2828 } | 2828 } |
2829 } | 2829 } |
2830 | 2830 |
2831 /// Choose a canonical name from the library element. | 2831 /// Choose a canonical name from the library element. |
2832 /// This never uses the library's name (the identifier in the `library` | 2832 /// This never uses the library's name (the identifier in the `library` |
2833 /// declaration) as it doesn't have any meaningful rules enforced. | 2833 /// declaration) as it doesn't have any meaningful rules enforced. |
2834 String jsLibraryName(LibraryElement library) => canonicalLibraryName(library); | 2834 String jsLibraryName(LibraryElement library) => canonicalLibraryName(library); |
2835 | 2835 |
2836 /// Shorthand for identifier-like property names. | 2836 /// Shorthand for identifier-like property names. |
2837 /// For now, we emit them as strings and the printer restores them to | 2837 /// For now, we emit them as strings and the printer restores them to |
(...skipping 14 matching lines...) Expand all Loading... |
2852 | 2852 |
2853 /// A special kind of element created by the compiler, signifying a temporary | 2853 /// A special kind of element created by the compiler, signifying a temporary |
2854 /// variable. These objects use instance equality, and should be shared | 2854 /// variable. These objects use instance equality, and should be shared |
2855 /// everywhere in the tree where they are treated as the same variable. | 2855 /// everywhere in the tree where they are treated as the same variable. |
2856 class TemporaryVariableElement extends LocalVariableElementImpl { | 2856 class TemporaryVariableElement extends LocalVariableElementImpl { |
2857 TemporaryVariableElement.forNode(Identifier name) : super.forNode(name); | 2857 TemporaryVariableElement.forNode(Identifier name) : super.forNode(name); |
2858 | 2858 |
2859 int get hashCode => identityHashCode(this); | 2859 int get hashCode => identityHashCode(this); |
2860 bool operator ==(Object other) => identical(this, other); | 2860 bool operator ==(Object other) => identical(this, other); |
2861 } | 2861 } |
2862 | |
2863 class _JsThisFinder extends JS.BaseVisitor { | |
2864 bool found = false; | |
2865 visitThis(JS.This node) { | |
2866 found = true; | |
2867 } | |
2868 visitNode(JS.Node node) { | |
2869 if (!found) super.visitNode(node); | |
2870 } | |
2871 } | |
OLD | NEW |