diff --git a/runtime/bin/io_natives.cc b/runtime/bin/io_natives.cc index 7fe58392c6e..86d7c2d8f22 100644 --- a/runtime/bin/io_natives.cc +++ b/runtime/bin/io_natives.cc @@ -48,6 +48,7 @@ namespace bin { V(Process_Wait, 5) \ V(Process_Kill, 3) \ V(Process_SetExitCode, 1) \ + V(Process_GetExitCode, 0) \ V(Process_Exit, 1) \ V(Process_Sleep, 1) \ V(Process_Pid, 1) \ diff --git a/runtime/bin/process.cc b/runtime/bin/process.cc index 09476b91009..5d392d23fff 100644 --- a/runtime/bin/process.cc +++ b/runtime/bin/process.cc @@ -221,6 +221,11 @@ void FUNCTION_NAME(Process_SetExitCode)(Dart_NativeArguments args) { } +void FUNCTION_NAME(Process_GetExitCode)(Dart_NativeArguments args) { + Dart_SetReturnValue(args, Dart_NewInteger(Process::GlobalExitCode())); +} + + void FUNCTION_NAME(Process_Sleep)(Dart_NativeArguments args) { int64_t milliseconds = 0; // Ignore result if passing invalid argument and just set exit code to 0. diff --git a/runtime/bin/process_patch.dart b/runtime/bin/process_patch.dart index 02a5e86a8a2..bb6461e21c8 100644 --- a/runtime/bin/process_patch.dart +++ b/runtime/bin/process_patch.dart @@ -125,6 +125,7 @@ patch class _ProcessUtils { /* patch */ static void _exit(int status) native "Process_Exit"; /* patch */ static void _setExitCode(int status) native "Process_SetExitCode"; + /* patch */ static int _getExitCode() native "Process_GetExitCode"; /* patch */ static void _sleep(int millis) native "Process_Sleep"; /* patch */ static int _pid(Process process) native "Process_Pid"; /* patch */ static Stream _watchSignal(ProcessSignal signal) { diff --git a/sdk/lib/_internal/lib/io_patch.dart b/sdk/lib/_internal/lib/io_patch.dart index 0bd05440c24..6e0821a5f6c 100644 --- a/sdk/lib/_internal/lib/io_patch.dart +++ b/sdk/lib/_internal/lib/io_patch.dart @@ -179,6 +179,9 @@ patch class _ProcessUtils { patch static void _setExitCode(int status) { throw new UnsupportedError("ProcessUtils._setExitCode"); } + patch static int _getExitCode() { + throw new UnsupportedError("ProcessUtils._getExitCode"); + } patch static void _sleep(int millis) { throw new UnsupportedError("ProcessUtils._sleep"); } diff --git a/sdk/lib/io/process.dart b/sdk/lib/io/process.dart index 828145e8304..f90f8baa968 100644 --- a/sdk/lib/io/process.dart +++ b/sdk/lib/io/process.dart @@ -9,6 +9,7 @@ part of dart.io; class _ProcessUtils { external static void _exit(int status); external static void _setExitCode(int status); + external static int _getExitCode(); external static void _sleep(int millis); external static int _pid(Process process); external static Stream _watchSignal(ProcessSignal signal); @@ -49,7 +50,26 @@ void exit(int code) { } /** - * Global exit code for the Dart VM. + * Set the global exit code for the Dart VM. + * + * The exit code is global for the Dart VM and the last assignment to + * exitCode from any isolate determines the exit code of the Dart VM + * on normal termination. + * + * Default value is `0`. + * + * See [exit] for more information on how to chose a value for the + * exit code. + */ +void set exitCode(int code) { + if (code is !int) { + throw new ArgumentError("Integer value for exit code expected"); + } + _ProcessUtils._setExitCode(code); +} + +/* + * Get the global exit code for the Dart VM. * * The exit code is global for the Dart VM and the last assignment to * exitCode from any isolate determines the exit code of the Dart VM @@ -58,12 +78,7 @@ void exit(int code) { * See [exit] for more information on how to chose a value for the * exit code. */ -set exitCode(int code) { - if (code is !int) { - throw new ArgumentError("Integer value for exit code expected"); - } - _ProcessUtils._setExitCode(code); -} +int get exitCode => _ProcessUtils._getExitCode(); /** * Sleep for the duration specified in [duration]. diff --git a/tests/standalone/io/process_set_exit_code_script.dart b/tests/standalone/io/process_set_exit_code_script.dart index 9a34f052a14..38a8075367e 100644 --- a/tests/standalone/io/process_set_exit_code_script.dart +++ b/tests/standalone/io/process_set_exit_code_script.dart @@ -5,7 +5,13 @@ import "dart:io"; main() { + if (exitCode != 0) { + throw "Bad initial exit-code"; + } stdout.write("standard out"); stderr.write("standard error"); exitCode = 25; + if (exitCode != 25) { + throw "Exit-code not set correctly"; + } }