Skip to content

Commit c909e16

Browse files
Anna Gringauzecommit-bot@chromium.org
Anna Gringauze
authored andcommitted
Added module information to metadata and use by frontend_sever
- created class representing module metadata, added metadata to JsCode class - added module information to metadata, such as module name, load function name - added import and file uris for libraries - added experimental-emit-debug-metadata flag to frontend_server - added frontend server tests to check for saved metadata Related: #41852 Closes: #40774 Change-Id: Iecbbf1e4eea1919e01f002f45363d30707cb1590 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/150181 Commit-Queue: Anna Gringauze <[email protected]> Reviewed-by: Nicholas Shahan <[email protected]> Reviewed-by: Gary Roumanis <[email protected]>
1 parent 40b9545 commit c909e16

10 files changed

+568
-49
lines changed

pkg/dev_compiler/lib/src/compiler/module_builder.dart

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class DdcModuleBuilder extends _ModuleBuilder {
191191
}
192192

193193
var resultModule = NamedFunction(
194-
loadFunctionName(module.name),
194+
loadFunctionIdentifier(module.name),
195195
js.fun("function(#) { 'use strict'; #; }", [parameters, statements]),
196196
true);
197197

@@ -299,7 +299,7 @@ class AmdModuleBuilder extends _ModuleBuilder {
299299
statements.add(Return(ObjectInitializer(exportedProps, multiline: true)));
300300
}
301301
var resultModule = NamedFunction(
302-
loadFunctionName(module.name),
302+
loadFunctionIdentifier(module.name),
303303
js.fun("function(#) { 'use strict'; #; }", [fnParams, statements]),
304304
true);
305305
var block = js.statement(
@@ -309,6 +309,17 @@ class AmdModuleBuilder extends _ModuleBuilder {
309309
}
310310
}
311311

312+
bool isSdkInternalRuntimeUri(Uri importUri) {
313+
return importUri.scheme == 'dart' && importUri.path == '_runtime';
314+
}
315+
316+
String libraryUriToJsIdentifier(Uri importUri) {
317+
if (importUri.scheme == 'dart') {
318+
return isSdkInternalRuntimeUri(importUri) ? 'dart' : importUri.path;
319+
}
320+
return pathToJSIdentifier(p.withoutExtension(importUri.pathSegments.last));
321+
}
322+
312323
/// Converts an entire arbitrary path string into a string compatible with
313324
/// JS identifier naming rules while conserving path information.
314325
///
@@ -327,9 +338,13 @@ String pathToJSIdentifier(String path) {
327338
.replaceAll('-', '_'));
328339
}
329340

341+
/// Creates function name given [moduleName].
342+
String loadFunctionName(String moduleName) =>
343+
'load__' + pathToJSIdentifier(moduleName.replaceAll('.', '_'));
344+
330345
/// Creates function name identifier given [moduleName].
331-
Identifier loadFunctionName(String moduleName) =>
332-
Identifier('load__' + pathToJSIdentifier(moduleName.replaceAll('.', '_')));
346+
Identifier loadFunctionIdentifier(String moduleName) =>
347+
Identifier(loadFunctionName(moduleName));
333348

334349
// Replacement string for path separators (i.e., '/', '\', '..').
335350
final encodedSeparator = '__';

