Skip to content

Commit 757d806

Browse files
lrhncommit-bot@chromium.org
authored andcommitted
Add type parameter to Isolate.spawn.
This is a strong mode migration that was missed in the earlier rounds. It allows use of functions that have a more restricted argument type than Object in strong mode while still ensuring that the argument has a correct type. Change-Id: Ib00e3f4b4a679c003a992d674c36ef672729b22e Reviewed-on: https://dart-review.googlesource.com/24540 Commit-Queue: Lasse R.H. Nielsen <[email protected]> Reviewed-by: Leaf Petersen <[email protected]>
1 parent 1699831 commit 757d806

File tree

11 files changed

+82
-31
lines changed

11 files changed

+82
-31
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@
8888
* `dart:isolate`
8989
* Rename `IMMEDIATE` and `BEFORE_NEXT_EVENT` on `Isolate` to `immediate` and
9090
`beforeNextEvent`.
91+
* Make `Isolate.spawn` take a type parameter representing the argument type
92+
of the provided function. This allows functions with arguments types other
93+
than `Object` in strong mode.
9194

9295
* `dart.math`
9396
* Renamed `E`, `LN10`, `LN`, `LOG2E`, `LOG10E`, `PI`, `SQRT1_2` and `SQRT2`

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import 'dart:_js_helper' show patch;
88
import 'dart:_isolate_helper'
99
show CapabilityImpl, IsolateNatives, ReceivePortImpl, RawReceivePortImpl;
1010

