From 462e284064cedb3dde02df7c9b335dd0fde4d61c Mon Sep 17 00:00:00 2001 From: Srujan Gaddam Date: Wed, 28 Feb 2024 17:07:57 -0800 Subject: [PATCH 1/2] Use URI instead of library name When analyzing library elements, we should use their import URIs instead of their library name. This is especially relevant as https://dart-review.googlesource.com/c/sdk/+/352977 is planning to remove the library name from dart:js_interop. Also fixes an issue where we were returning instead of continuing in the loop that iterates over dart:js_interop. --- tool/update_bindings.dart | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/tool/update_bindings.dart b/tool/update_bindings.dart index 459a6e84..3906eac2 100644 --- a/tool/update_bindings.dart +++ b/tool/update_bindings.dart @@ -159,27 +159,26 @@ Future _runProc( } } -bool _isInJsTypesOrJsInterop(InterfaceElement element) => - element.library.isInSdk && - (element.library.name == '_js_types' || - element is ExtensionTypeElement && - element.library.name == 'dart.js_interop'); - +// Creates a supertype hierarchy of the JS types defined in `dart:js_interop`. Future _generateJsTypeSupertypes() async { // Use a file that uses `dart:js_interop` for analysis. final contextCollection = AnalysisContextCollection(includedPaths: [ p.fromUri(Platform.script.resolve('../lib/src/dom.dart')) ]); - final dartJsInterop = await contextCollection.contexts.single.currentSession - .getLibraryByUri('dart:js_interop') as LibraryElementResult; - final definedNames = dartJsInterop.element.exportNamespace.definedNames; + final dartJsInterop = (await contextCollection.contexts.single.currentSession + .getLibraryByUri('dart:js_interop') as LibraryElementResult) + .element; + final definedNames = dartJsInterop.exportNamespace.definedNames; // `SplayTreeMap` to avoid moving types around in `dart:js_interop` affecting // the code generation. final jsTypeSupertypes = SplayTreeMap(); for (final name in definedNames.keys) { final element = definedNames[name]; if (element is ExtensionTypeElement) { - if (!_isInJsTypesOrJsInterop(element)) return; + // Only extension types defined in `dart:js_interop` are JS types. + bool _isJSType(InterfaceElement element) => + element is ExtensionTypeElement && element.library == dartJsInterop; + if (!_isJSType(element)) continue; String? parentJsType; final supertype = element.supertype; @@ -190,7 +189,7 @@ Future _generateJsTypeSupertypes() async { // We should have at most one non-trivial supertype. assert(immediateSupertypes.length <= 1); for (final supertype in immediateSupertypes) { - if (_isInJsTypesOrJsInterop(supertype.element)) { + if (_isJSType(supertype.element)) { parentJsType = "'${supertype.element.name}'"; } } From 7ca94179c0277d1ff014de2756255ba847661c16 Mon Sep 17 00:00:00 2001 From: Srujan Gaddam Date: Wed, 28 Feb 2024 17:12:09 -0800 Subject: [PATCH 2/2] Cleanup comment and address naming lint --- tool/update_bindings.dart | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tool/update_bindings.dart b/tool/update_bindings.dart index 3906eac2..777ea398 100644 --- a/tool/update_bindings.dart +++ b/tool/update_bindings.dart @@ -159,7 +159,8 @@ Future _runProc( } } -// Creates a supertype hierarchy of the JS types defined in `dart:js_interop`. +// Generates a map of the JS type hierarchy defined in `dart:js_interop` that is +// used by the translator to handle IDL types. Future _generateJsTypeSupertypes() async { // Use a file that uses `dart:js_interop` for analysis. final contextCollection = AnalysisContextCollection(includedPaths: [ @@ -176,9 +177,9 @@ Future _generateJsTypeSupertypes() async { final element = definedNames[name]; if (element is ExtensionTypeElement) { // Only extension types defined in `dart:js_interop` are JS types. - bool _isJSType(InterfaceElement element) => + bool isJSType(InterfaceElement element) => element is ExtensionTypeElement && element.library == dartJsInterop; - if (!_isJSType(element)) continue; + if (!isJSType(element)) continue; String? parentJsType; final supertype = element.supertype; @@ -189,7 +190,7 @@ Future _generateJsTypeSupertypes() async { // We should have at most one non-trivial supertype. assert(immediateSupertypes.length <= 1); for (final supertype in immediateSupertypes) { - if (_isJSType(supertype.element)) { + if (isJSType(supertype.element)) { parentJsType = "'${supertype.element.name}'"; } }