From f8f06d784ad1c9e67cbdb5e25eb08d0c62ca8d8c Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Wed, 19 Aug 2015 13:52:36 +0200 Subject: [PATCH] Implement new parameters on Isolate.spawn() for dart2js. R=sgjesse@google.com Review URL: https://codereview.chromium.org//1294803003 . --- .../js_runtime/lib/isolate_helper.dart | 1 + .../js_runtime/lib/isolate_patch.dart | 34 ++++++++++++++++--- tests/isolate/exit_at_spawn_test.dart | 25 ++++++++++++-- tests/isolate/isolate.status | 3 -- 4 files changed, 53 insertions(+), 10 deletions(-) diff --git a/sdk/lib/_internal/js_runtime/lib/isolate_helper.dart b/sdk/lib/_internal/js_runtime/lib/isolate_helper.dart index a6edabbf03e..9e463ba5b5b 100644 --- a/sdk/lib/_internal/js_runtime/lib/isolate_helper.dart +++ b/sdk/lib/_internal/js_runtime/lib/isolate_helper.dart @@ -1069,6 +1069,7 @@ class IsolateNatives { } else { topLevel(); } + context._updateGlobalState(); } if (startPaused) { diff --git a/sdk/lib/_internal/js_runtime/lib/isolate_patch.dart b/sdk/lib/_internal/js_runtime/lib/isolate_patch.dart index 53361cc3a48..eb681071b2a 100644 --- a/sdk/lib/_internal/js_runtime/lib/isolate_patch.dart +++ b/sdk/lib/_internal/js_runtime/lib/isolate_patch.dart @@ -25,11 +25,35 @@ class Isolate { static Future spawn(void entryPoint(message), var message, {bool paused: false, bool errorsAreFatal, SendPort onExit, SendPort onError}) { + bool forcePause = (errorsAreFatal != null) || + (onExit != null) || + (onError != null); try { - return IsolateNatives.spawnFunction(entryPoint, message, paused) - .then((msg) => new Isolate(msg[1], - pauseCapability: msg[2], - terminateCapability: msg[3])); + // TODO: Consider passing the errorsAreFatal/onExit/onError values + // as arguments to the internal spawnUri instead of setting + // them after the isolate has been created. + return IsolateNatives.spawnFunction(entryPoint, message, + paused || forcePause) + .then((msg) { + var isolate = new Isolate(msg[1], + pauseCapability: msg[2], + terminateCapability: msg[3]); + if (forcePause) { + if (errorsAreFatal != null) { + isolate.setErrorsFatal(errorsAreFatal); + } + if (onExit != null) { + isolate.addOnExitListener(onExit); + } + if (onError != null) { + isolate.addErrorListener(onError); + } + if (!paused) { + isolate.resume(isolate.pauseCapability); + } + } + return isolate; + }); } catch (e, st) { return new Future.error(e, st); } @@ -70,7 +94,7 @@ class Isolate { isolate.addOnExitListener(onExit); } if (onError != null) { - isolate.addOnErrorListener(onError); + isolate.addErrorListener(onError); } if (!paused) { isolate.resume(isolate.pauseCapability); diff --git a/tests/isolate/exit_at_spawn_test.dart b/tests/isolate/exit_at_spawn_test.dart index a03b565b004..355c449607e 100644 --- a/tests/isolate/exit_at_spawn_test.dart +++ b/tests/isolate/exit_at_spawn_test.dart @@ -9,19 +9,40 @@ import "dart:async"; import "package:async_helper/async_helper.dart"; import "package:expect/expect.dart"; +// Isolate exiting immediately. isomain(args) {} +// Isolate exiting after running microtasks. +isomain2(args) { + scheduleMicrotask((){}); +} + +// Isolate exiting after running timers. +isomain3(args) { + new Timer(Duration.ZERO, (){}); +} + main(){ asyncStart(); + test(isomain); + test(isomain2); + test(isomain3); + + asyncEnd(); +} + +void test(mainFunction) { + asyncStart(); + RawReceivePort exitPort = new RawReceivePort(); exitPort.handler = (message) { Expect.equals(null, message); exitPort.close(); asyncEnd(); }; - - Isolate.spawn(isomain, + + Isolate.spawn(mainFunction, null, // Setup handler as part of spawn. errorsAreFatal: false, diff --git a/tests/isolate/isolate.status b/tests/isolate/isolate.status index 035100e4eff..4073b51b362 100644 --- a/tests/isolate/isolate.status +++ b/tests/isolate/isolate.status @@ -55,9 +55,6 @@ message3_test/constMap: RuntimeError # Issue 21817 message3_test/constInstance: RuntimeError # Issue 21817 browser/issue_12474_test: CompileTimeError # Issue 22529 enum_const_test/02: RuntimeError # Issue 21817 -error_exit_at_spawn_test: Fail # Issue 23876 -error_at_spawn_test: Fail # Issue 23876 -exit_at_spawn_test: Fail # Issue 23876 [ $compiler == dart2js && $runtime != d8 ] error_exit_at_spawn_test: Skip # Issue 23876