Skip to content

Commit

Permalink
process: remove process.exit(), process.exitCode coercion to integer
Browse files Browse the repository at this point in the history
This removes the deprecation, `DEP0164` and validates the exit code for
both `process.exit([code])` and `process.exitCode`.

Signed-off-by: Daeyeon Jeong <daeyeon.dev@gmail.com>
PR-URL: #43716
Refs: #43738
Refs: #44714
Refs: #44711
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
daeyeon authored Oct 19, 2022
1 parent 19f3973 commit 2d0d997
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 148 deletions.
5 changes: 4 additions & 1 deletion doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -3178,6 +3178,9 @@ thing instead.

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/43716
description: End-of-Life.
- version: v19.0.0
pr-url: https://github.com/nodejs/node/pull/44711
description: Runtime deprecation.
Expand All @@ -3195,7 +3198,7 @@ changes:
coercion.
-->

Type: Runtime
Type: End-of-Life

Values other than `undefined`, `null`, integer numbers, and integer strings
(e.g., `'1'`) are deprecated as value for the `code` parameter in
Expand Down
16 changes: 14 additions & 2 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1708,9 +1708,15 @@ that started the Node.js process. Symbolic links, if any, are resolved.
<!-- YAML
added: v0.1.13
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/43716
description: Only accepts a code of type number, or of type string if it
represents an integer.
-->
* `code` {integer} The exit code. **Default:** `0`.
* `code` {integer|string|null|undefined} The exit code. For string type, only
integer strings (e.g.,'1') are allowed. **Default:** `0`.
The `process.exit()` method instructs Node.js to terminate the process
synchronously with an exit status of `code`. If `code` is omitted, exit uses
Expand Down Expand Up @@ -1810,9 +1816,15 @@ than the current process.
<!-- YAML
added: v0.11.8
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/43716
description: Only accepts a code of type number, or of type string if it
represents an integer.
-->
* {integer}
* {integer|string|null|undefined} The exit code. For string type, only
integer strings (e.g.,'1') are allowed. **Default:** `undefined`.
A number which will be the process exit code, when the process either
exits gracefully, or is exited via [`process.exit()`][] without specifying
Expand Down
20 changes: 12 additions & 8 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ const {
ArrayPrototypeFill,
FunctionPrototypeCall,
JSONParse,
Number,
NumberIsNaN,
ObjectDefineProperty,
ObjectGetPrototypeOf,
ObjectSetPrototypeOf,
Expand All @@ -74,6 +76,9 @@ const {
deprecate,
exposeInterface,
} = require('internal/util');
const {
validateInteger,
} = require('internal/validators');
const {
exiting_aliased_Uint32Array,
getHiddenValue,
Expand Down Expand Up @@ -103,12 +108,6 @@ process.domain = null;
process._exiting = false;

{
const warnIntegerCoercionDeprecation = deprecate(
() => {},
'Implicit coercion to integer for exit code is deprecated.',
'DEP0164'
);

let exitCode;

ObjectDefineProperty(process, 'exitCode', {
Expand All @@ -117,8 +116,13 @@ process._exiting = false;
return exitCode;
},
set(code) {
if (perThreadSetup.isDeprecatedExitCode(code)) {
warnIntegerCoercionDeprecation();
if (code !== null && code !== undefined) {
let value = code;
if (typeof code === 'string' && code !== '' &&
NumberIsNaN((value = Number(code)))) {
value = code;
}
validateInteger(value, 'code');
}
exitCode = code;
},
Expand Down
38 changes: 2 additions & 36 deletions lib/internal/process/per_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ const {
ArrayPrototypeSplice,
BigUint64Array,
Float64Array,
Number,
NumberIsInteger,
NumberMAX_SAFE_INTEGER,
NumberMIN_SAFE_INTEGER,
ObjectFreeze,
ObjectDefineProperty,
ReflectApply,
Expand Down Expand Up @@ -183,25 +180,12 @@ function wrapProcessMethods(binding) {

memoryUsage.rss = rss;

const { deprecate } = require('internal/util');
const warnIntegerCoercionDeprecationSync = deprecate(
() => {},
'Implicit coercion to integer for exit code is deprecated.',
'DEP0164',
true
);

function exit(code) {
process.off('exit', handleProcessExit);

if (isDeprecatedExitCode(code)) {
// Emit the deprecation warning synchronously since deprecation warning is
// generally emitted in a next tick but we have no next tick timing here.
warnIntegerCoercionDeprecationSync();
}

if (code || code === 0)
if (arguments.length !== 0) {
process.exitCode = code;
}

if (!process._exiting) {
process._exiting = true;
Expand Down Expand Up @@ -424,23 +408,6 @@ function toggleTraceCategoryState(asyncHooksEnabled) {
}
}

function isDeprecatedExitCode(code) {
if (code !== null && code !== undefined) {
const value =
typeof code === 'string' && code !== '' ? Number(code) : code;
// Check if the value is an integer.
if (
typeof value !== 'number' ||
!NumberIsInteger(value) ||
value < NumberMIN_SAFE_INTEGER ||
value > NumberMAX_SAFE_INTEGER
) {
return true;
}
}
return false;
}

module.exports = {
toggleTraceCategoryState,
assert,
Expand All @@ -449,5 +416,4 @@ module.exports = {
hrtime,
hrtimeBigInt,
refreshHrtimeBuffer,
isDeprecatedExitCode,
};
100 changes: 0 additions & 100 deletions test/parallel/test-process-exit-code-deprecation.js

This file was deleted.

Loading

0 comments on commit 2d0d997

Please sign in to comment.