Index: lib/src/compiler/code_generator.dart |
diff --git a/lib/src/compiler/code_generator.dart b/lib/src/compiler/code_generator.dart |
index d0bdf2043190b9a91a642c7f84ea0751b1be688b..51b7fe5ca628002ef2d74ce10ed2a1f21489289f 100644 |
--- a/lib/src/compiler/code_generator.dart |
+++ b/lib/src/compiler/code_generator.dart |
@@ -255,6 +255,10 @@ class CodeGenerator extends GeneralizingAstVisitor |
_collectElements(unit, nodes); |
} |
_loader = new ElementLoader(nodes); |
+ if (compilationUnits.isNotEmpty) { |
+ _constField = new ConstFieldVisitor(types, |
+ dummySource: compilationUnits.first.element.source); |
+ } |
// Add implicit dart:core dependency so it is first. |
emitLibraryName(dartCoreLibrary); |
@@ -267,7 +271,7 @@ class CodeGenerator extends GeneralizingAstVisitor |
// NOTE: declarations are not necessarily emitted in this order. |
// Order will be changed as needed so the resulting code can execute. |
// This is done by forward declaring items. |
- compilationUnits.forEach(visitCompilationUnit); |
+ compilationUnits.forEach(_finishDeclarationsInUnit); |
// Declare imports |
_finishImports(items); |
@@ -439,10 +443,12 @@ class CodeGenerator extends GeneralizingAstVisitor |
_loader.declareBeforeUse(e, _emitDeclaration); |
} |
- @override |
- void visitCompilationUnit(CompilationUnit unit) { |
- _constField = new ConstFieldVisitor(types, unit.element.source); |
- |
+ void _finishDeclarationsInUnit(CompilationUnit unit) { |
+ // NOTE: this method isn't the right place to initialize |
+ // per-compilation-unit state. Declarations can be visited out of order, |
+ // this is only to catch things that haven't been emitted yet. |
+ // |
+ // See _emitDeclaration. |
for (var declaration in unit.declarations) { |
var element = declaration.element; |
if (element != null) { |
@@ -4242,11 +4248,23 @@ class CodeGenerator extends GeneralizingAstVisitor |
// Check if the target could be `null`, is dynamic, or may be an extension |
// native type. In all of those cases we need defensive code generation. |
var type = getStaticType(target); |
+ |
return isNullable(target) || |
+ _targetIsFunction(target) || |
type.isDynamic || |
(_extensionTypes.hasNativeSubtype(type) && target is! SuperExpression); |
} |
+ bool _targetIsFunction(Expression expr) { |
+ Element element = null; |
+ if (expr is PropertyAccess) { |
vsm
2016/06/16 23:41:13
Is this sufficient? I.e., what about FunctionExpr
Jennifer Messerly
2016/06/16 23:44:14
I think those should be handled by isNullable (I s
|
+ element = expr.propertyName.staticElement; |
+ } else if (expr is Identifier) { |
+ element = expr.staticElement; |
+ } |
+ return element is FunctionElement || element is MethodElement; |
+ } |
+ |
/// Shared code for [PrefixedIdentifier] and [PropertyAccess]. |
JS.Expression _emitAccess( |
Expression target, SimpleIdentifier memberId, DartType resultType) { |