11-
typedef _UnaryFunction(arg);
12-
1311
@patch
1412
class Isolate {
1513
static final _currentIsolateCache = IsolateNatives.currentIsolate;
@@ -38,19 +36,14 @@ class Isolate {
3836
}
3937

4038
@patch
41-
static Future<Isolate> spawn(void entryPoint(message), var message,
39+
static Future<Isolate> spawn<T>(void entryPoint(T message), T message,
4240
{bool paused: false,
4341
bool errorsAreFatal,
4442
SendPort onExit,
4543
SendPort onError}) {
4644
bool forcePause =
4745
(errorsAreFatal != null) || (onExit != null) || (onError != null);
4846
try {
49-
// Check for the type of `entryPoint` on the spawning isolate to make
50-
// error-handling easier.
51-
if (entryPoint is! _UnaryFunction) {
52-
throw new ArgumentError(entryPoint);
53-
}
5447
// TODO: Consider passing the errorsAreFatal/onExit/onError values
5548
// as arguments to the internal spawnUri instead of setting
5649
// them after the isolate has been created.

pkg/dev_compiler/tool/input_sdk/private/isolate_helper.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ class IsolateNatives {
936936
}
937937

938938
static Future<List> spawnFunction(
939-
void topLevelFunction(message), var message, bool startPaused) {
939+
void topLevelFunction(Null message), var message, bool startPaused) {
940940
IsolateNatives.enableSpawnWorker = true;
941941
final name = _getJSFunctionName(topLevelFunction);
942942
if (name == null) {

runtime/lib/isolate_patch.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ class Isolate {
325325
(VMLibraryHooks.resolvePackageUriFuture != null);
326326

327327
@patch
328-
static Future<Isolate> spawn(void entryPoint(message), var message,
328+
static Future<Isolate> spawn<T>(void entryPoint(T message), T message,
329329
{bool paused: false,
330330
bool errorsAreFatal,
331331
SendPort onExit,

sdk/lib/_internal/js_runtime/lib/isolate_helper.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ class IsolateNatives {
949949
}
950950

951951
static Future<List> spawnFunction(
952-
void topLevelFunction(message), var message, bool startPaused) {
952+
void topLevelFunction(Null message), var message, bool startPaused) {
953953
IsolateNatives.enableSpawnWorker = true;
954954
final name = _getJSFunctionName(topLevelFunction);
955955
if (name == null) {

sdk/lib/_internal/js_runtime/lib/isolate_patch.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'dart:_js_helper' show patch;
88
import 'dart:_isolate_helper'
99
show CapabilityImpl, IsolateNatives, ReceivePortImpl, RawReceivePortImpl;
1010

11-
typedef _UnaryFunction(arg);
11+
typedef _UnaryFunction(Null arg);
1212

1313
@patch
1414
class Isolate {
@@ -41,7 +41,7 @@ class Isolate {
4141
}
4242

4343
@patch
44-
static Future<Isolate> spawn(void entryPoint(message), var message,
44+
static Future<Isolate> spawn<T>(void entryPoint(T message), T message,
4545
{bool paused: false,
4646
bool errorsAreFatal,
4747
SendPort onExit,

sdk/lib/core/uri.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,7 +2848,7 @@ class _Uri implements Uri {
28482848
// be escaped or not.
28492849

28502850
// The unreserved characters of RFC 3986.
2851-
static const _unreservedTable = const [
2851+
static const _unreservedTable = const <int>[
28522852
// LSB MSB
28532853
// | |
28542854
0x0000, // 0x00 - 0x0f 0000000000000000
@@ -2868,7 +2868,7 @@ class _Uri implements Uri {
28682868
];
28692869

28702870
// The unreserved characters of RFC 2396.
2871-
static const _unreserved2396Table = const [
2871+
static const _unreserved2396Table = const <int>[
28722872
// LSB MSB
28732873
// | |
28742874
0x0000, // 0x00 - 0x0f 0000000000000000
@@ -2888,7 +2888,7 @@ class _Uri implements Uri {
28882888
];
28892889

28902890
// Table of reserved characters specified by ECMAScript 5.
2891-
static const _encodeFullTable = const [
2891+
static const _encodeFullTable = const <int>[
28922892
// LSB MSB
28932893
// | |
28942894
0x0000, // 0x00 - 0x0f 0000000000000000
@@ -2908,7 +2908,7 @@ class _Uri implements Uri {
29082908
];
29092909

29102910
// Characters allowed in the scheme.
2911-
static const _schemeTable = const [
2911+
static const _schemeTable = const <int>[
29122912
// LSB MSB
29132913
// | |
29142914
0x0000, // 0x00 - 0x0f 0000000000000000
@@ -2928,7 +2928,7 @@ class _Uri implements Uri {
29282928
];
29292929

29302930
// Characters allowed in scheme except for upper case letters.
2931-
static const _schemeLowerTable = const [
2931+
static const _schemeLowerTable = const <int>[
29322932
// LSB MSB
29332933
// | |
29342934
0x0000, // 0x00 - 0x0f 0000000000000000
@@ -2952,7 +2952,7 @@ class _Uri implements Uri {
29522952
// / "*" / "+" / "," / ";" / "="
29532953
// RFC 3986 section 2.3.
29542954
// unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
2955-
static const _subDelimitersTable = const [
2955+
static const _subDelimitersTable = const <int>[
29562956
// LSB MSB
29572957
// | |
29582958
0x0000, // 0x00 - 0x0f 0000000000000000
@@ -2974,7 +2974,7 @@ class _Uri implements Uri {
29742974
// General delimiter characters, RFC 3986 section 2.2.
29752975
// gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
29762976
//
2977-
static const _genDelimitersTable = const [
2977+
static const _genDelimitersTable = const <int>[
29782978
// LSB MSB
29792979
// | |
29802980
0x0000, // 0x00 - 0x0f 0000000000000000
@@ -2996,7 +2996,7 @@ class _Uri implements Uri {
29962996
// Characters allowed in the userinfo as of RFC 3986.
29972997
// RFC 3986 Appendix A
29982998
// userinfo = *( unreserved / pct-encoded / sub-delims / ':')
2999-
static const _userinfoTable = const [
2999+
static const _userinfoTable = const <int>[
30003000
// LSB MSB
30013001
// | |
30023002
0x0000, // 0x00 - 0x0f 0000000000000000
@@ -3018,7 +3018,7 @@ class _Uri implements Uri {
30183018
// Characters allowed in the reg-name as of RFC 3986.
30193019
// RFC 3986 Appendix A
30203020
// reg-name = *( unreserved / pct-encoded / sub-delims )
3021-
static const _regNameTable = const [
3021+
static const _regNameTable = const <int>[
30223022
// LSB MSB
30233023
// | |
30243024
0x0000, // 0x00 - 0x0f 0000000000000000
@@ -3040,7 +3040,7 @@ class _Uri implements Uri {
30403040
// Characters allowed in the path as of RFC 3986.
30413041
// RFC 3986 section 3.3.
30423042
// pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
3043-
static const _pathCharTable = const [
3043+
static const _pathCharTable = const <int>[
30443044
// LSB MSB
30453045
// | |
30463046
0x0000, // 0x00 - 0x0f 0000000000000000

sdk/lib/isolate/isolate.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ class Isolate {
228228
* Returns a future which will complete with an [Isolate] instance if the
229229
* spawning succeeded. It will complete with an error otherwise.
230230
*/
231-
external static Future<Isolate> spawn(void entryPoint(message), var message,
231+
external static Future<Isolate> spawn<T>(
232+
void entryPoint(T message), T message,
232233
{bool paused: false,
233234
bool errorsAreFatal,
234235
SendPort onExit,
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Check that Isolate.spawn is generic.
6+
library spawn_generic;
7+
8+
import "dart:isolate";
9+
import "dart:async";
10+
import "package:async_helper/async_helper.dart";
11+
import "package:expect/expect.dart";
12+
13+
void isomain(num args) {
14+
print(args);
15+
// All is well. No throwing.
16+
}
17+
18+
int _count = 0;
19+
void enter() {
20+
asyncStart();
21+
_count++;
22+
}
23+
24+
bool exit() {
25+
asyncEnd();
26+
return --_count == 0;
27+
}
28+
29+
main() {
30+
var remotePort = new RawReceivePort();
31+
remotePort.handler = (m) {
32+
if (m == null) {
33+
if (exit()) remotePort.close();
34+
} else {
35+
List list = m;
36+
throw new AsyncError(m[0], new StackTrace.fromString(m[1]));
37+
}
38+
};
39+
var port = remotePort.sendPort;
40+
41+
// Explicit type works.
42+
enter();
43+
Isolate.spawn<int>(isomain, 42, onExit: port, onError: port);
44+
enter();
45+
Isolate.spawn<num>(isomain, 42, onExit: port, onError: port);
46+
enter();
47+
Isolate.spawn<num>(isomain, 1.2, onExit: port, onError: port);
48+
enter();
49+
Isolate.spawn<double>(isomain, 1.2, onExit: port, onError: port);
50+
enter();
51+
Isolate.spawn<int>(isomain, null, onExit: port, onError: port);
52+
53+
// Inference gets it right.
54+
enter();
55+
Isolate.spawn(isomain, 42, onExit: port, onError: port);
56+
enter();
57+
Isolate.spawn(isomain, 1.2, onExit: port, onError: port);
58+
enter();
59+
Isolate.spawn(isomain, null, onExit: port, onError: port);
60+
}

tests/lib_2/lib_2_kernel.status

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@ convert/json_toEncodable_reviver_test: CompileTimeError
308308
convert/json_utf8_chunk_test: RuntimeError
309309
convert/streamed_conversion_json_encode1_test: RuntimeError
310310
convert/streamed_conversion_json_utf8_encode_test: RuntimeError
311-
isolate/compile_time_error_test/none: RuntimeError
312311
isolate/count_test: Timeout
313312
isolate/cross_isolate_message_test: RuntimeError
314313
isolate/illegal_msg_function_test: RuntimeError
@@ -333,19 +332,15 @@ isolate/message_test: RuntimeError
333332
isolate/mint_maker_test: RuntimeError
334333
isolate/nested_spawn2_test: RuntimeError
335334
isolate/nested_spawn_test: Timeout
336-
isolate/ondone_test: RuntimeError
337335
isolate/ping_pause_test: RuntimeError
338336
isolate/raw_port_test: RuntimeError
339337
isolate/request_reply_test: Timeout
340-
isolate/simple_message_test/none: RuntimeError
341338
isolate/spawn_function_test: Timeout
342-
isolate/spawn_uri_missing_from_isolate_test: RuntimeError
343339
isolate/spawn_uri_multi_test/none: RuntimeError
344340
isolate/spawn_uri_nested_vm_test: RuntimeError
345341
isolate/spawn_uri_test: RuntimeError
346342
isolate/spawn_uri_vm_test: RuntimeError
347343
isolate/stacktrace_message_test: RuntimeError
348-
isolate/start_paused_test: RuntimeError
349344
isolate/static_function_test: Timeout
350345
isolate/timer_isolate_test: RuntimeError
351346
isolate/typed_message_test: RuntimeError
@@ -397,7 +392,6 @@ mirrors/reflected_type_test/03: MissingCompileTimeError
397392
mirrors/regress_16321_test/none: Crash
398393
mirrors/regress_19731_test: RuntimeError
399394
mirrors/return_type_test: RuntimeError
400-
mirrors/spawn_function_root_library_test: RuntimeError
401395
mirrors/top_level_accessors_test/01: MissingCompileTimeError
402396
mirrors/type_argument_is_type_variable_test: RuntimeError
403397
mirrors/typearguments_mirror_test: RuntimeError

tests/lib_2/mirrors/spawn_function_root_library_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ child(SendPort port) {
1616
}
1717

1818
main() {
19-
var port;
19+
RawReceivePort port;
2020
port = new RawReceivePort((String childRootUri) {
2121
LibraryMirror root = currentMirrorSystem().isolate.rootLibrary;
2222
Expect.isNotNull(root);

0 commit comments

Comments
 (0)