Skip to content

Commit f4b41f0

Browse files
osa1Commit Queue
authored andcommitted
[dart2wasm] Fix handling of --define/-D
When parsing `--define` or `-D` arguments don't split the the value by commas. This is consistent with how dart2js handles `-D`, but inconsistent with how VM handles it. Example: void main() { print(const String.fromEnvironment("FOO")); } When compiled with `dart compile js -DFOO="a, b"` and run, dart2js prints a, b VM prints (when compiled to exe) a Between these two, I think dart2js' behavior is more common, so we follow dart2js. Also update compile_benchmark to avoid splitting a single argument "a b" into "a" and "b" when parsing the arguments and then splicing them back before calling `dart2wasm`. Also update the test runner and ddc batch mode argument parser to handle splitting quoted arguments in `// dart2jsOption = ...` and the same options for ddc and dart2wasm, by moving dart2js's `splitLine` to a new library and reusing it in the test runner and ddc. Fixes flutter/flutter#164873. See also #60341 for relevant future work. Change-Id: Idbdf69072fa212c8e4a390990577eb5a57b49e8a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/415280 Reviewed-by: Martin Kustermann <[email protected]> Commit-Queue: Ömer Ağacan <[email protected]>
1 parent 367474d commit f4b41f0

File tree

16 files changed

+66
-26
lines changed

16 files changed

+66
-26
lines changed

pkg/compiler/lib/src/dart2js.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'dart:isolate' show Isolate;
1111

1212
// ignore: implementation_imports
1313
import 'package:front_end/src/api_unstable/dart2js.dart' as fe;
14+
import 'package:shell_arg_splitter/shell_arg_splitter.dart';
1415

1516
import '../compiler_api.dart' as api;
1617
import 'commandline_options.dart';
@@ -20,7 +21,6 @@ import 'io/mapped_file.dart';
2021
import 'options.dart'
2122
show CompilerOptions, CompilerStage, DumpInfoFormat, FeatureOptions;
2223
import 'source_file_provider.dart';
23-
import 'util/command_line.dart';
2424
import 'util/util.dart' show stackTraceFilePrefix;
2525

2626
const String _defaultSpecificationUri = '../../../../sdk/lib/libraries.json';

pkg/compiler/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies:
2121
kernel: any
2222
meta: any
2323
mmap: any
24+
shell_arg_splitter: any
2425
vm_service: any
2526

2627
# Use 'any' constraints here; we get our versions from the DEPS file.

pkg/dart2wasm/lib/dart2wasm.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ final List<Option> options = [
6868
"watch", (o, values) => o.translatorOptions.watchPoints = values),
6969
StringMultiOption(
7070
"define", (o, values) => o.environment.addAll(processEnvironment(values)),
71-
abbr: "D"),
71+
abbr: "D", splitCommas: false),
7272
StringMultiOption(
7373
"enable-experiment",
7474
(o, values) =>

pkg/dart2wasm/lib/option.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ class MultiValueOption<T> extends Option<List<T>> {
7878
void Function(WasmCompilerOptions o, List<T> v) applyToOptions,
7979
T Function(dynamic v) converter,
8080
{Iterable<String>? defaultsTo,
81-
String? abbr})
81+
String? abbr,
82+
bool splitCommas = true})
8283
: super(
8384
name,
84-
(a) => a.addMultiOption(name, abbr: abbr, defaultsTo: defaultsTo),
85+
(a) => a.addMultiOption(name,
86+
abbr: abbr, defaultsTo: defaultsTo, splitCommas: splitCommas),
8587
applyToOptions,
8688
(vs) => vs.map(converter).cast<T>().toList());
8789
}
@@ -97,9 +99,9 @@ class IntMultiOption extends MultiValueOption<int> {
9799
class StringMultiOption extends MultiValueOption<String> {
98100
StringMultiOption(String name,
99101
void Function(WasmCompilerOptions o, List<String> v) applyToOptions,
100-
{String? abbr, Iterable<String>? defaultsTo})
102+
{String? abbr, Iterable<String>? defaultsTo, bool splitCommas = true})
101103
: super(name, applyToOptions, (v) => v,
102-
abbr: abbr, defaultsTo: defaultsTo);
104+
abbr: abbr, defaultsTo: defaultsTo, splitCommas: splitCommas);
103105
}
104106

