fix test-set-process-debug-port.js (#19662)

Co-authored-by: Ciro Spaciari <ciro.spaciari@gmail.com>
This commit is contained in:
Dylan Conway
2025-05-15 21:38:50 -07:00
committed by GitHub
parent 6bbdbcef33
commit 3983010835
2 changed files with 76 additions and 18 deletions

View File

@@ -3310,15 +3310,11 @@ static JSValue constructFeatures(VM& vm, JSObject* processObject)
return object;
}
static int _debugPort;
static uint16_t debugPort;
JSC_DEFINE_CUSTOM_GETTER(processDebugPort, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::PropertyName))
{
if (_debugPort == 0) {
_debugPort = 9229;
}
return JSC::JSValue::encode(jsNumber(_debugPort));
return JSC::JSValue::encode(jsNumber(debugPort));
}
JSC_DEFINE_CUSTOM_SETTER(setProcessDebugPort, (JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue thisValue, JSC::EncodedJSValue encodedValue, JSC::PropertyName))
@@ -3327,21 +3323,19 @@ JSC_DEFINE_CUSTOM_SETTER(setProcessDebugPort, (JSC::JSGlobalObject * globalObjec
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue value = JSValue::decode(encodedValue);
if (!value.isInt32AsAnyInt()) {
throwNodeRangeError(globalObject, scope, "debugPort must be 0 or in range 1024 to 65535"_s);
double port = value.toNumber(globalObject);
RETURN_IF_EXCEPTION(scope, {});
if (std::isnan(port) || std::isinf(port)) {
port = 0;
}
if ((port != 0 && port < 1024) || port > 65535) {
throwNodeRangeError(globalObject, scope, "process.debugPort must be 0 or in range 1024 to 65535"_s);
return false;
}
int port = value.toInt32(globalObject);
if (port != 0) {
if (port < 1024 || port > 65535) {
throwNodeRangeError(globalObject, scope, "debugPort must be 0 or in range 1024 to 65535"_s);
return false;
}
}
_debugPort = port;
debugPort = floor(port);
return true;
}

View File

@@ -0,0 +1,64 @@
'use strict';
const common = require('../common');
common.skipIfInspectorDisabled();
const { isMainThread } = require('worker_threads');
if (!isMainThread) {
common.skip('This test only works on a main thread');
}
const assert = require('assert');
const kMinPort = 1024;
const kMaxPort = 65535;
function check(value, expected) {
process.debugPort = value;
assert.strictEqual(process.debugPort, expected);
}
// Expected usage with numbers.
check(0, 0);
check(kMinPort, kMinPort);
check(kMinPort + 1, kMinPort + 1);
check(kMaxPort - 1, kMaxPort - 1);
check(kMaxPort, kMaxPort);
// Numeric strings coerce.
check('0', 0);
check(`${kMinPort}`, kMinPort);
check(`${kMinPort + 1}`, kMinPort + 1);
check(`${kMaxPort - 1}`, kMaxPort - 1);
check(`${kMaxPort}`, kMaxPort);
// Most other values are coerced to 0.
check('', 0);
check(false, 0);
check(NaN, 0);
check(Infinity, 0);
check(-Infinity, 0);
check(function() {}, 0);
check({}, 0);
check([], 0);
// Symbols do not coerce.
assert.throws(() => {
process.debugPort = Symbol();
}, /^TypeError: Cannot convert a symbol to a number$/);
// Verify port bounds checking.
[
true,
-1,
1,
kMinPort - 1,
kMaxPort + 1,
'-1',
'1',
`${kMinPort - 1}`,
`${kMaxPort + 1}`,
].forEach((value) => {
assert.throws(() => {
process.debugPort = value;
}, /^RangeError: process\.debugPort must be 0 or in range 1024 to 65535$/);
});