Index: pkg/dev_compiler/lib/src/compiler/code_generator.dart |
diff --git a/pkg/dev_compiler/lib/src/compiler/code_generator.dart b/pkg/dev_compiler/lib/src/compiler/code_generator.dart |
index 89bbabc1d0c553611a3a8f21e5adf1839c99bf85..d9e763670379889d0d0ee0474bce60a9f9cc4857 100644 |
--- a/pkg/dev_compiler/lib/src/compiler/code_generator.dart |
+++ b/pkg/dev_compiler/lib/src/compiler/code_generator.dart |
@@ -125,7 +125,7 @@ class CodeGenerator extends GeneralizingAstVisitor |
final ClassElement objectClass; |
final ClassElement stringClass; |
- ConstFieldVisitor _constField; |
+ ConstFieldVisitor _constants; |
/// The current function body being compiled. |
FunctionBody _currentFunction; |
@@ -276,7 +276,7 @@ class CodeGenerator extends GeneralizingAstVisitor |
} |
_loader = new ElementLoader(nodes); |
if (compilationUnits.isNotEmpty) { |
- _constField = new ConstFieldVisitor(types, |
+ _constants = new ConstFieldVisitor(context, |
dummySource: compilationUnits.first.element.source); |
} |
@@ -2120,7 +2120,7 @@ class CodeGenerator extends GeneralizingAstVisitor |
for (var declaration in fieldDecls) { |
for (var fieldNode in declaration.fields.variables) { |
var element = fieldNode.element; |
- if (_constField.isFieldInitConstant(fieldNode)) { |
+ if (_constants.isFieldInitConstant(fieldNode)) { |
unsetFields[element as FieldElement] = fieldNode; |
} else { |
fields[element as FieldElement] = _visitInitializer(fieldNode); |
@@ -3767,7 +3767,7 @@ class CodeGenerator extends GeneralizingAstVisitor |
bool isLoaded = _loader.finishCheckingReferences(); |
bool eagerInit = |
- isLoaded && (field.isConst || _constField.isFieldInitConstant(field)); |
+ isLoaded && (field.isConst || _constants.isFieldInitConstant(field)); |
var fieldName = field.name.name; |
if (eagerInit && |
@@ -3796,7 +3796,7 @@ class CodeGenerator extends GeneralizingAstVisitor |
bool eagerInit; |
JS.Expression jsInit; |
- if (field.isConst || _constField.isFieldInitConstant(field)) { |
+ if (field.isConst || _constants.isFieldInitConstant(field)) { |
// If the field is constant, try and generate it at the top level. |
_loader.startTopLevel(element); |
jsInit = _visitInitializer(field); |
@@ -3961,6 +3961,41 @@ class CodeGenerator extends GeneralizingAstVisitor |
var constructor = node.constructorName; |
var name = constructor.name; |
var type = constructor.type.type; |
+ if (node.isConst && |
+ element?.name == 'fromEnvironment' && |
+ element.library.isDartCore) { |
+ var value = node.accept(_constants.constantVisitor); |
+ |
+ if (value == null || value.isNull) { |
+ return new JS.LiteralNull(); |
+ } |
+ // Handle unknown value: when the declared variable wasn't found, and no |
+ // explicit default value was passed either. |
+ // TODO(jmesserly): ideally Analyzer would simply resolve this to the |
+ // default value that is specified in the SDK. Instead we implement that |
+ // here. `bool.fromEnvironment` defaults to `false`, the others to `null`: |
+ // https://api.dartlang.org/stable/1.20.1/dart-core/bool/bool.fromEnvironment.html |
+ if (value.isUnknown) { |
+ return type == types.boolType |
+ ? js.boolean(false) |
+ : new JS.LiteralNull(); |
+ } |
+ if (value.type == types.boolType) { |
+ var boolValue = value.toBoolValue(); |
+ return boolValue != null ? js.boolean(boolValue) : new JS.LiteralNull(); |
+ } |
+ if (value.type == types.intType) { |
+ var intValue = value.toIntValue(); |
+ return intValue != null ? js.number(intValue) : new JS.LiteralNull(); |
+ } |
+ if (value.type == types.stringType) { |
+ var stringValue = value.toStringValue(); |
+ return stringValue != null |
+ ? js.escapedString(stringValue) |
+ : new JS.LiteralNull(); |
+ } |
+ throw new StateError('failed to evaluate $node'); |
+ } |
return _emitInstanceCreationExpression( |
element, type, name, node.argumentList, node.isConst); |
} |