Files
sdk/pkg/shell_arg_splitter/lib/shell_arg_splitter.dart
Ömer Ağacan f4b41f0902 [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 https://github.com/flutter/flutter/issues/164873.

See also https://github.com/dart-lang/sdk/issues/60341 for relevant
future work.

Change-Id: Idbdf69072fa212c8e4a390990577eb5a57b49e8a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/415280
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
2025-03-18 04:11:41 -07:00

76 lines
2.0 KiB
Dart

// Copyright (c) 2014, 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.
/// The accepted escapes in the input of the --batch processor.
///
/// Contrary to Dart strings it does not contain hex escapes (\u or \x).
const Map<String, String> _escapeMapping = {
'n': '\n',
'r': '\r',
't': '\t',
'b': '\b',
'f': '\f',
'v': '\v',
'\\': '\\',
};
/// Splits the [line] similar to how a shell would split arguments. If [windows]
/// is `true` escapes will be handled like on the Windows command-line.
///
/// Example:
///
/// splitLine("""--args "ab"c 'with " \'spaces'""").forEach(print);
/// // --args
/// // abc
/// // with " 'spaces
List<String> splitLine(String line, {bool windows = false}) {
List<String> result = [];
bool inQuotes = false;
String? openingQuote;
StringBuffer buffer = StringBuffer();
for (int i = 0; i < line.length; i++) {
String c = line[i];
if (inQuotes && c == openingQuote) {
inQuotes = false;
continue;
}
if (!inQuotes && (c == '"' || (c == "'" && !windows))) {
inQuotes = true;
openingQuote = c;
continue;
}
if (c == '\\') {
if (i == line.length - 1) {
throw FormatException('Unfinished escape: $line');
}
if (windows) {
String next = line[i + 1];
if (next == '"' || next == r'\') {
buffer.write(next);
i++;
continue;
}
} else {
i++;
c = line[i];
String mapped = _escapeMapping[c] ?? c;
buffer.write(mapped);
continue;
}
}
if (!inQuotes && c == ' ') {
if (buffer.isNotEmpty) {
result.add(buffer.toString());
buffer.clear();
}
continue;
}
buffer.write(c);
}
if (inQuotes) throw FormatException('Unclosed quotes: $line');
if (buffer.isNotEmpty) result.add(buffer.toString());
return result;
}