Skip to content

Commit

Permalink
Version 3.6.0-205.0.dev
Browse files Browse the repository at this point in the history
Merge 3cc6105 into dev
  • Loading branch information
Dart CI committed Aug 31, 2024
2 parents 9f2fd00 + 3cc6105 commit 1b20c24
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 79 deletions.
7 changes: 4 additions & 3 deletions pkg/dev_compiler/lib/js/ddc/ddc_module_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1325,8 +1325,9 @@ if (!self.deferred_loader) {
throw 'Library not defined: ' + libraryName + '. Failed to initialize.';
}
currentLibrary = initializer(currentLibrary);
// TODO(nshahan): Link the library when link methods are added to the
// compiled output.
// Link the library. This action will trigger the initialization and
// linking of dependency libraries as needed.
currentLibrary.link();
this.libraries[libraryName] = currentLibrary;
}
if (installFn != null) {
Expand Down Expand Up @@ -1367,7 +1368,7 @@ if (!self.deferred_loader) {
// See docs on `DartDevEmbedder.runMain`.
runMain(entryPointLibraryName, dartSdkRuntimeOptions) {
console.log('Setting Dart SDK runtime options.');
let dartRuntimeLibrary = this.initializeAndLinkLibrary('dart');
let dartRuntimeLibrary = this.initializeAndLinkLibrary('dart:_runtime');

// TODO(nshahan) Use a single method in the Dart SDK to set all options.
dartRuntimeLibrary.weakNullSafetyErrors(dartSdkRuntimeOptions.weakNullSafetyErrors);
Expand Down
20 changes: 9 additions & 11 deletions pkg/dev_compiler/lib/src/compiler/module_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -474,21 +474,21 @@ class DdcLibraryBundleBuilder extends _ModuleBuilder {
Identifier? moduleVar, ImportDeclaration import) {
var items = <Statement>[];

var fromName = import.from;
for (var importName in import.namedImports!) {
// import * is not emitted by the compiler, so we don't handle it here.
assert(!importName.isStar);

var fromName = importName.name!.name;
var asName = importName.asName ?? importName.name;
if (import.from.valueWithoutQuotes != dartSdkModule) {
// Load non-SDK modules on demand (i.e., deferred).
items.add(js.statement(
'let # = dartDevEmbedder.importLibrary(#, function (lib) { '
'# = lib; });',
[asName, js.string(fromName), asName]));
[asName, fromName, asName]));
} else {
items.add(js.statement('const # = dartDevEmbedder.importLibrary(#)',
[asName, js.string(fromName)]));
items.add(js.statement(
'const # = dartDevEmbedder.importLibrary(#)', [asName, fromName]));
}
}
return items;
Expand All @@ -503,13 +503,11 @@ class DdcLibraryBundleBuilder extends _ModuleBuilder {
for (var export in exports) {
// Dart SDK module must export the libraries via a definition until it
// can be separated into individual libraries.
var names = export.exportedNames!;
for (var name in names) {
var alias = name.asName ?? name.name!;
items.add(js.statement(
'dartDevEmbedder.defineLibrary(#, function(_) { return #; })',
[js.string(name.name!.name), alias]));
}
var name = export.exportedNames!.single;
var alias = name.asName ?? name.name!;
items.add(js.statement(
'dartDevEmbedder.defineLibrary(#, function(_) { return #; })',
[(export.exported as ExportClause).from, alias]));
}
}
return items;
Expand Down
121 changes: 92 additions & 29 deletions pkg/dev_compiler/lib/src/kernel/compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -915,11 +915,33 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
_emitLibraryProcedures(library);
_emitTopLevelFields(library.fields);
}

if (_options.emitLibraryBundle) {
// TODO(nshahan): Remove when the Dart SDK can be compiled with the
// `LibraryBundleCompiler`.
_moduleItems.add(_emitEmptyLinkMethod(
_jsLibraryName(library), _emitLibraryName(library)));
}
_staticTypeContext.leaveLibrary(_currentLibrary!);
_currentLibrary = null;
}

/// Returns an empty placeholder link method for the libraries in the SDK.
///
/// This is a temporary solution to allow the Dart SDK to act like it was
/// compiled as a bundle of individual libraries.
// TODO(nshahan): Remove when the Dart SDK can be compiled with the
// `LibraryBundleCompiler`.
js_ast.Statement _emitEmptyLinkMethod(
String libraryName, js_ast.Identifier libraryId) {
assert(_options.emitLibraryBundle && _isBuildingSdk);
var functionName = _emitTemporaryId('link__$libraryName');
return js.statement('# = #', [
js_ast.PropertyAccess.field(libraryId, 'link'),
js_ast.NamedFunction(
functionName, js_ast.Fun(const [], js_ast.Block(const [])))
]);
}

