[vm] Fix --enable-mirrors VM flag to affect environment and conditional imports

This change propagates --enable-mirrors VM flag to CFE via
TargetFlags.supportMirrors. CFE uses that to select conditional
imports and for constant evaluation of bool.fromEnvironment.

TEST=runtime/tests/vm/dart/enable_mirrors_test

Issue: https://github.com/dart-lang/sdk/issues/49266
Change-Id: I9f6b03f1ab224d13cbe9f035fb5b6fa6eb59055c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/249020
Reviewed-by: Ben Konyi <bkonyi@google.com>
Commit-Queue: Alexander Markov <alexmarkov@google.com>
This commit is contained in:
Alexander Markov 2022-06-17 17:20:09 +00:00 committed by Commit Bot
parent a60df664b3
commit 8bc1cda3f7
8 changed files with 120 additions and 19 deletions

View file

@ -95,7 +95,8 @@ CompilerOptions setupCompilerOptions(
List<String> errorsPlain,
List<String> errorsColorized,
String invocationModes,
String verbosityLevel) {
String verbosityLevel,
bool enableMirrors) {
final expFlags = <String>[];
if (experimentalFlags != null) {
for (String flag in experimentalFlags) {
@ -107,7 +108,8 @@ CompilerOptions setupCompilerOptions(
return new CompilerOptions()
..fileSystem = fileSystem
..target = new VmTarget(new TargetFlags(
enableNullSafety: nullSafety == kNullSafetyOptionStrong))
enableNullSafety: nullSafety == kNullSafetyOptionStrong,
supportMirrors: enableMirrors))
..packagesFileUri = packagesUri
..sdkSummary = platformKernelPath
..verbose = verbose
@ -165,6 +167,7 @@ abstract class Compiler {
final String? packageConfig;
final String invocationModes;
final String verbosityLevel;
final bool enableMirrors;
// Code coverage and hot reload are only supported by incremental compiler,
// which is used if vm-service is enabled.
@ -184,7 +187,8 @@ abstract class Compiler {
this.supportHotReload: false,
this.packageConfig: null,
this.invocationModes: '',
this.verbosityLevel: Verbosity.defaultValue}) {
this.verbosityLevel: Verbosity.defaultValue,
required this.enableMirrors}) {
Uri? packagesUri = null;
final packageConfig = this.packageConfig ?? Platform.packageConfig;
if (packageConfig != null) {
@ -208,7 +212,8 @@ abstract class Compiler {
errorsPlain,
errorsColorized,
invocationModes,
verbosityLevel);
verbosityLevel,
enableMirrors);
}
Future<CompilerResult> compile(Uri script) {
@ -295,7 +300,8 @@ class IncrementalCompilerWrapper extends Compiler {
List<String>? experimentalFlags,
String? packageConfig,
String invocationModes: '',
String verbosityLevel: Verbosity.defaultValue})
String verbosityLevel: Verbosity.defaultValue,
required bool enableMirrors})
: super(isolateGroupId, fileSystem, platformKernelPath,
enableAsserts: enableAsserts,
nullSafety: nullSafety,
@ -304,7 +310,8 @@ class IncrementalCompilerWrapper extends Compiler {
supportCodeCoverage: true,
packageConfig: packageConfig,
invocationModes: invocationModes,
verbosityLevel: verbosityLevel);
verbosityLevel: verbosityLevel,
enableMirrors: enableMirrors);
factory IncrementalCompilerWrapper.forExpressionCompilationOnly(
Component component,
@ -314,13 +321,15 @@ class IncrementalCompilerWrapper extends Compiler {
{bool enableAsserts: false,
List<String>? experimentalFlags,
String? packageConfig,
String invocationModes: ''}) {
String invocationModes: '',
required bool enableMirrors}) {
IncrementalCompilerWrapper result = IncrementalCompilerWrapper(
isolateGroupId, fileSystem, platformKernelPath,
enableAsserts: enableAsserts,
experimentalFlags: experimentalFlags,
packageConfig: packageConfig,
invocationModes: invocationModes);
invocationModes: invocationModes,
enableMirrors: enableMirrors);
result.generator = new IncrementalCompiler.forExpressionCompilationOnly(
component,
result.options,
@ -349,7 +358,8 @@ class IncrementalCompilerWrapper extends Compiler {
nullSafety: nullSafety,
experimentalFlags: experimentalFlags,
packageConfig: packageConfig,
invocationModes: invocationModes);
invocationModes: invocationModes,
enableMirrors: enableMirrors);
final generator = this.generator!;
// TODO(VM TEAM): This does not seem safe. What if cloning while having
// pending deltas for instance?
@ -384,14 +394,16 @@ class SingleShotCompilerWrapper extends Compiler {
List<String>? experimentalFlags,
String? packageConfig,
String invocationModes: '',
String verbosityLevel: Verbosity.defaultValue})
String verbosityLevel: Verbosity.defaultValue,
required bool enableMirrors})
: super(isolateGroupId, fileSystem, platformKernelPath,
enableAsserts: enableAsserts,
nullSafety: nullSafety,
experimentalFlags: experimentalFlags,
packageConfig: packageConfig,
invocationModes: invocationModes,
verbosityLevel: verbosityLevel);
verbosityLevel: verbosityLevel,
enableMirrors: enableMirrors);
@override
Future<CompilerResult> compileInternal(Uri script) async {
@ -428,7 +440,8 @@ Future<Compiler> lookupOrBuildNewIncrementalCompiler(int isolateGroupId,
String? multirootFilepaths,
String? multirootScheme,
String invocationModes: '',
String verbosityLevel: Verbosity.defaultValue}) async {
String verbosityLevel: Verbosity.defaultValue,
required bool enableMirrors}) async {
IncrementalCompilerWrapper? compiler =
lookupIncrementalCompiler(isolateGroupId);
if (compiler != null) {
@ -457,7 +470,8 @@ Future<Compiler> lookupOrBuildNewIncrementalCompiler(int isolateGroupId,
experimentalFlags: experimentalFlags,
packageConfig: packageConfig,
invocationModes: invocationModes,
verbosityLevel: verbosityLevel);
verbosityLevel: verbosityLevel,
enableMirrors: enableMirrors);
}
isolateCompilers[isolateGroupId] = compiler;
}
@ -513,6 +527,7 @@ Future _processExpressionCompilationRequest(request) async {
final bool enableAsserts = request[16];
final List<String>? experimentalFlags =
request[17] != null ? request[17].cast<String>() : null;
final bool enableMirrors = request[18];
IncrementalCompilerWrapper? compiler = isolateCompilers[isolateGroupId];
@ -595,7 +610,8 @@ Future _processExpressionCompilationRequest(request) async {
component, isolateGroupId, fileSystem, null,
enableAsserts: enableAsserts,
experimentalFlags: experimentalFlags,
packageConfig: dotPackagesFile);
packageConfig: dotPackagesFile,
enableMirrors: enableMirrors);
isolateCompilers[isolateGroupId] = compiler;
await compiler.compile(
component.mainMethod?.enclosingLibrary.importUri ??
@ -776,6 +792,7 @@ Future _processLoadRequest(request) async {
final String? multirootScheme = request[13];
final String? workingDirectory = request[14];
final String verbosityLevel = request[15];
final bool enableMirrors = request[16];
Uri platformKernelPath;
List<int>? platformKernel = null;
if (request[3] is String) {
@ -844,7 +861,8 @@ Future _processLoadRequest(request) async {
errorsPlain,
errorsColorized,
invocationModes,
verbosityLevel);
verbosityLevel,
false);
// script should only be null for kUpdateSourcesTag.
await autoDetectNullSafetyMode(script!, options);
@ -870,7 +888,8 @@ Future _processLoadRequest(request) async {
multirootFilepaths: multirootFilepaths,
multirootScheme: multirootScheme,
invocationModes: invocationModes,
verbosityLevel: verbosityLevel);
verbosityLevel: verbosityLevel,
enableMirrors: enableMirrors);
} else {
FileSystem fileSystem = _buildFileSystem(
sourceFiles, platformKernel, multirootFilepaths, multirootScheme);
@ -882,7 +901,8 @@ Future _processLoadRequest(request) async {
experimentalFlags: experimentalFlags,
packageConfig: packageConfig,
invocationModes: invocationModes,
verbosityLevel: verbosityLevel);
verbosityLevel: verbosityLevel,
enableMirrors: enableMirrors);
}
CompilationResult result;
@ -1044,6 +1064,7 @@ Future trainInternal(String scriptUri, String? platformKernelPath) async {
null /* multirootScheme */,
null /* original working directory */,
'all' /* CFE logging mode */,
true /* enableMirrors */,
];
await _processLoadRequest(request);
}

