Skip to content

Commit dd5530a

Browse files
vsmenoncommit-bot@chromium.org
authored andcommitted
[dartdevc] make ImmutableSet private
Patch files should not add public members to the underlying library. Siggi was hitting this when wiring up modular testing for DDC: https://dart-review.googlesource.com/c/sdk/+/103680/3/pkg/dev_compiler/tool/input_sdk/private/ddc_runtime/operations.dart#544 The ddk builder appears to have be swallowing this error - not yet sure why. Change-Id: I86098b25a344b2bb8b69fe3dc386d01f6f7cb309 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/103740 Reviewed-by: Nate Bosch <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]> Commit-Queue: Vijay Menon <[email protected]>
1 parent 4c9bf54 commit dd5530a

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

pkg/dev_compiler/tool/input_sdk/patch/collection_patch.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,9 @@ class _HashSet<E> extends _InternalSet<E>
303303
}
304304
}
305305

306-
class ImmutableSet<E> extends _HashSet<E> {
307-
ImmutableSet.from(JSArray entries) {
306+
// Used for DDC const sets.
307+
class _ImmutableSet<E> extends _HashSet<E> {
308+
_ImmutableSet.from(JSArray entries) {
308309
var map = _map;
309310
for (Object key in entries) {
310311
if (key == null) {
@@ -323,7 +324,7 @@ class ImmutableSet<E> extends _HashSet<E> {
323324
bool remove(Object key) => throw _unsupported();
324325

325326
static Error _unsupported() =>
326-
UnsupportedError("Cannot modify unmodifiable map");
327+
UnsupportedError("Cannot modify unmodifiable set");
327328
}
328329

329330
class _IdentityHashSet<E> extends _InternalSet<E>

pkg/dev_compiler/tool/input_sdk/private/ddc_runtime/operations.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,14 @@ Map<K, V> constMap<K, V>(JSArray elements) {
548548
}
549549

550550
final constantSets = JS('', 'new Map()');
551+
var _immutableSetConstructor;
552+
553+
// We cannot invoke private class constructors directly in Dart.
554+
Set<E> _createImmutableSet<E>(JSArray<E> elements) {
555+
_immutableSetConstructor ??=
556+
JS('', '#.#', getLibrary('dart:collection'), '_ImmutableSet\$');
557+
return JS('', 'new (#(#)).from(#)', _immutableSetConstructor, E, elements);
558+
}
551559

552560
Set<E> constSet<E>(JSArray<E> elements) {
553561
var count = elements.length;
@@ -557,7 +565,7 @@ Set<E> constSet<E>(JSArray<E> elements) {
557565
}
558566
var result = JS('', '#.get(#)', map, E);
559567
if (result != null) return result;
560-
result = ImmutableSet<E>.from(elements);
568+
result = _createImmutableSet<E>(elements);
561569
JS('', '#.set(#, #)', map, E, result);
562570
return result;
563571
}

pkg/dev_compiler/tool/input_sdk/private/ddc_runtime/rtti.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,26 +155,42 @@ void trackLibraries(
155155
}
156156

157157
List<String> _libraries;
158+
Map<String, Object> _libraryObjects;
158159
Map<String, List<String>> _parts;
159160

160161
_computeLibraryMetadata() {
161162
_libraries = [];
163+
_libraryObjects = {};
162164
_parts = {};
163165
var modules = getModuleNames();
164166
for (var name in modules) {
165167
// Add libraries from each module.
166168
var module = getModuleLibraries(name);
167-
_libraries.addAll(getOwnPropertyNames(module).cast());
169+
var libraries = getOwnPropertyNames(module).cast<String>();
170+
_libraries.addAll(libraries);
171+
for (var library in libraries) {
172+
_libraryObjects[library] = JS('', '#.#', module, library);
173+
}
168174

169175
// Add parts from each module.
170176
var partMap = getModulePartMap(name);
171-
List<String> libraries = List.from(getOwnPropertyNames(partMap));
177+
libraries = getOwnPropertyNames(partMap).cast<String>();
172178
for (var library in libraries) {
173179
_parts[library] = List.from(JS('List', '#.#', partMap, library));
174180
}
175181
}
176182
}
177183

184+
/// Returns the JS library object for a given library [uri] or
185+
/// undefined / null if it isn't loaded. Top-level types and
186+
/// methods are available on this object.
187+
Object getLibrary(String uri) {
188+
if (_libraryObjects == null) {
189+
_computeLibraryMetadata();
190+
}
191+
return _libraryObjects[uri];
192+
}
193+
178194
/// Returns a JSArray of library uris (e.g,
179195
/// ['dart:core', 'dart:_internal', ..., 'package:foo/bar.dart', ... 'main.dart'])
180196
/// loaded in this application.

tests/compiler/dartdevc_native/libraries_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import 'dart:_runtime' as dart;
99
part 'libraries_part.dart';
1010

1111
void main() {
12+
// Test getLibraries()
1213
var libraries = dart.getLibraries();
1314
Expect.isTrue(libraries.contains('dart:core'));
1415
Expect.isTrue(libraries.contains('package:expect/expect.dart'));
1516

17+
// Test getParts(...)
1618
var expectParts = dart.getParts('package:expect/expect.dart');
1719
Expect.isTrue(expectParts.isEmpty);
1820

@@ -22,4 +24,9 @@ void main() {
2224
var testParts = dart.getParts(testLibraries.first);
2325
Expect.isTrue(testParts.length == 1);
2426
Expect.isTrue(testParts.first.endsWith('libraries_part.dart'));
27+
28+
// Test getLibrary(...)
29+
var core = dart.getLibrary('dart:core');
30+
var stackTraceType = dart.wrapType(JS('', '#.StackTrace', core));
31+
Expect.equals(StackTrace, stackTraceType);
2532
}

0 commit comments

Comments
 (0)