105107
class UriMultiOption extends MultiValueOption<Uri> {

pkg/dart2wasm/tool/compile_benchmark

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ while [ $# -gt 0 ]; do
133133
;;
134134

135135
--extra-compiler-option=*)
136-
DART2WASM_ARGS+=(${1#--extra-compiler-option=})
136+
DART2WASM_ARGS+=("${1#--extra-compiler-option=}")
137137
shift
138138
;;
139139

@@ -219,7 +219,7 @@ fi
219219
binaryen_command=("$BINARYEN" "${BINARYEN_FLAGS[@]}" "$WASM_FILE" -o "$WASM_FILE")
220220

221221
if [ -n "$COMPILE_BENCHMARK_BASE_NAME" ]; then
222-
measure ${dart2wasm_command[@]}
222+
measure "${dart2wasm_command[@]}"
223223
COMPILER_TIME=$TIME
224224
COMPILER_MEMORY=$MEMORY
225225

@@ -262,6 +262,6 @@ if [ -n "$COMPILE_BENCHMARK_BASE_NAME" ]; then
262262
echo "$COMPILE_BENCHMARK_BASE_NAME.MemoryUse.Dart2Wasm(MemoryUse): $COMPILER_MEMORY bytes"
263263
run_if_binaryen_enabled echo "$COMPILE_BENCHMARK_BASE_NAME.MemoryUse.Wasm2WasmOpt(MemoryUse): $BINARYEN_MEMORY bytes"
264264
else
265-
${dart2wasm_command[@]}
265+
"${dart2wasm_command[@]}"
266266
run_if_binaryen_enabled ${binaryen_command[@]}
267267
fi

pkg/dartdev/lib/src/commands/compile.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,7 @@ class CompileWasmCommand extends CompileSubcommandCommand {
799799
help: defineOption.help,
800800
abbr: defineOption.abbr,
801801
valueHelp: defineOption.valueHelp,
802+
splitCommas: false,
802803
)
803804
..addExperimentalFlags(verbose: verbose);
804805
}

pkg/dev_compiler/lib/ddc.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import 'dart:io';
1414
import 'dart:isolate';
1515
import 'package:bazel_worker/bazel_worker.dart';
1616
import 'package:kernel/ast.dart' show clearDummyTreeNodesParentPointer;
17+
import 'package:shell_arg_splitter/shell_arg_splitter.dart';
1718

1819
import 'src/command/arguments.dart';
1920
import 'src/command/command.dart';
@@ -129,7 +130,7 @@ class _BatchHelper {
129130

130131
Future<void> _doIteration(ParsedArguments batchArgs, String line) async {
131132
totalTests++;
132-
var args = batchArgs.merge(line.split(RegExp(r'\s+')));
133+
var args = batchArgs.merge(splitLine(line));
133134

134135
String outcome;
135136
try {

pkg/dev_compiler/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dependencies:
1818
js_shared: any
1919
kernel: any
2020
path: any
21+
shell_arg_splitter: any
2122
source_maps: any
2223
source_span: any
2324

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include: package:lints/recommended.yaml

pkg/compiler/lib/src/util/command_line.dart renamed to pkg/shell_arg_splitter/lib/shell_arg_splitter.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
library;
6-
75
/// The accepted escapes in the input of the --batch processor.
86
///
97
/// Contrary to Dart strings it does not contain hex escapes (\u or \x).
10-
const Map<String, String> escapeMapping = {
8+
const Map<String, String> _escapeMapping = {
119
'n': '\n',
1210
'r': '\r',
1311
't': '\t',
@@ -57,7 +55,7 @@ List<String> splitLine(String line, {bool windows = false}) {
5755
i++;
5856

5957
c = line[i];
60-
String mapped = escapeMapping[c] ?? c;
58+
String mapped = _escapeMapping[c] ?? c;
6159
buffer.write(mapped);
6260
continue;
6361
}

0 commit comments

Comments
 (0)