View file

@ -0,0 +1,7 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Auxiliary library for enable_mirrors_test.dart.
bool dartLibraryMirrorsConditionalImport = false;

View file

@ -0,0 +1,7 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Auxiliary library for enable_mirrors_test.dart.
bool dartLibraryMirrorsConditionalImport = true;

View file

@ -0,0 +1,18 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// VMOptions=--enable-mirrors=false
// Verfies that '--enable-mirrors=false' affects conditional imports and
// constant bool.fromEnvironment.
import "package:expect/expect.dart";
import 'enable_mirrors_lib_false.dart'
if (dart.library.mirrors) 'enable_mirrors_lib_true.dart';
main() {
Expect.isFalse(const bool.fromEnvironment('dart.library.mirrors'));
Expect.isFalse(dartLibraryMirrorsConditionalImport);
}

View file

@ -0,0 +1,9 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Auxiliary library for enable_mirrors_test.dart.
// @dart = 2.9
bool dartLibraryMirrorsConditionalImport = false;

View file

@ -0,0 +1,9 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Auxiliary library for enable_mirrors_test.dart.
// @dart = 2.9
bool dartLibraryMirrorsConditionalImport = true;

View file

@ -0,0 +1,20 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// VMOptions=--enable-mirrors=false
// Verfies that '--enable-mirrors=false' affects conditional imports and
// constant bool.fromEnvironment.
// @dart = 2.9
import "package:expect/expect.dart";
import 'enable_mirrors_lib_false.dart'
if (dart.library.mirrors) 'enable_mirrors_lib_true.dart';
main() {
Expect.isFalse(const bool.fromEnvironment('dart.library.mirrors'));
Expect.isFalse(dartLibraryMirrorsConditionalImport);
}