void _emitExports(Library library) {
assert(_currentLibrary == null);
_currentLibrary = library;
Expand Down Expand Up @@ -7934,38 +7956,70 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
items.add(js.statement('#.library = #', [_runtimeModule, libraryProto]));
exports.add(js_ast.NameSpecifier(_runtimeModule));
}
if (_options.emitLibraryBundle) {
assert(_isBuildingSdk);
for (var library in libraries) {
js_ast.Identifier libraryId;
if (_isSdkInternalRuntime(library)) {
libraryId = _runtimeModule;
} else if (_isDartLibrary(library, '_rti')) {
libraryId = _rtiLibraryId;
} else {
libraryId = js_ast.TemporaryId(_jsLibraryName(library));
}
_libraries[library] = libraryId;
var alias = _jsLibraryAlias(library);
var aliasId = alias == null ? null : js_ast.TemporaryId(alias);
items.add(js_ast.ExportDeclaration(js_ast.ExportClause(
[js_ast.NameSpecifier(libraryId, asName: aliasId)],
from: js.string('${library.importUri}'))));
// The initialization object for the runtime library is created above so
// it is skipped here.
if (_isSdkInternalRuntime(library)) continue;
items.add(js.statement(
'const # = Object.create(#.library)', [libraryId, _runtimeModule]));
}
// dart:_runtime has a magic library that holds extension method symbols.
// TODO(nshahan): Could this be created with a kernel transform or just
// become a member in dart:_runtime?
items.add(js.statement('const # = Object.create(#.library)',
[_extensionSymbolsModule, _runtimeModule]));
items.add(js_ast.ExportDeclaration(js_ast.ExportClause(
[js_ast.NameSpecifier(_extensionSymbolsModule)],
from: js.string('dartx'))));
} else {
for (var library in libraries) {
if (_isBuildingSdk && _isSdkInternalRuntime(library)) {
_libraries[library] = _runtimeModule;
continue;
}
var libraryId = _isBuildingSdk && _isDartLibrary(library, '_rti')
? _rtiLibraryId
: js_ast.TemporaryId(_jsLibraryName(library));

for (var library in libraries) {
if (_isBuildingSdk && _isSdkInternalRuntime(library)) {
_libraries[library] = _runtimeModule;
continue;
}
var libraryId = _isBuildingSdk && _isDartLibrary(library, '_rti')
? _rtiLibraryId
: js_ast.TemporaryId(_jsLibraryName(library));

_libraries[library] = libraryId;
var alias = _jsLibraryAlias(library);
var aliasId = alias == null ? null : js_ast.TemporaryId(alias);
_libraries[library] = libraryId;
var alias = _jsLibraryAlias(library);
var aliasId = alias == null ? null : js_ast.TemporaryId(alias);

// TODO(vsm): Change back to `const`.
// See https://github.com/dart-lang/sdk/issues/40380.
items.add(js.statement(
'var # = Object.create(#.library)', [libraryId, _runtimeModule]));
exports.add(js_ast.NameSpecifier(libraryId, asName: aliasId));
}
// TODO(vsm): Change back to `const`.
// See https://github.com/dart-lang/sdk/issues/40380.
items.add(js.statement(
'var # = Object.create(#.library)', [libraryId, _runtimeModule]));
exports.add(js_ast.NameSpecifier(libraryId, asName: aliasId));
}

// dart:_runtime has a magic module that holds extension method symbols.
// TODO(jmesserly): find a cleaner design for this.
if (_isBuildingSdk) {
var id = _extensionSymbolsModule;
// TODO(vsm): Change back to `const`.
// See https://github.com/dart-lang/sdk/issues/40380.
items.add(js
.statement('var # = Object.create(#.library)', [id, _runtimeModule]));
exports.add(js_ast.NameSpecifier(id));
// dart:_runtime has a magic module that holds extension method symbols.
// TODO(jmesserly): find a cleaner design for this.
if (_isBuildingSdk) {
var id = _extensionSymbolsModule;
// TODO(vsm): Change back to `const`.
// See https://github.com/dart-lang/sdk/issues/40380.
items.add(js.statement(
'var # = Object.create(#.library)', [id, _runtimeModule]));
exports.add(js_ast.NameSpecifier(id));
}
items.add(js_ast.ExportDeclaration(js_ast.ExportClause(exports)));
}
items.add(js_ast.ExportDeclaration(js_ast.ExportClause(exports)));

if (_isBuildingSdk) {
// Initialize the private name function.
Expand Down Expand Up @@ -8210,6 +8264,15 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
/// field of the result.
js_ast.Program _finishModule(List<js_ast.ModuleItem> items, String moduleName,
{List<js_ast.Comment> header = const []}) {
if (_options.emitLibraryBundle) {
assert(_isBuildingSdk);
// Manually add a link method for the runtime "dartx" library. It is
// synthetically created by DDC and doesn't have an associated kernel
// library node.
// TODO(nshahan): Remove when the Dart SDK can be compiled with the
// `LibraryBundleCompiler`.
_moduleItems.add(_emitEmptyLinkMethod('dartx', _extensionSymbolsModule));
}
// TODO(jmesserly): there's probably further consolidation we can do
// between DDC's two backends, by moving more code into this method, as the
// code between `startModule` and `finishModule` is very similar in both.
Expand Down
59 changes: 42 additions & 17 deletions pkg/dev_compiler/lib/src/kernel/compiler_new.dart
Original file line number Diff line number Diff line change
Expand Up @@ -867,15 +867,37 @@ class LibraryCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
// Emit the hoisted instantiated generic class table cache variables
items.addAll(_genericClassTable.dischargeBoundTypes());
_ticker?.logMs('Emitted instantiated generic class table');

var module = _finishLibrary(
items, _jsLibraryName(library), _emitLibraryName(library));
var compiledLibrary = _finishLibrary(
items, '${library.importUri}', _emitLibraryName(library));
_ticker?.logMs('Finished emitting module');

// Mark as finished for incremental mode, so it is safe to
// switch to the incremental mode for expression compilation.
_moduleEmitted = true;
return module;
return compiledLibrary;
}

/// Returns a method that will perform all class hierarchy operations for the
/// classes defined in this module.
///
/// At a high level this method performs the prototype stitching for all
/// `class A extends B` relationships but in practice will also include the
/// operations that implicitly depend on those relationships to be established
/// so they can walk the prototype chain.
js_ast.Statement _emitLibraryLinkMethod(Library library) {
var libraryName = _emitLibraryName(library);
var nameExpr = js_ast.PropertyAccess.field(libraryName, 'link');
var functionName = _emitTemporaryId('link__${_jsLibraryName(library)}');

var parameters = const <js_ast.Parameter>[];
var body = js_ast.Block([
// TODO(nshahan): Remove logging and add linking statements here.
js.statement(
'console.log("Linking library: ${_jsLibraryName(library)}")'),
]);
var function =
js_ast.NamedFunction(functionName, js_ast.Fun(parameters, body));
return js.statement('# = #', [nameExpr, function]);
}

/// Choose a canonical name from the [library] element.
Expand Down Expand Up @@ -999,7 +1021,8 @@ class LibraryCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
_emitLibraryProcedures(library);
_emitTopLevelFields(library.fields);
}

// Additional method used by the module system to link class hierarchies.
_moduleItems.add(_emitLibraryLinkMethod(library));
_staticTypeContext.leaveLibrary(_currentLibrary!);
}

