Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f199c32

Browse files
author
Dart CI
committed
Version 2.19.0-33.0.dev
Merge commit 'c8728d92e3abc5f06e8c4e744f4671611511669c' into 'dev'
2 parents be3fb94 + c8728d9 commit f199c32

File tree

6 files changed

+209
-96
lines changed

6 files changed

+209
-96
lines changed

pkg/dart2wasm/bin/dart2wasm.dart

Lines changed: 91 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -5,110 +5,117 @@
55
import 'dart:io';
66
import 'dart:typed_data';
77

8+
import 'package:args/args.dart' as args;
89
import 'package:front_end/src/api_unstable/vm.dart'
910
show printDiagnosticMessage, resolveInputUri;
1011

1112
import 'package:dart2wasm/compile.dart';
12-
import 'package:dart2wasm/translator.dart';
13+
import 'package:dart2wasm/compiler_options.dart';
14+
import 'package:dart2wasm/option.dart';
1315

14-
final Map<String, void Function(TranslatorOptions, bool)> boolOptionMap = {
15-
"export-all": (o, value) => o.exportAll = value,
16-
"import-shared-memory": (o, value) => o.importSharedMemory = value,
17-
"inlining": (o, value) => o.inlining = value,
18-
"lazy-constants": (o, value) => o.lazyConstants = value,
19-
"local-nullability": (o, value) => o.localNullability = value,
20-
"name-section": (o, value) => o.nameSection = value,
21-
"nominal-types": (o, value) => o.nominalTypes = value,
22-
"parameter-nullability": (o, value) => o.parameterNullability = value,
23-
"polymorphic-specialization": (o, value) =>
24-
o.polymorphicSpecialization = value,
25-
"print-kernel": (o, value) => o.printKernel = value,
26-
"print-wasm": (o, value) => o.printWasm = value,
27-
"runtime-types": (o, value) => o.runtimeTypes = value,
28-
"string-data-segments": (o, value) => o.stringDataSegments = value,
29-
};
30-
final Map<String, void Function(TranslatorOptions, int)> intOptionMap = {
31-
"shared-memory-max-pages": (o, value) => o.sharedMemoryMaxPages = value,
32-
"watch": (o, value) => (o.watchPoints ??= []).add(value),
33-
};
16+
// Used to allow us to keep defaults on their respective option structs.
17+
final CompilerOptions _d = CompilerOptions.defaultOptions();
3418