View file

@ -699,6 +699,10 @@ class KernelCompilationRequest : public ValueObject {
experimental_flags_object.value.as_array.values = experimental_flags_array;
experimental_flags_object.value.as_array.length = num_experimental_flags;
Dart_CObject enable_mirrors;
enable_mirrors.type = Dart_CObject_kBool;
enable_mirrors.value.as_bool = FLAG_enable_mirrors;
Dart_CObject message;
message.type = Dart_CObject_kArray;
Dart_CObject* message_arr[] = {&tag,
@ -718,7 +722,8 @@ class KernelCompilationRequest : public ValueObject {
&dills_object,
&num_blob_loads,
&enable_asserts,
&experimental_flags_object};
&experimental_flags_object,
&enable_mirrors};
message.value.as_array.values = message_arr;
message.value.as_array.length = ARRAY_SIZE(message_arr);
@ -942,6 +947,10 @@ class KernelCompilationRequest : public ValueObject {
verbosity_str.value.as_string =
const_cast<char*>(KernelCompilationVerbosityLevelToString(verbosity));
Dart_CObject enable_mirrors;
enable_mirrors.type = Dart_CObject_kBool;
enable_mirrors.value.as_bool = FLAG_enable_mirrors;
Dart_CObject* message_arr[] = {&tag,
&send_port,
&uri,
@ -957,7 +966,8 @@ class KernelCompilationRequest : public ValueObject {
&multiroot_filepaths_object,
&multiroot_scheme_object,
&original_working_directory_object,
&verbosity_str};
&verbosity_str,
&enable_mirrors};
message.value.as_array.values = message_arr;
message.value.as_array.length = ARRAY_SIZE(message_arr);
// Send the message.