diff options
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/js/30_os.js | 21 | ||||
-rw-r--r-- | runtime/js/99_main.js | 8 | ||||
-rw-r--r-- | runtime/ops/os/mod.rs | 10 |
3 files changed, 37 insertions, 2 deletions
diff --git a/runtime/js/30_os.js b/runtime/js/30_os.js index 8948cf1ad..866fad287 100644 --- a/runtime/js/30_os.js +++ b/runtime/js/30_os.js @@ -7,6 +7,7 @@ import { op_exec_path, op_exit, op_get_env, + op_get_exit_code, op_gid, op_hostname, op_loadavg, @@ -21,7 +22,9 @@ import { const { Error, FunctionPrototypeBind, + NumberParseInt, SymbolFor, + TypeError, } = primordials; import { Event, EventTarget } from "ext:deno_web/02_event.js"; @@ -75,7 +78,7 @@ function exit(code) { if (typeof code === "number") { op_set_exit_code(code); } else { - code = 0; + code = op_get_exit_code(); } // Dispatches `unload` only when it's not dispatched yet. @@ -94,6 +97,20 @@ function exit(code) { throw new Error("Code not reachable"); } +function getExitCode() { + return op_get_exit_code(); +} + +function setExitCode(value) { + const code = NumberParseInt(value, 10); + if (typeof code !== "number") { + throw new TypeError( + `Exit code must be a number, got: ${code} (${typeof code}).`, + ); + } + op_set_exit_code(code); +} + function setEnv(key, value) { op_set_env(key, value); } @@ -126,12 +143,14 @@ export { env, execPath, exit, + getExitCode, gid, hostname, loadavg, networkInterfaces, osRelease, osUptime, + setExitCode, setExitHandler, systemMemoryInfo, uid, diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index 3fa9fc41b..4f949b214 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -674,6 +674,14 @@ ObjectDefineProperties(finalDenoNs, { return internals.future ? undefined : customInspect; }, }, + exitCode: { + get() { + return os.getExitCode(); + }, + set(value) { + os.setExitCode(value); + }, + }, }); const { diff --git a/runtime/ops/os/mod.rs b/runtime/ops/os/mod.rs index 7a71590ba..f6f55f68f 100644 --- a/runtime/ops/os/mod.rs +++ b/runtime/ops/os/mod.rs @@ -32,6 +32,7 @@ deno_core::extension!( op_os_uptime, op_set_env, op_set_exit_code, + op_get_exit_code, op_system_memory_info, op_uid, op_runtime_memory_usage, @@ -60,12 +61,13 @@ deno_core::extension!( op_os_uptime, op_set_env, op_set_exit_code, + op_get_exit_code, op_system_memory_info, op_uid, op_runtime_memory_usage, ], middleware = |op| match op.name { - "op_exit" | "op_set_exit_code" => + "op_exit" | "op_set_exit_code" | "op_get_exit_code" => op.with_implementation_from(&deno_core::op_void_sync()), _ => op, }, @@ -165,6 +167,12 @@ fn op_set_exit_code(state: &mut OpState, #[smi] code: i32) { } #[op2(fast)] +#[smi] +fn op_get_exit_code(state: &mut OpState) -> i32 { + state.borrow_mut::<ExitCode>().get() +} + +#[op2(fast)] fn op_exit(state: &mut OpState) { let code = state.borrow::<ExitCode>().get(); std::process::exit(code) |