35-
Never usage(String message) {
36-
print("Usage: dart2wasm [<options>] <infile.dart> <outfile.wasm>");
37-
print("");
38-
print("*NOTE*: Wasm compilation is experimental.");
39-
print("The support may change, or be removed, with no advance notice.");
40-
print("");
41-
print("Options:");
42-
print(" --dart-sdk=<path>");
43-
print(" --platform=<path>");
44-
print("");
45-
for (String option in boolOptionMap.keys) {
46-
print(" --[no-]$option");
47-
}
48-
print("");
49-
for (String option in intOptionMap.keys) {
50-
print(" --$option <value>");
51-
}
52-
print("");
19+
final List<Option> options = [
20+
Flag("help", (o, _) {}, abbr: "h", negatable: false, defaultsTo: false),
21+
Flag("export-all", (o, value) => o.translatorOptions.exportAll = value,
22+
defaultsTo: _d.translatorOptions.exportAll),
23+
Flag("import-shared-memory",
24+
(o, value) => o.translatorOptions.importSharedMemory = value,
25+
defaultsTo: _d.translatorOptions.importSharedMemory),
26+
Flag("inlining", (o, value) => o.translatorOptions.inlining = value,
27+
defaultsTo: _d.translatorOptions.inlining),
28+
Flag(
29+
"lazy-constants", (o, value) => o.translatorOptions.lazyConstants = value,
30+
defaultsTo: _d.translatorOptions.lazyConstants),
31+
Flag("local-nullability",
32+
(o, value) => o.translatorOptions.localNullability = value,
33+
defaultsTo: _d.translatorOptions.localNullability),
34+
Flag("name-section", (o, value) => o.translatorOptions.nameSection = value,
35+
defaultsTo: _d.translatorOptions.nameSection),
36+
Flag("nominal-types", (o, value) => o.translatorOptions.nominalTypes = value,
37+
defaultsTo: _d.translatorOptions.nominalTypes),
38+
Flag("parameter-nullability",
39+
(o, value) => o.translatorOptions.parameterNullability = value,
40+
defaultsTo: _d.translatorOptions.parameterNullability),
41+
Flag("polymorphic-specialization",
42+
(o, value) => o.translatorOptions.polymorphicSpecialization = value,
43+
defaultsTo: _d.translatorOptions.polymorphicSpecialization),
44+
Flag("print-kernel", (o, value) => o.translatorOptions.printKernel = value,
45+
defaultsTo: _d.translatorOptions.printKernel),
46+
Flag("print-wasm", (o, value) => o.translatorOptions.printWasm = value,
47+
defaultsTo: _d.translatorOptions.printWasm),
48+
Flag("runtime-types", (o, value) => o.translatorOptions.runtimeTypes = value,
49+
defaultsTo: _d.translatorOptions.runtimeTypes),
50+
Flag("string-data-segments",
51+
(o, value) => o.translatorOptions.stringDataSegments = value,
52+
defaultsTo: _d.translatorOptions.stringDataSegments),
53+
IntOption(
54+
"inlining-limit", (o, value) => o.translatorOptions.inliningLimit = value,
55+
defaultsTo: "${_d.translatorOptions.inliningLimit}"),
56+
IntOption("shared-memory-max-pages",
57+
(o, value) => o.translatorOptions.sharedMemoryMaxPages = value),
58+
UriOption("dart-sdk", (o, value) => o.sdkPath = value,
59+
defaultsTo: "${_d.sdkPath}"),
60+
UriOption("platform", (o, value) => o.platformPath = value),
61+
IntMultiOption(
62+
"watch", (o, values) => o.translatorOptions.watchPoints = values),
63+
];
5364

54-
throw message;
55-
}
65+
CompilerOptions parseArguments(List<String> arguments) {
66+
args.ArgParser parser = args.ArgParser();
67+
for (Option arg in options) {
68+
arg.applyToParser(parser);
69+
}
5670

57-
Future<int> main(List<String> args) async {
58-
Uri sdkPath = Platform.script.resolve("../../../sdk");
59-
Uri? platformPath = null;
60-
TranslatorOptions options = TranslatorOptions();
61-
List<String> nonOptions = [];
62-
void Function(TranslatorOptions, int)? intOptionFun = null;
63-
for (String arg in args) {
64-
if (intOptionFun != null) {
65-
intOptionFun(options, int.parse(arg));
66-
intOptionFun = null;
67-
} else if (arg.startsWith("--dart-sdk=")) {
68-
String path = arg.substring("--dart-sdk=".length);
69-
sdkPath = Uri.file(Directory(path).absolute.path);
70-
} else if (arg.startsWith("--platform=")) {
71-
String path = arg.substring("--platform=".length);
72-
platformPath = Uri.file(Directory(path).absolute.path);
73-
} else if (arg.startsWith("--no-")) {
74-
var optionFun = boolOptionMap[arg.substring(5)];
75-
if (optionFun == null) usage("Unknown option $arg");
76-
optionFun(options, false);
77-
} else if (arg.startsWith("--")) {
78-
var optionFun = boolOptionMap[arg.substring(2)];
79-
if (optionFun != null) {
80-
optionFun(options, true);
81-
} else {
82-
intOptionFun = intOptionMap[arg.substring(2)];
83-
if (intOptionFun == null) usage("Unknown option $arg");
84-
}
85-
} else {
86-
nonOptions.add(arg);
71+
Never usage() {
72+
print("Usage: dart2wasm [<options>] <infile.dart> <outfile.wasm>");
73+
print("");
74+
print("*NOTE*: Wasm compilation is experimental.");
75+
print("The support may change, or be removed, with no advance notice.");
76+
print("");
77+
print("Options:");
78+
for (String line in parser.usage.split('\n')) {
79+
print('\t$line');
8780
}
88-
}
89-
if (intOptionFun != null) {
90-
usage("Missing argument to ${args.last}");
81+
exit(64);
9182
}
9283

93-
if (options.importSharedMemory && options.sharedMemoryMaxPages == null) {
94-
usage("--shared-memory-max-pages must be "
95-
"specified if --import-shared-memory is used.");
84+
try {
85+
args.ArgResults results = parser.parse(arguments);
86+
if (results['help']) {
87+
usage();
88+
}
89+
List<String> rest = results.rest;
90+
if (rest.length != 2) {
91+
throw ArgumentError('Requires two positional file arguments');
92+
}
93+
CompilerOptions compilerOptions =
94+
CompilerOptions(mainUri: resolveInputUri(rest[0]), outputFile: rest[1]);
95+
for (Option arg in options) {
96+
if (results.wasParsed(arg.name)) {
97+
arg.applyToOptions(compilerOptions, results[arg.name]);
98+
}
99+
}
100+
return compilerOptions;
101+
} catch (e, s) {
102+
print(s);
103+
print('Argument Error: ' + e.toString());
104+
usage();
96105
}
106+
}
97107

98-
if (nonOptions.length != 2) usage("Requires two file arguments");
99-
String input = nonOptions[0];
100-
String output = nonOptions[1];
101-
Uri mainUri = resolveInputUri(input);
102-
103-
Uint8List? module = await compileToModule(mainUri, sdkPath, platformPath,
108+
Future<int> main(List<String> args) async {
109+
CompilerOptions options = parseArguments(args);
110+
Uint8List? module = await compileToModule(
104111
options, (message) => printDiagnosticMessage(message, print));
105112

106113
if (module == null) {
107114
exitCode = 1;
108115
return exitCode;
109116
}
110117

111-
await File(output).writeAsBytes(module);
118+
await File(options.outputFile).writeAsBytes(module);
112119

113120
return 0;
114121
}

pkg/dart2wasm/lib/compile.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import 'package:kernel/verifier.dart';
2222
import 'package:vm/transformations/type_flow/transformer.dart' as globalTypeFlow
2323
show transformComponent;
2424

25+
import 'package:dart2wasm/compiler_options.dart' as compiler;
2526
import 'package:dart2wasm/target.dart';
2627
import 'package:dart2wasm/translator.dart';
2728

@@ -30,11 +31,7 @@ import 'package:dart2wasm/translator.dart';
3031
/// Returns `null` if an error occurred during compilation. The
3132
/// [handleDiagnosticMessage] callback will have received an error message
3233
/// describing the error.
33-
Future<Uint8List?> compileToModule(
34-
Uri mainUri,
35-
Uri sdkRoot,
36-
Uri? platformDill,
37-
TranslatorOptions options,
34+
Future<Uint8List?> compileToModule(compiler.CompilerOptions options,
3835
void Function(DiagnosticMessage) handleDiagnosticMessage) async {
3936
var succeeded = true;
4037
void diagnosticMessageHandler(DiagnosticMessage message) {
@@ -47,20 +44,20 @@ Future<Uint8List?> compileToModule(
4744
Target target = WasmTarget();
4845
CompilerOptions compilerOptions = CompilerOptions()
4946
..target = target
50-
..sdkRoot = sdkRoot
47+
..sdkRoot = options.sdkPath
5148
..environmentDefines = {}
5249
..verbose = false
5350
..onDiagnostic = diagnosticMessageHandler
5451
..nnbdMode = NnbdMode.Strong;
5552

56-
if (platformDill != null) {
57-
compilerOptions.sdkSummary = platformDill;
53+
if (options.platformPath != null) {
54+
compilerOptions.sdkSummary = options.platformPath;
5855
} else {
5956
compilerOptions.compileSdk = true;
6057
}
6158

6259
CompilerResult? compilerResult =
63-
await kernelForProgram(mainUri, compilerOptions);
60+
await kernelForProgram(options.mainUri, compilerOptions);
6461
if (compilerResult == null || !succeeded) {
6562
return null;
6663
}
@@ -78,7 +75,10 @@ Future<Uint8List?> compileToModule(
7875
return true;
7976
}());
8077

81-
var translator = Translator(component, coreTypes,
82-
TypeEnvironment(coreTypes, compilerResult.classHierarchy!), options);
78+
var translator = Translator(
79+
component,
80+
coreTypes,
81+
TypeEnvironment(coreTypes, compilerResult.classHierarchy!),
82+
options.translatorOptions);
8383
return translator.translate();
8484
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
import 'dart:io';
6+
7+
import 'package:dart2wasm/translator.dart';
8+
9+
class CompilerOptions {
10+
final TranslatorOptions translatorOptions = TranslatorOptions();
11+
12+
Uri sdkPath = Platform.script.resolve("../../../sdk");
13+
Uri? platformPath;
14+
Uri mainUri;
15+
String outputFile;
16+
17+
factory CompilerOptions.defaultOptions() =>
18+
CompilerOptions(mainUri: Uri(), outputFile: '');
19+
20+
CompilerOptions({required this.mainUri, required this.outputFile});
21+
22+
void validate() {
23+
if (translatorOptions.importSharedMemory &&
24+
translatorOptions.sharedMemoryMaxPages == null) {
25+
throw ArgumentError("--shared-memory-max-pages must be specified if "
26+
"--import-shared-memory is used.");
27+
}
28+
}
29+
}

pkg/dart2wasm/lib/option.dart

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
import 'dart:io';
6+
7+
import 'package:args/args.dart';
8+
9+
import 'package:dart2wasm/compiler_options.dart';
10+
11+
class Option<T> {
12+
final String name;
13+
final void Function(ArgParser a) applyToParser;
14+
final void Function(CompilerOptions o, T v) _applyToOptions;
15+
final T Function(dynamic v) converter;
16+
17+
void applyToOptions(CompilerOptions o, dynamic v) =>
18+
_applyToOptions(o, converter(v));
19+
20+
Option(this.name, this.applyToParser, this._applyToOptions, this.converter);
21+
}
22+
23+
class Flag extends Option<bool> {
24+
Flag(String name, void applyToOptions(CompilerOptions o, bool v),
25+
{String? abbr,
26+
String? help,
27+
bool? defaultsTo = false,
28+
bool negatable = true})
29+
: super(
30+
name,
31+
(a) => a.addFlag(name,
32+
abbr: abbr,
33+
help: help,
34+
defaultsTo: defaultsTo,
35+
negatable: negatable),
36+
applyToOptions,
37+
(v) => v == 'true' ? true : false);
38+
}
39+
40+
class ValueOption<T> extends Option<T> {
41+
ValueOption(String name, void applyToOptions(CompilerOptions o, T v),
42+
T converter(dynamic v), {String? defaultsTo})
43+
: super(name, (a) => a.addOption(name, defaultsTo: defaultsTo),
44+
applyToOptions, converter);
45+
}
46+
47+
class IntOption extends ValueOption<int> {
48+
IntOption(String name, void applyToOptions(CompilerOptions o, int v),
49+
{String? defaultsTo})
50+
: super(name, applyToOptions, (v) => int.parse(v),
51+
defaultsTo: defaultsTo);
52+
}
53+
54+
class UriOption extends ValueOption<Uri> {
55+
UriOption(String name, void applyToOptions(CompilerOptions o, Uri v),
56+
{String? defaultsTo})
57+
: super(name, applyToOptions, (v) => Uri.file(Directory(v).absolute.path),
58+
defaultsTo: defaultsTo);
59+
}
60+
61+
class MultiValueOption<T> extends Option<List<T>> {
62+
MultiValueOption(
63+
String name,
64+
void Function(CompilerOptions o, List<T> v) applyToOptions,
65+
T converter(dynamic v),
66+
{Iterable<String>? defaultsTo})
67+
: super(name, (a) => a.addMultiOption(name, defaultsTo: defaultsTo),
68+
applyToOptions, (vs) => vs.map(converter).cast<T>().toList());
69+
}
70+
71+
class IntMultiOption extends MultiValueOption<int> {
72+
IntMultiOption(name, void applyToOptions(CompilerOptions o, List<int> v),
73+
{Iterable<String>? defaultsTo})
74+
: super(name, applyToOptions, (v) => int.parse(v),
75+
defaultsTo: defaultsTo);
76+
}

pkg/dart2wasm/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ environment:
77
# Use 'any' constraints here; we get our versions from the DEPS file.
88
dependencies:
99
_js_interop_checks: any
10+
args: any
1011
front_end: any
1112
kernel: any
1213
vm: any

tools/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ CHANNEL dev
2727
MAJOR 2
2828
MINOR 19
2929
PATCH 0
30-
PRERELEASE 32
30+
PRERELEASE 33
3131
PRERELEASE_PATCH 0

0 commit comments

Comments
 (0)