Skip to content

Commit 0b38e4b

Browse files
a-sivaCommit Queue
authored andcommitted
[dartdev] Add 'compile js-dev' command in the Dart cli tool.
Added the option 'js-dev' to the compile command to allow compilation of Dart sources using DDC. This ensures that tools will not be reaching into the internal implementation details of a Dart SDK to run DDC. TEST=unit tests added in compile_test.dart Change-Id: I3c670b43a989dfa60c75622fdbc1b7f99b399293 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/392202 Reviewed-by: Ben Konyi <[email protected]> Commit-Queue: Siva Annamalai <[email protected]>
1 parent 3488fd5 commit 0b38e4b

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,58 @@ class CompileJSCommand extends CompileSubcommandCommand {
125125
}
126126
}
127127

128+
class CompileDDCCommand extends CompileSubcommandCommand {
129+
static const String cmdName = 'js-dev';
130+
131+
/// Accept all flags so we can delegate arg parsing to ddc internally.
132+
@override
133+
final ArgParser argParser = ArgParser.allowAnything();
134+
135+
// This command is an internal developer command used by tools and is
136+
// hidden in the help message.
137+
CompileDDCCommand({bool verbose = false})
138+
: super(cmdName, 'Compile Dart to JavaScript using ddc.', verbose,
139+
hidden:true,);
140+
141+
@override
142+
String get invocation => '${super.invocation} <dart entry point>';
143+
144+
@override
145+
FutureOr<int> run() async {
146+
if (!Sdk.checkArtifactExists(sdk.librariesJson)) {
147+
return genericErrorExitCode;
148+
}
149+
final args = argResults!;
150+
var snapshot = sdk.ddcAotSnapshot;
151+
var runtime = sdk.dartAotRuntime;
152+
if (!Sdk.checkArtifactExists(snapshot, logError: false)) {
153+
// AOT snapshots cannot be generated on IA32, so we need this fallback
154+
// branch until support for IA32 is dropped (https://dartbug.com/49969).
155+
snapshot = sdk.ddcSnapshot;
156+
runtime = sdk.dart;
157+
if (!Sdk.checkArtifactExists(snapshot)) {
158+
return genericErrorExitCode;
159+
}
160+
}
161+
final ddcCommand = <String>[
162+
runtime,
163+
snapshot,
164+
// Add the remaining arguments.
165+
if (args.rest.isNotEmpty) ...args.rest.sublist(0),
166+
];
167+
try {
168+
return await runProcessInheritStdio(ddcCommand);
169+
} catch (e, st) {
170+
log.stderr('Error: JS compilation failed');
171+
log.stderr(e.toString());
172+
if (verbose) {
173+
log.stderr(st.toString());
174+
}
175+
return compileErrorExitCode;
176+
}
177+
}
178+
}
179+
128180
class CompileKernelSnapshotCommand extends CompileSubcommandCommand {
129181
static const commandName = 'kernel';
130182
static const help = 'Compile Dart to a kernel snapshot.\n'
@@ -947,6 +999,7 @@ class CompileCommand extends DartdevCommand {
947999
bool nativeAssetsExperimentEnabled = false,
9481000
}) : super(cmdName, 'Compile Dart to various formats.', verbose) {
9491001
addSubcommand(CompileJSCommand(verbose: verbose));
1002+
addSubcommand(CompileDDCCommand(verbose: verbose));
9501003
addSubcommand(CompileJitSnapshotCommand(verbose: verbose));
9511004
addSubcommand(CompileKernelSnapshotCommand(verbose: verbose));
9521005
addSubcommand(CompileNativeCommand(

pkg/dartdev/lib/src/sdk.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ class Sdk {
6767
'analysis_server.dart.snapshot',
6868
);
6969

70+
String get ddcSnapshot => _snapshotPathFor(
71+
'dartdevc.dart.snapshot',
72+
);
73+
74+
String get ddcAotSnapshot => _snapshotPathFor(
75+
'dartdevc_aot.dart.snapshot',
76+
);
77+
7078
String get dart2jsSnapshot => _snapshotPathFor(
7179
'dart2js.dart.snapshot',
7280
);

pkg/dartdev/test/commands/compile_test.dart

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ void defineCompileTests() {
179179
],
180180
);
181181
expect(result.stderr, contains('Compile Dart'));
182+
expect(result.stderr, isNot(contains('js-dev')));
182183
expect(result.exitCode, 64);
183184
});
184185

