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; | 7 import 'dart:collection' show HashSet, HashMap; |
8 import 'dart:io' show Directory, File; | 8 import 'dart:io' show Directory, File; |
9 | 9 |
10 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; | 10 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; |
(...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 // Implements Dart constructor behavior. Because of V8 `super` | 609 // Implements Dart constructor behavior. Because of V8 `super` |
610 // [constructor restrictions] | 610 // [constructor restrictions] |
611 // (https://code.google.com/p/v8/issues/detail?id=3330#c65) | 611 // (https://code.google.com/p/v8/issues/detail?id=3330#c65) |
612 // we cannot currently emit actual ES6 constructors with super calls. | 612 // we cannot currently emit actual ES6 constructors with super calls. |
613 // Instead we use the same trick as named constructors, and do them as | 613 // Instead we use the same trick as named constructors, and do them as |
614 // instance methods that perform initialization. | 614 // instance methods that perform initialization. |
615 // TODO(jmesserly): we'll need to rethink this once the ES6 spec and V8 | 615 // TODO(jmesserly): we'll need to rethink this once the ES6 spec and V8 |
616 // settles. See <https://github.com/dart-lang/dev_compiler/issues/51>. | 616 // settles. See <https://github.com/dart-lang/dev_compiler/issues/51>. |
617 // Performance of this pattern is likely to be bad. | 617 // Performance of this pattern is likely to be bad. |
618 name = 'constructor'; | 618 name = 'constructor'; |
| 619 // Mark the parameter as no-rename. |
| 620 var args = new JS.Identifier('arguments', allowRename: false); |
619 body = js.statement('''{ | 621 body = js.statement('''{ |
620 // Get the class name for this instance. | 622 // Get the class name for this instance. |
621 var name = this.constructor.name; | 623 var name = this.constructor.name; |
622 // Call the default constructor. | 624 // Call the default constructor. |
623 var init = this[name]; | 625 var init = this[name]; |
624 var result = void 0; | 626 var result = void 0; |
625 if (init) result = init.apply(this, arguments); | 627 if (init) result = init.apply(this, #); |
626 return result === void 0 ? this : result; | 628 return result === void 0 ? this : result; |
627 }'''); | 629 }''', args); |
628 } else { | 630 } else { |
629 body = _emitConstructorBody(node, fields); | 631 body = _emitConstructorBody(node, fields); |
630 } | 632 } |
631 | 633 |
632 // We generate constructors as initializer methods in the class; | 634 // We generate constructors as initializer methods in the class; |
633 // this allows use of `super` for instance methods/properties. | 635 // this allows use of `super` for instance methods/properties. |
634 // It also avoids V8 restrictions on `super` in default constructors. | 636 // It also avoids V8 restrictions on `super` in default constructors. |
635 return new JS.Method( | 637 return new JS.Method( |
636 _propertyName(name), new JS.Fun(_visit(node.parameters), body)) | 638 _propertyName(name), new JS.Fun(_visit(node.parameters), body)) |
637 ..sourceInformation = node; | 639 ..sourceInformation = node; |
(...skipping 1714 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2352 | 2354 |
2353 // TODO(jmesserly): in many cases marking the end will be unncessary. | 2355 // TODO(jmesserly): in many cases marking the end will be unncessary. |
2354 printer.mark(_location(node.end)); | 2356 printer.mark(_location(node.end)); |
2355 } | 2357 } |
2356 | 2358 |
2357 String _getIdentifier(AstNode node) { | 2359 String _getIdentifier(AstNode node) { |
2358 if (node is SimpleIdentifier) return node.name; | 2360 if (node is SimpleIdentifier) return node.name; |
2359 return null; | 2361 return null; |
2360 } | 2362 } |
2361 } | 2363 } |
OLD | NEW |