Files
sdk/tests/lib/isolate/weak_reference_message_2_test.dart
T
Martin Kustermann 31ac1d26f9 [vm] Make Isolate.spawnUri() work in AOT iff the uri is compatible AOT snapshot
Right now the implementation of `Isolate.spawnUri(<uri>, ...)` in
the standalone embedder is to ignore `<uri>` and make the spawnned
isolate from the same AOT snapshot as the main isolate.

This is very confusing and very incorrect.

Instead `Isolate.spawnUri()` should work if-and-only-if the given
`<uri>` points to a valid and compatible AOT snapshot. If not, it should
throw an appropriate exception.

Fixes https://github.com/dart-lang/sdk/issues/48375
Fixes https://github.com/dart-lang/sdk/issues/48326

TEST=vm/dart{,_2}/spawn_uri_aot_test

Change-Id: I279ace08ac1b9a9eed3ae03ebe5d9e2336c1e5c9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/232603
Reviewed-by: Tess Strickland <sstrickl@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
2022-02-14 15:18:33 +00:00

81 lines
2.2 KiB
Dart

// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import "dart:isolate";
import 'dart:typed_data';
import "package:expect/expect.dart";
void main(List<String> arguments, Object? message) async {
if (arguments.length == 1) {
assert(arguments[0] == 'helper');
await runHelper(message as SendPort);
} else {
await runTest();
}
}
Future<void> runTest() async {
final port = ReceivePort();
// By spawning the isolate from an uri the newly isolate will run in it's own
// isolate group. This way we can test the message snapshot serialization
// code.
await Isolate.spawnUri(
Platform.script,
['helper'],
port.sendPort,
);
final message = await port.first;
final weakRef1copy = message[0] as WeakReference<Uint8List>;
final weakRef2copy = message[1] as WeakReference<Uint8List>;
final weakRef3copy = message[3] as WeakReference<Uint8List>;
final weakRef4copy = message[5] as WeakReference<Uint8List>;
Expect.isNull(weakRef1copy.target);
Expect.equals(2, weakRef2copy.target?.length);
Expect.isNull(weakRef3copy.target);
Expect.equals(4, weakRef4copy.target?.length);
port.close();
}
Future<void> runHelper(SendPort port) async {
final object1 = Uint8List(1);
final weakRef1 = WeakReference(object1);
final object2 = Uint8List(2);
final weakRef2 = WeakReference(object2);
final object3 = Uint8List(3);
final weakRef3 = WeakReference(object3);
final object4 = Uint8List(4);
final weakRef4 = WeakReference(object4);
final key3 = Object();
final expando3 = Expando();
expando3[key3] = object3;
final key4 = Object();
final expando4 = Expando();
expando4[key4] = object4;
final message = <dynamic>[
weakRef1, // Does not have its target inluded.
weakRef2, // Has its target included later than itself.
object2,
weakRef3, // Does not have its target inluded.
expando3,
weakRef4, // Has its target included due to expando.
expando4,
key4,
];
port.send(message);
}
class Nonce {
final int value;
Nonce(this.value);
String toString() => 'Nonce($value)';
}