Skip to content

Commit ada2d05

Browse files
authored
process: runtime deprecate coercion to integer in process.exit()
This is a follow up of doc-only deprecation #43738. Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: #44711 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 24d255f commit ada2d05

6 files changed

Lines changed: 171 additions & 5 deletions

File tree

doc/api/deprecations.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3175,6 +3175,9 @@ thing instead.
31753175

31763176
<!-- YAML
31773177
changes:
3178+
- version: REPLACEME
3179+
pr-url: https://github.com/nodejs/node/pull/44711
3180+
description: Runtime deprecation.
31783181
- version: v18.10.0
31793182
pr-url: https://github.com/nodejs/node/pull/44714
31803183
description: Documentation-only deprecation of `process.exitCode` integer
@@ -3187,7 +3190,7 @@ changes:
31873190
coercion.
31883191
-->
31893192

3190-
Type: Documentation-only
3193+
Type: Runtime
31913194

31923195
Values other than `undefined`, `null`, integer numbers, and integer strings
31933196
(e.g., `'1'`) are deprecated as value for the `code` parameter in

lib/internal/bootstrap/node.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,31 @@ process.domain = null;
102102
}
103103
process._exiting = false;
104104

105+
{
106+
const warnIntegerCoercionDeprecation = deprecate(
107+
() => {},
108+
'Implicit coercion to integer for exit code is deprecated.',
109+
'DEP0164'
110+
);
111+
112+
let exitCode;
113+
114+
ObjectDefineProperty(process, 'exitCode', {
115+
__proto__: null,
116+
get() {
117+
return exitCode;
118+
},
119+
set(code) {
120+
if (perThreadSetup.isDeprecatedExitCode(code)) {
121+
warnIntegerCoercionDeprecation();
122+
}
123+
exitCode = code;
124+
},
125+
enumerable: true,
126+
configurable: false,
127+
});
128+
}
129+
105130
// process.config is serialized config.gypi
106131
const nativeModule = internalBinding('builtins');
107132

lib/internal/process/per_thread.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ const {
1313
ArrayPrototypeSplice,
1414
BigUint64Array,
1515
Float64Array,
16+
Number,
17+
NumberIsInteger,
1618
NumberMAX_SAFE_INTEGER,
19+
NumberMIN_SAFE_INTEGER,
1720
ObjectFreeze,
1821
ObjectDefineProperty,
1922
ReflectApply,
@@ -180,9 +183,23 @@ function wrapProcessMethods(binding) {
180183

181184
memoryUsage.rss = rss;
182185

186+
const { deprecate } = require('internal/util');
187+
const warnIntegerCoercionDeprecationSync = deprecate(
188+
() => {},
189+
'Implicit coercion to integer for exit code is deprecated.',
190+
'DEP0164',
191+
true
192+
);
193+
183194
function exit(code) {
184195
process.off('exit', handleProcessExit);
185196

197+
if (isDeprecatedExitCode(code)) {
198+
// Emit the deprecation warning synchronously since deprecation warning is
199+
// generally emitted in a next tick but we have no next tick timing here.
200+
warnIntegerCoercionDeprecationSync();
201+
}
202+
186203
if (code || code === 0)
187204
process.exitCode = code;
188205

@@ -407,6 +424,23 @@ function toggleTraceCategoryState(asyncHooksEnabled) {
407424
}
408425
}
409426

427+
function isDeprecatedExitCode(code) {
428+
if (code !== null && code !== undefined) {
429+
const value =
430+
typeof code === 'string' && code !== '' ? Number(code) : code;
431+
// Check if the value is an integer.
432+
if (
433+
typeof value !== 'number' ||
434+
!NumberIsInteger(value) ||
435+
value < NumberMIN_SAFE_INTEGER ||
436+
value > NumberMAX_SAFE_INTEGER
437+
) {
438+
return true;
439+
}
440+
}
441+
return false;
442+
}
443+
410444
module.exports = {
411445
toggleTraceCategoryState,
412446
assert,
@@ -415,4 +449,5 @@ module.exports = {
415449
hrtime,
416450
hrtimeBigInt,
417451
refreshHrtimeBuffer,
452+
isDeprecatedExitCode,
418453
};