From 0df58fc2d07107846ec5fb28764283b32e6bd1dc Mon Sep 17 00:00:00 2001 From: Michael Thomsen Date: Tue, 5 Dec 2023 15:44:53 +0000 Subject: [PATCH] Add new `dart compile wasm` command. Marked as experimental, and only shown when verbose flag is passed: ``` $ dart help compile -v Compile Dart to various formats. Usage: dart [vm-options] compile [arguments] -h, --help Print this usage information. Available subcommands: aot-snapshot Compile Dart to an AOT snapshot. exe Compile Dart to a self-contained executable. jit-snapshot Compile Dart to a JIT snapshot. js Compile Dart to JavaScript. kernel Compile Dart to a kernel snapshot. wasm Compile Dart to a WebAssembly/WasmGC module (EXPERIMENTAL). ``` Change-Id: I6a0e65d4fbdd7b2782406b8b9969e14036bf0711 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/304860 Reviewed-by: Ben Konyi Commit-Queue: Michael Thomsen Reviewed-by: Aske Simon Christensen --- pkg/dart2wasm/bin/dart2wasm.dart | 6 +- pkg/dart2wasm/lib/compile.dart | 2 +- pkg/dart2wasm/lib/compiler_options.dart | 8 +- pkg/dart2wasm/lib/dart2wasm.dart | 34 +--- pkg/dart2wasm/lib/generate_wasm.dart | 37 ++++ pkg/dart2wasm/lib/option.dart | 33 ++-- pkg/dartdev/lib/src/commands/compile.dart | 197 ++++++++++++++++++++ pkg/dartdev/pubspec.yaml | 1 + pkg/dartdev/test/commands/compile_test.dart | 130 +++++++++---- pkg/dartdev/test/core_test.dart | 9 +- 10 files changed, 371 insertions(+), 86 deletions(-) create mode 100644 pkg/dart2wasm/lib/generate_wasm.dart diff --git a/pkg/dart2wasm/bin/dart2wasm.dart b/pkg/dart2wasm/bin/dart2wasm.dart index 26b4ce86002..130a13584a8 100644 --- a/pkg/dart2wasm/bin/dart2wasm.dart +++ b/pkg/dart2wasm/bin/dart2wasm.dart @@ -2,6 +2,10 @@ // 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. +import 'dart:io'; + import 'package:dart2wasm/dart2wasm.dart' as dart2wasm; -Future main(List args) async => await dart2wasm.main(args); +Future main(List args) async { + exitCode = await dart2wasm.main(args); +} diff --git a/pkg/dart2wasm/lib/compile.dart b/pkg/dart2wasm/lib/compile.dart index ea9c6b62b3e..b4581d25147 100644 --- a/pkg/dart2wasm/lib/compile.dart +++ b/pkg/dart2wasm/lib/compile.dart @@ -58,7 +58,7 @@ class CompilerOutput { /// Returns `null` if an error occurred during compilation. The /// [handleDiagnosticMessage] callback will have received an error message /// describing the error. -Future compileToModule(compiler.CompilerOptions options, +Future compileToModule(compiler.WasmCompilerOptions options, void Function(DiagnosticMessage) handleDiagnosticMessage) async { var succeeded = true; void diagnosticMessageHandler(DiagnosticMessage message) { diff --git a/pkg/dart2wasm/lib/compiler_options.dart b/pkg/dart2wasm/lib/compiler_options.dart index a3eeb0065a0..f7377f7f25c 100644 --- a/pkg/dart2wasm/lib/compiler_options.dart +++ b/pkg/dart2wasm/lib/compiler_options.dart @@ -7,7 +7,7 @@ import 'dart:io'; import 'package:dart2wasm/translator.dart'; import 'package:front_end/src/api_unstable/vm.dart' as fe; -class CompilerOptions { +class WasmCompilerOptions { final TranslatorOptions translatorOptions = TranslatorOptions(); Uri sdkPath = Platform.script.resolve("../../../sdk"); @@ -26,10 +26,10 @@ class CompilerOptions { String? dumpKernelBeforeTfa; String? dumpKernelAfterTfa; - factory CompilerOptions.defaultOptions() => - CompilerOptions(mainUri: Uri(), outputFile: ''); + factory WasmCompilerOptions.defaultOptions() => + WasmCompilerOptions(mainUri: Uri(), outputFile: ''); - CompilerOptions({required this.mainUri, required this.outputFile}); + WasmCompilerOptions({required this.mainUri, required this.outputFile}); void validate() { if (translatorOptions.importSharedMemory && diff --git a/pkg/dart2wasm/lib/dart2wasm.dart b/pkg/dart2wasm/lib/dart2wasm.dart index 33a09903934..9bf0553eb3b 100644 --- a/pkg/dart2wasm/lib/dart2wasm.dart +++ b/pkg/dart2wasm/lib/dart2wasm.dart @@ -5,16 +5,16 @@ import 'dart:io'; import 'package:args/args.dart' as args; -import 'package:front_end/src/api_unstable/vm.dart' - show printDiagnosticMessage, resolveInputUri; +import 'package:front_end/src/api_unstable/vm.dart' show resolveInputUri; import 'package:front_end/src/api_unstable/vm.dart' as fe; -import 'package:dart2wasm/compile.dart'; -import 'package:dart2wasm/compiler_options.dart'; +import 'package:dart2wasm/generate_wasm.dart'; import 'package:dart2wasm/option.dart'; // Used to allow us to keep defaults on their respective option structs. -final CompilerOptions _d = CompilerOptions.defaultOptions(); +// Note: When adding new options, consider if CLI options should be add +// to `pkg/dartdev/lib/src/commands/compile.dart` too. +final WasmCompilerOptions _d = WasmCompilerOptions.defaultOptions(); final List