pkg/dev_compiler/lib/src/compiler/shared_command.dart

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ class SharedCompilerOptions {
6161
/// runtime can enable synchronous stack trace deobsfuscation.
6262
final bool inlineSourceMap;
6363

64+
/// Whether to emit the debug metadata
65+
///
66+
/// Debugger uses this information about to construct mapping between
67+
/// modules and libraries that otherwise requires expensive communication with
68+
/// the browser.
69+
final bool emitDebugMetadata;
70+
6471
/// Whether to emit a summary file containing API signatures.
6572
///
6673
/// This is required for a modular build process.
@@ -99,6 +106,7 @@ class SharedCompilerOptions {
99106
this.summarizeApi = true,
100107
this.enableAsserts = true,
101108
this.replCompile = false,
109+
this.emitDebugMetadata = false,
102110
this.summaryModules = const {},
103111
this.moduleFormats = const [],
104112
this.experiments = const {},
@@ -119,7 +127,9 @@ class SharedCompilerOptions {
119127
moduleFormats: parseModuleFormatOption(args),
120128
moduleName: _getModuleName(args, moduleRoot),
121129
replCompile: args['repl-compile'] as bool,
122-
soundNullSafety: args['sound-null-safety'] as bool);
130+
soundNullSafety: args['sound-null-safety'] as bool,
131+
emitDebugMetadata:
132+
args['experimental-emit-debug-metadata'] as bool);
123133

124134
static void addArguments(ArgParser parser, {bool hide = true}) {
125135
addModuleFormatOptions(parser, hide: hide);
@@ -152,7 +162,14 @@ class SharedCompilerOptions {
152162
..addFlag('sound-null-safety',
153163
help: 'Compile for sound null safety at runtime.',
154164
negatable: true,
155-
defaultsTo: false);
165+
defaultsTo: false)
166+
// TODO(41852) Define a process for breaking changes before graduating from
167+
// experimental.
168+
..addFlag('experimental-emit-debug-metadata',
169+
help: 'Experimental option for compiler development.\n'
170+
'Output a metadata file for debug tools next to the .js output.',
171+
defaultsTo: false,
172+
hide: true);
156173
}
157174

158175
static String _getModuleName(ArgResults args, String moduleRoot) {

pkg/dev_compiler/lib/src/kernel/command.dart

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import '../js_ast/js_ast.dart' as js_ast;
2727
import '../js_ast/js_ast.dart' show js;
2828
import '../js_ast/source_map_printer.dart' show SourceMapPrintingContext;
2929
import 'compiler.dart';
30+
import 'module_metadata.dart';
3031
import 'target.dart';
3132

3233
const _binaryName = 'dartdevc -k';
@@ -109,13 +110,6 @@ Future<CompilerResult> _compile(List<String> args,
109110
help: 'The path to the libraries.json file for the sdk.')
110111
..addOption('used-inputs-file',
111112
help: 'If set, the file to record inputs used.', hide: true)
112-
// TODO(41852) Define a process for breaking changes before graduating from
113-
// experimental.
114-
..addFlag('experimental-emit-debug-metadata',
115-
help: 'Experimental option for compiler development.\n'
116-
'Output a metadata file for debug tools next to the .js output.',
117-
defaultsTo: false,
118-
hide: true)
119113
..addFlag('kernel',
120114
abbr: 'k',
121115
help: 'Deprecated and ignored. To be removed in a future release.',
@@ -425,6 +419,7 @@ Future<CompilerResult> _compile(List<String> args,
425419
var jsCode = jsProgramToCode(jsModule, moduleFormat,
426420
buildSourceMap: options.sourceMap,
427421
inlineSourceMap: options.inlineSourceMap,
422+
emitDebugMetadata: options.emitDebugMetadata,
428423
jsUrl: p.toUri(output).toString(),
429424
mapUrl: mapUrl,
430425
customScheme: multiRootScheme,
@@ -436,21 +431,9 @@ Future<CompilerResult> _compile(List<String> args,
436431
outFiles.add(
437432
File('$output.map').writeAsString(json.encode(jsCode.sourceMap)));
438433
}
439-
440-
if (argResults['experimental-emit-debug-metadata'] as bool) {
441-
var moduleMetadata = [
442-
for (var lib in compiledLibraries.libraries)
443-
{
444-
'name': compiler.jsLibraryName(lib),
445-
'sourceMapFileUri': mapUrl,
446-
'dartFileUris': [
447-
lib.fileUri.toString(),
448-
...lib.parts.map((p) => p.partUri.toString())
449-
],
450-
}
451-
];
434+
if (jsCode.metadata != null) {
452435
outFiles.add(
453-
File('$output.metadata').writeAsString(json.encode(moduleMetadata)));
436+
File('$output.metadata').writeAsString(json.encode(jsCode.metadata)));
454437
}
455438
}
456439

@@ -598,7 +581,14 @@ class JSCode {
598581
/// using [placeSourceMap].
599582
final Map sourceMap;
600583

601-
JSCode(this.code, this.sourceMap);
584+
/// Module and library information
585+
///
586+
/// The [metadata] is a contract between compiler and the debugger,
587+
/// helping the debugger map between libraries, modules, source paths.
588+
/// see: https://goto.google.com/dart-web-debugger-metadata
589+
final ModuleMetadata metadata;
590+
591+
JSCode(this.code, this.sourceMap, {this.metadata});
602592
}
603593

604594
/// Converts [moduleTree] to [JSCode], using [format].
@@ -608,6 +598,7 @@ class JSCode {
608598
JSCode jsProgramToCode(js_ast.Program moduleTree, ModuleFormat format,
609599
{bool buildSourceMap = false,
610600
bool inlineSourceMap = false,
601+
bool emitDebugMetadata = false,
611602
String jsUrl,
612603
String mapUrl,
613604
String sourceMapBase,
@@ -666,7 +657,27 @@ JSCode jsProgramToCode(js_ast.Program moduleTree, ModuleFormat format,
666657
};
667658
text = text.replaceFirst(
668659
SharedCompiler.metricsLocationID, '$compileTimeStatistics');
669-
return JSCode(text, builtMap);
660+
661+
var debugMetadata = emitDebugMetadata
662+
? _emitMetadata(moduleTree, component, mapUrl, jsUrl)
663+
: null;
664+
665+
return JSCode(text, builtMap, metadata: debugMetadata);
666+
}
667+
668+
ModuleMetadata _emitMetadata(js_ast.Program program, Component component,
669+
String sourceMapUri, String moduleUri) {
670+
var metadata = ModuleMetadata(
671+
program.name, loadFunctionName(program.name), sourceMapUri, moduleUri);
672+
673+
for (var lib in component.libraries) {
674+
metadata.addLibrary(LibraryMetadata(
675+
libraryUriToJsIdentifier(lib.importUri),
676+
lib.importUri.toString(),
677+
lib.fileUri.toString(),
678+
[...lib.parts.map((p) => p.partUri)]));
679+
}
680+
return metadata;
670681
}
671682

672683
/// Parses Dart's non-standard `-Dname=value` syntax for declared variables,

pkg/dev_compiler/lib/src/kernel/compiler.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import 'package:path/path.dart' as p;
1717

1818
import '../compiler/js_names.dart' as js_ast;
1919
import '../compiler/js_utils.dart' as js_ast;
20-
import '../compiler/module_builder.dart' show pathToJSIdentifier;
20+
import '../compiler/module_builder.dart'
21+
show isSdkInternalRuntimeUri, libraryUriToJsIdentifier, pathToJSIdentifier;
2122
import '../compiler/shared_command.dart' show SharedCompilerOptions;
2223
import '../compiler/shared_compiler.dart';
2324
import '../js_ast/js_ast.dart' as js_ast;
@@ -371,11 +372,7 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
371372

372373
@override
373374
String jsLibraryName(Library library) {
374-
var uri = library.importUri;
375-
if (uri.scheme == 'dart') {
376-
return isSdkInternalRuntime(library) ? 'dart' : uri.path;
377-
}
378-
return pathToJSIdentifier(p.withoutExtension(uri.pathSegments.last));
375+
return libraryUriToJsIdentifier(library.importUri);
379376
}
380377

381378
@override
@@ -405,8 +402,7 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
405402

406403
@override
407404
bool isSdkInternalRuntime(Library l) {
408-
var uri = l.importUri;
409-
return uri.scheme == 'dart' && uri.path == '_runtime';
405+
return isSdkInternalRuntimeUri(l.importUri);
410406
}
411407

412408
@override

0 commit comments

Comments
 (0)