@@ -198,6 +199,7 @@ void defineCompileTests() {
198199
expect(result.stdout, contains('jit-snapshot'));
199200
expect(result.stdout, contains('kernel'));
200201
expect(result.stdout, contains('js'));
202+
expect(result.stderr, isNot(contains('js-dev')));
201203
expect(result.stdout, contains('aot-snapshot'));
202204
expect(result.stdout, contains('exe'));
203205
expect(result.exitCode, 0);
@@ -209,6 +211,7 @@ void defineCompileTests() {
209211
['compile', '--help', '--verbose'],
210212
);
211213
expect(result.stdout, contains('Compile Dart'));
214+
expect(result.stdout, isNot(contains('js-dev')));
212215
expect(
213216
result.stdout,
214217
contains(
@@ -602,6 +605,35 @@ void defineCompileTests() {
602605
expect(contents.contains('2: foo'), true);
603606
});
604607

608+
test('Compile JS DDC', () async {
609+
final p = project(mainSrc: '''
610+
void main() {
611+
print('1: ' + const String.fromEnvironment('foo'));
612+
print('2: ' + const String.fromEnvironment('bar'));
613+
}''');
614+
final inFile = path.canonicalize(path.join(p.dirPath, p.relativeFilePath));
615+
final outFile = path.canonicalize(path.join(p.dirPath, 'main.js'));
616+
617+
final result = await p.run([
618+
'compile',
619+
'js-dev',
620+
'-Dfoo=bar',
621+
'--define=bar=foo',
622+
'-o',
623+
outFile,
624+
inFile,
625+
]);
626+
expect(result.stdout, isNot(contains(usingTargetOSMessage)));
627+
expect(result.stderr, isEmpty);
628+
expect(result.exitCode, 0);
629+
final file = File(outFile);
630+
expect(file.existsSync(), true, reason: 'File not found: $outFile');
631+
632+
final contents = file.readAsStringSync();
633+
expect(contents.contains('"1: " + "bar"'), true);
634+
expect(contents.contains('"2: " + "foo"'), true);
635+
});
636+
605637
test('Compile exe with error', () async {
606638
final p = project(mainSrc: '''
607639
void main() {
@@ -939,6 +971,28 @@ void main() {
939971
reason: 'File not found: $outFile');
940972
}, skip: isRunningOnIA32);
941973

974+
test('Compile JS DDC with sound null safety', () async {
975+
final p = project(mainSrc: '''void main() {}''');
976+
final inFile = path.canonicalize(path.join(p.dirPath, p.relativeFilePath));
977+
final outFile = path.canonicalize(path.join(p.dirPath, 'myjs'));
978+
979+
final result = await p.run(
980+
[
981+
'compile',
982+
'js-dev',
983+
'-o',
984+
outFile,
985+
inFile,
986+
],
987+
);
988+
989+
expect(result.stdout, isNot(contains(soundNullSafetyMessage)));
990+
expect(result.stderr, isEmpty);
991+
expect(result.exitCode, 0);
992+
expect(File(outFile).existsSync(), true,
993+
reason: 'File not found: $outFile');
994+
}, skip: isRunningOnIA32);
995+
942996
test('Compile JS with unsound null safety', () async {
943997
final p = project(mainSrc: '''
944998
// @dart=2.9

pkg/dartdev/test/core_test.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void _dartdevCommand() {
4141
});
4242

4343
test('compile', () {
44-
assertDartdevCommandProperties(CompileCommand(), 'compile', 'compile', 6);
44+
assertDartdevCommandProperties(CompileCommand(), 'compile', 'compile', 7);
4545
});
4646

4747
test('compile/js', () {
@@ -51,6 +51,13 @@ void _dartdevCommand() {
5151
'compile/js');
5252
});
5353

54+
test('compile/js-dev', () {
55+
assertDartdevCommandProperties(
56+
CompileCommand().subcommands['js-dev'] as DartdevCommand,
57+
'js-dev',
58+
'compile/js-dev');
59+
});
60+
5461
test('compile/jit-snapshot', () {
5562
assertDartdevCommandProperties(
5663
CompileCommand().subcommands['jit-snapshot'] as DartdevCommand,

0 commit comments

Comments
 (0)