| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 fasta.compiler_context; | 5 library fasta.compiler_context; |
| 6 | 6 |
| 7 import 'dart:async' show Zone, runZoned; | 7 import 'dart:async' show Zone, runZoned; |
| 8 | 8 |
| 9 import 'package:kernel/ast.dart' show Source; | 9 import 'package:kernel/ast.dart' show Source; |
| 10 | 10 |
| 11 import 'compiler_command_line.dart' show CompilerCommandLine; | 11 import 'compiler_command_line.dart' show CompilerCommandLine; |
| 12 | 12 |
| 13 import 'colors.dart' show computeEnableColors; |
| 14 |
| 13 final Object compilerContextKey = new Object(); | 15 final Object compilerContextKey = new Object(); |
| 14 | 16 |
| 15 final CompilerContext rootContext = | 17 final CompilerContext rootContext = |
| 16 new CompilerContext(CompilerCommandLine.forRootContext()); | 18 new CompilerContext(CompilerCommandLine.forRootContext()); |
| 17 | 19 |
| 18 class CompilerContext { | 20 class CompilerContext { |
| 19 final CompilerCommandLine options; | 21 final CompilerCommandLine options; |
| 20 | 22 |
| 21 final Map<String, Source> uriToSource = <String, Source>{}; | 23 final Map<String, Source> uriToSource = <String, Source>{}; |
| 22 | 24 |
| 25 bool enableColorsCached = null; |
| 26 |
| 23 CompilerContext(this.options); | 27 CompilerContext(this.options); |
| 24 | 28 |
| 25 static CompilerContext get current { | 29 static CompilerContext get current { |
| 26 return Zone.current[compilerContextKey] ?? rootContext; | 30 return Zone.current[compilerContextKey] ?? rootContext; |
| 27 } | 31 } |
| 28 | 32 |
| 29 /// Perform [action] in a [Zone] where [cl] will be available as | 33 /// Perform [action] in a [Zone] where [cl] will be available as |
| 30 /// `CompilerContext.current.options`. | 34 /// `CompilerContext.current.options`. |
| 31 static dynamic withGlobalOptions( | 35 static dynamic withGlobalOptions( |
| 32 CompilerCommandLine cl, dynamic action(CompilerContext c)) { | 36 CompilerCommandLine cl, dynamic action(CompilerContext c)) { |
| 33 CompilerContext c = new CompilerContext(cl); | 37 CompilerContext c = new CompilerContext(cl); |
| 34 return runZoned(() => action(c), zoneValues: {compilerContextKey: c}); | 38 return runZoned(() => action(c), zoneValues: {compilerContextKey: c}); |
| 35 } | 39 } |
| 40 |
| 41 static bool get enableColors { |
| 42 return current.enableColorsCached ??= computeEnableColors(current); |
| 43 } |
| 36 } | 44 } |
| OLD | NEW |