Expand Down Expand Up @@ -8023,36 +8046,38 @@ class LibraryCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
// import {foo} from 'foo'; // if no rename needed
// import {foo as foo$} from 'foo'; // if rename was needed
//
var imports = <js_ast.NameSpecifier>[];
for (var library in libraries) {
if (!_incrementalMode ||
usedLibraries!.contains(_jsLibraryName(library))) {
var alias = _jsLibraryAlias(library);
if (alias != null) {
var aliasId = js_ast.TemporaryId(alias);
imports.add(
js_ast.NameSpecifier(aliasId, asName: _imports[library]));
items.add(js_ast.ImportDeclaration(
from: js.string('${library.importUri}'),
namedImports: [
js_ast.NameSpecifier(aliasId, asName: _imports[library])
]));
} else {
imports.add(js_ast.NameSpecifier(_imports[library]));
items.add(js_ast.ImportDeclaration(
from: js.string('${library.importUri}'),
namedImports: [js_ast.NameSpecifier(_imports[library])]));
}
}
}

if (module == coreModuleName) {
if (!_incrementalMode ||
usedLibraries!.contains(_runtimeModule.name)) {
imports.add(js_ast.NameSpecifier(_runtimeModule));
items.add(js_ast.ImportDeclaration(
from: js.string('dart:_runtime'),
namedImports: [js_ast.NameSpecifier(_runtimeModule)]));
}
if (!_incrementalMode ||
usedLibraries!.contains(_extensionSymbolsModule.name)) {
imports.add(js_ast.NameSpecifier(_extensionSymbolsModule));
items.add(js_ast.ImportDeclaration(
from: js.string('dartx'),
namedImports: [js_ast.NameSpecifier(_extensionSymbolsModule)]));
}
}

if (!_incrementalMode || imports.isNotEmpty) {
items.add(js_ast.ImportDeclaration(
namedImports: imports, from: js.string(module, "'")));
}
}
});
}
Expand Down
Loading

0 comments on commit 1b20c24

Please sign in to comment.