11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci// This files contains process bootstrappers that can be 41cb0ef41Sopenharmony_ci// run when setting up each thread, including the main 51cb0ef41Sopenharmony_ci// thread and the worker threads. 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst { 81cb0ef41Sopenharmony_ci ArrayPrototypeEvery, 91cb0ef41Sopenharmony_ci ArrayPrototypeForEach, 101cb0ef41Sopenharmony_ci ArrayPrototypeIncludes, 111cb0ef41Sopenharmony_ci ArrayPrototypeMap, 121cb0ef41Sopenharmony_ci ArrayPrototypePush, 131cb0ef41Sopenharmony_ci ArrayPrototypeSplice, 141cb0ef41Sopenharmony_ci BigUint64Array, 151cb0ef41Sopenharmony_ci Float64Array, 161cb0ef41Sopenharmony_ci NumberMAX_SAFE_INTEGER, 171cb0ef41Sopenharmony_ci ObjectFreeze, 181cb0ef41Sopenharmony_ci ObjectDefineProperty, 191cb0ef41Sopenharmony_ci ReflectApply, 201cb0ef41Sopenharmony_ci RegExpPrototypeExec, 211cb0ef41Sopenharmony_ci SafeArrayIterator, 221cb0ef41Sopenharmony_ci Set, 231cb0ef41Sopenharmony_ci SetPrototypeEntries, 241cb0ef41Sopenharmony_ci SetPrototypeValues, 251cb0ef41Sopenharmony_ci StringPrototypeEndsWith, 261cb0ef41Sopenharmony_ci StringPrototypeReplace, 271cb0ef41Sopenharmony_ci StringPrototypeSlice, 281cb0ef41Sopenharmony_ci StringPrototypeStartsWith, 291cb0ef41Sopenharmony_ci Symbol, 301cb0ef41Sopenharmony_ci SymbolIterator, 311cb0ef41Sopenharmony_ci Uint32Array, 321cb0ef41Sopenharmony_ci} = primordials; 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ciconst { 351cb0ef41Sopenharmony_ci errnoException, 361cb0ef41Sopenharmony_ci codes: { 371cb0ef41Sopenharmony_ci ERR_ASSERTION, 381cb0ef41Sopenharmony_ci ERR_INVALID_ARG_TYPE, 391cb0ef41Sopenharmony_ci ERR_INVALID_ARG_VALUE, 401cb0ef41Sopenharmony_ci ERR_OUT_OF_RANGE, 411cb0ef41Sopenharmony_ci ERR_UNKNOWN_SIGNAL, 421cb0ef41Sopenharmony_ci }, 431cb0ef41Sopenharmony_ci} = require('internal/errors'); 441cb0ef41Sopenharmony_ciconst format = require('internal/util/inspect').format; 451cb0ef41Sopenharmony_ciconst { 461cb0ef41Sopenharmony_ci validateArray, 471cb0ef41Sopenharmony_ci validateNumber, 481cb0ef41Sopenharmony_ci validateObject, 491cb0ef41Sopenharmony_ci} = require('internal/validators'); 501cb0ef41Sopenharmony_ciconst constants = internalBinding('constants').os.signals; 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ciconst kInternal = Symbol('internal properties'); 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_cifunction assert(x, msg) { 551cb0ef41Sopenharmony_ci if (!x) throw new ERR_ASSERTION(msg || 'assertion error'); 561cb0ef41Sopenharmony_ci} 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ciconst binding = internalBinding('process_methods'); 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_cilet hrValues; 611cb0ef41Sopenharmony_cilet hrBigintValues; 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_cifunction refreshHrtimeBuffer() { 641cb0ef41Sopenharmony_ci // The 3 entries filled in by the original process.hrtime contains 651cb0ef41Sopenharmony_ci // the upper/lower 32 bits of the second part of the value, 661cb0ef41Sopenharmony_ci // and the remaining nanoseconds of the value. 671cb0ef41Sopenharmony_ci hrValues = new Uint32Array(binding.hrtimeBuffer); 681cb0ef41Sopenharmony_ci // Use a BigUint64Array in the closure because this is actually a bit 691cb0ef41Sopenharmony_ci // faster than simply returning a BigInt from C++ in V8 7.1. 701cb0ef41Sopenharmony_ci hrBigintValues = new BigUint64Array(binding.hrtimeBuffer, 0, 1); 711cb0ef41Sopenharmony_ci} 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci// Create the buffers. 741cb0ef41Sopenharmony_cirefreshHrtimeBuffer(); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_cifunction hrtime(time) { 771cb0ef41Sopenharmony_ci binding.hrtime(); 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci if (time !== undefined) { 801cb0ef41Sopenharmony_ci validateArray(time, 'time'); 811cb0ef41Sopenharmony_ci if (time.length !== 2) { 821cb0ef41Sopenharmony_ci throw new ERR_OUT_OF_RANGE('time', 2, time.length); 831cb0ef41Sopenharmony_ci } 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci const sec = (hrValues[0] * 0x100000000 + hrValues[1]) - time[0]; 861cb0ef41Sopenharmony_ci const nsec = hrValues[2] - time[1]; 871cb0ef41Sopenharmony_ci const needsBorrow = nsec < 0; 881cb0ef41Sopenharmony_ci return [needsBorrow ? sec - 1 : sec, needsBorrow ? nsec + 1e9 : nsec]; 891cb0ef41Sopenharmony_ci } 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci return [ 921cb0ef41Sopenharmony_ci hrValues[0] * 0x100000000 + hrValues[1], 931cb0ef41Sopenharmony_ci hrValues[2], 941cb0ef41Sopenharmony_ci ]; 951cb0ef41Sopenharmony_ci} 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_cifunction hrtimeBigInt() { 981cb0ef41Sopenharmony_ci binding.hrtimeBigInt(); 991cb0ef41Sopenharmony_ci return hrBigintValues[0]; 1001cb0ef41Sopenharmony_ci} 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_cifunction nop() {} 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_ci// The execution of this function itself should not cause any side effects. 1051cb0ef41Sopenharmony_cifunction wrapProcessMethods(binding) { 1061cb0ef41Sopenharmony_ci const { 1071cb0ef41Sopenharmony_ci cpuUsage: _cpuUsage, 1081cb0ef41Sopenharmony_ci memoryUsage: _memoryUsage, 1091cb0ef41Sopenharmony_ci rss, 1101cb0ef41Sopenharmony_ci resourceUsage: _resourceUsage, 1111cb0ef41Sopenharmony_ci } = binding; 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci function _rawDebug(...args) { 1141cb0ef41Sopenharmony_ci binding._rawDebug(ReflectApply(format, null, args)); 1151cb0ef41Sopenharmony_ci } 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci // Create the argument array that will be passed to the native function. 1181cb0ef41Sopenharmony_ci const cpuValues = new Float64Array(2); 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci // Replace the native function with the JS version that calls the native 1211cb0ef41Sopenharmony_ci // function. 1221cb0ef41Sopenharmony_ci function cpuUsage(prevValue) { 1231cb0ef41Sopenharmony_ci // If a previous value was passed in, ensure it has the correct shape. 1241cb0ef41Sopenharmony_ci if (prevValue) { 1251cb0ef41Sopenharmony_ci if (!previousValueIsValid(prevValue.user)) { 1261cb0ef41Sopenharmony_ci validateObject(prevValue, 'prevValue'); 1271cb0ef41Sopenharmony_ci 1281cb0ef41Sopenharmony_ci validateNumber(prevValue.user, 'prevValue.user'); 1291cb0ef41Sopenharmony_ci throw new ERR_INVALID_ARG_VALUE.RangeError('prevValue.user', 1301cb0ef41Sopenharmony_ci prevValue.user); 1311cb0ef41Sopenharmony_ci } 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ci if (!previousValueIsValid(prevValue.system)) { 1341cb0ef41Sopenharmony_ci validateNumber(prevValue.system, 'prevValue.system'); 1351cb0ef41Sopenharmony_ci throw new ERR_INVALID_ARG_VALUE.RangeError('prevValue.system', 1361cb0ef41Sopenharmony_ci prevValue.system); 1371cb0ef41Sopenharmony_ci } 1381cb0ef41Sopenharmony_ci } 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ci // Call the native function to get the current values. 1411cb0ef41Sopenharmony_ci _cpuUsage(cpuValues); 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_ci // If a previous value was passed in, return diff of current from previous. 1441cb0ef41Sopenharmony_ci if (prevValue) { 1451cb0ef41Sopenharmony_ci return { 1461cb0ef41Sopenharmony_ci user: cpuValues[0] - prevValue.user, 1471cb0ef41Sopenharmony_ci system: cpuValues[1] - prevValue.system, 1481cb0ef41Sopenharmony_ci }; 1491cb0ef41Sopenharmony_ci } 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ci // If no previous value passed in, return current value. 1521cb0ef41Sopenharmony_ci return { 1531cb0ef41Sopenharmony_ci user: cpuValues[0], 1541cb0ef41Sopenharmony_ci system: cpuValues[1], 1551cb0ef41Sopenharmony_ci }; 1561cb0ef41Sopenharmony_ci } 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ci // Ensure that a previously passed in value is valid. Currently, the native 1591cb0ef41Sopenharmony_ci // implementation always returns numbers <= Number.MAX_SAFE_INTEGER. 1601cb0ef41Sopenharmony_ci function previousValueIsValid(num) { 1611cb0ef41Sopenharmony_ci return typeof num === 'number' && 1621cb0ef41Sopenharmony_ci num <= NumberMAX_SAFE_INTEGER && 1631cb0ef41Sopenharmony_ci num >= 0; 1641cb0ef41Sopenharmony_ci } 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ci const memValues = new Float64Array(5); 1671cb0ef41Sopenharmony_ci function memoryUsage() { 1681cb0ef41Sopenharmony_ci _memoryUsage(memValues); 1691cb0ef41Sopenharmony_ci return { 1701cb0ef41Sopenharmony_ci rss: memValues[0], 1711cb0ef41Sopenharmony_ci heapTotal: memValues[1], 1721cb0ef41Sopenharmony_ci heapUsed: memValues[2], 1731cb0ef41Sopenharmony_ci external: memValues[3], 1741cb0ef41Sopenharmony_ci arrayBuffers: memValues[4], 1751cb0ef41Sopenharmony_ci }; 1761cb0ef41Sopenharmony_ci } 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ci memoryUsage.rss = rss; 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ci function exit(code) { 1811cb0ef41Sopenharmony_ci const { 1821cb0ef41Sopenharmony_ci handleProcessExit, 1831cb0ef41Sopenharmony_ci } = require('internal/modules/esm/handle_process_exit'); 1841cb0ef41Sopenharmony_ci process.off('exit', handleProcessExit); 1851cb0ef41Sopenharmony_ci 1861cb0ef41Sopenharmony_ci if (code || code === 0) 1871cb0ef41Sopenharmony_ci process.exitCode = code; 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci if (!process._exiting) { 1901cb0ef41Sopenharmony_ci process._exiting = true; 1911cb0ef41Sopenharmony_ci process.emit('exit', process.exitCode || 0); 1921cb0ef41Sopenharmony_ci } 1931cb0ef41Sopenharmony_ci // FIXME(joyeecheung): This is an undocumented API that gets monkey-patched 1941cb0ef41Sopenharmony_ci // in the user land. Either document it, or deprecate it in favor of a 1951cb0ef41Sopenharmony_ci // better public alternative. 1961cb0ef41Sopenharmony_ci process.reallyExit(process.exitCode || 0); 1971cb0ef41Sopenharmony_ci 1981cb0ef41Sopenharmony_ci // If this is a worker, v8::Isolate::TerminateExecution() is called above. 1991cb0ef41Sopenharmony_ci // That function spoofs the stack pointer to cause the stack guard 2001cb0ef41Sopenharmony_ci // check to throw the termination exception. Because v8 performs 2011cb0ef41Sopenharmony_ci // stack guard check upon every function call, we give it a chance. 2021cb0ef41Sopenharmony_ci // 2031cb0ef41Sopenharmony_ci // Without this, user code after `process.exit()` would take effect. 2041cb0ef41Sopenharmony_ci // test/parallel/test-worker-voluntarily-exit-followed-by-addition.js 2051cb0ef41Sopenharmony_ci // test/parallel/test-worker-voluntarily-exit-followed-by-throw.js 2061cb0ef41Sopenharmony_ci nop(); 2071cb0ef41Sopenharmony_ci } 2081cb0ef41Sopenharmony_ci 2091cb0ef41Sopenharmony_ci function kill(pid, sig) { 2101cb0ef41Sopenharmony_ci let err; 2111cb0ef41Sopenharmony_ci 2121cb0ef41Sopenharmony_ci // eslint-disable-next-line eqeqeq 2131cb0ef41Sopenharmony_ci if (pid != (pid | 0)) { 2141cb0ef41Sopenharmony_ci throw new ERR_INVALID_ARG_TYPE('pid', 'number', pid); 2151cb0ef41Sopenharmony_ci } 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ci // Preserve null signal 2181cb0ef41Sopenharmony_ci if (sig === (sig | 0)) { 2191cb0ef41Sopenharmony_ci // XXX(joyeecheung): we have to use process._kill here because 2201cb0ef41Sopenharmony_ci // it's monkey-patched by tests. 2211cb0ef41Sopenharmony_ci err = process._kill(pid, sig); 2221cb0ef41Sopenharmony_ci } else { 2231cb0ef41Sopenharmony_ci sig = sig || 'SIGTERM'; 2241cb0ef41Sopenharmony_ci if (constants[sig]) { 2251cb0ef41Sopenharmony_ci err = process._kill(pid, constants[sig]); 2261cb0ef41Sopenharmony_ci } else { 2271cb0ef41Sopenharmony_ci throw new ERR_UNKNOWN_SIGNAL(sig); 2281cb0ef41Sopenharmony_ci } 2291cb0ef41Sopenharmony_ci } 2301cb0ef41Sopenharmony_ci 2311cb0ef41Sopenharmony_ci if (err) 2321cb0ef41Sopenharmony_ci throw errnoException(err, 'kill'); 2331cb0ef41Sopenharmony_ci 2341cb0ef41Sopenharmony_ci return true; 2351cb0ef41Sopenharmony_ci } 2361cb0ef41Sopenharmony_ci 2371cb0ef41Sopenharmony_ci const resourceValues = new Float64Array(16); 2381cb0ef41Sopenharmony_ci function resourceUsage() { 2391cb0ef41Sopenharmony_ci _resourceUsage(resourceValues); 2401cb0ef41Sopenharmony_ci return { 2411cb0ef41Sopenharmony_ci userCPUTime: resourceValues[0], 2421cb0ef41Sopenharmony_ci systemCPUTime: resourceValues[1], 2431cb0ef41Sopenharmony_ci maxRSS: resourceValues[2], 2441cb0ef41Sopenharmony_ci sharedMemorySize: resourceValues[3], 2451cb0ef41Sopenharmony_ci unsharedDataSize: resourceValues[4], 2461cb0ef41Sopenharmony_ci unsharedStackSize: resourceValues[5], 2471cb0ef41Sopenharmony_ci minorPageFault: resourceValues[6], 2481cb0ef41Sopenharmony_ci majorPageFault: resourceValues[7], 2491cb0ef41Sopenharmony_ci swappedOut: resourceValues[8], 2501cb0ef41Sopenharmony_ci fsRead: resourceValues[9], 2511cb0ef41Sopenharmony_ci fsWrite: resourceValues[10], 2521cb0ef41Sopenharmony_ci ipcSent: resourceValues[11], 2531cb0ef41Sopenharmony_ci ipcReceived: resourceValues[12], 2541cb0ef41Sopenharmony_ci signalsCount: resourceValues[13], 2551cb0ef41Sopenharmony_ci voluntaryContextSwitches: resourceValues[14], 2561cb0ef41Sopenharmony_ci involuntaryContextSwitches: resourceValues[15], 2571cb0ef41Sopenharmony_ci }; 2581cb0ef41Sopenharmony_ci } 2591cb0ef41Sopenharmony_ci 2601cb0ef41Sopenharmony_ci 2611cb0ef41Sopenharmony_ci return { 2621cb0ef41Sopenharmony_ci _rawDebug, 2631cb0ef41Sopenharmony_ci cpuUsage, 2641cb0ef41Sopenharmony_ci resourceUsage, 2651cb0ef41Sopenharmony_ci memoryUsage, 2661cb0ef41Sopenharmony_ci kill, 2671cb0ef41Sopenharmony_ci exit, 2681cb0ef41Sopenharmony_ci }; 2691cb0ef41Sopenharmony_ci} 2701cb0ef41Sopenharmony_ci 2711cb0ef41Sopenharmony_ciconst replaceUnderscoresRegex = /_/g; 2721cb0ef41Sopenharmony_ciconst leadingDashesRegex = /^--?/; 2731cb0ef41Sopenharmony_ciconst trailingValuesRegex = /=.*$/; 2741cb0ef41Sopenharmony_ci 2751cb0ef41Sopenharmony_ci// This builds the initial process.allowedNodeEnvironmentFlags 2761cb0ef41Sopenharmony_ci// from data in the config binding. 2771cb0ef41Sopenharmony_cifunction buildAllowedFlags() { 2781cb0ef41Sopenharmony_ci const { 2791cb0ef41Sopenharmony_ci envSettings: { kAllowedInEnvvar }, 2801cb0ef41Sopenharmony_ci types: { kBoolean }, 2811cb0ef41Sopenharmony_ci } = internalBinding('options'); 2821cb0ef41Sopenharmony_ci const { options, aliases } = require('internal/options'); 2831cb0ef41Sopenharmony_ci 2841cb0ef41Sopenharmony_ci const allowedNodeEnvironmentFlags = []; 2851cb0ef41Sopenharmony_ci for (const { 0: name, 1: info } of options) { 2861cb0ef41Sopenharmony_ci if (info.envVarSettings === kAllowedInEnvvar) { 2871cb0ef41Sopenharmony_ci ArrayPrototypePush(allowedNodeEnvironmentFlags, name); 2881cb0ef41Sopenharmony_ci if (info.type === kBoolean) { 2891cb0ef41Sopenharmony_ci const negatedName = `--no-${name.slice(2)}`; 2901cb0ef41Sopenharmony_ci ArrayPrototypePush(allowedNodeEnvironmentFlags, negatedName); 2911cb0ef41Sopenharmony_ci } 2921cb0ef41Sopenharmony_ci } 2931cb0ef41Sopenharmony_ci } 2941cb0ef41Sopenharmony_ci 2951cb0ef41Sopenharmony_ci function isAccepted(to) { 2961cb0ef41Sopenharmony_ci if (!StringPrototypeStartsWith(to, '-') || to === '--') return true; 2971cb0ef41Sopenharmony_ci const recursiveExpansion = aliases.get(to); 2981cb0ef41Sopenharmony_ci if (recursiveExpansion) { 2991cb0ef41Sopenharmony_ci if (recursiveExpansion[0] === to) 3001cb0ef41Sopenharmony_ci ArrayPrototypeSplice(recursiveExpansion, 0, 1); 3011cb0ef41Sopenharmony_ci return ArrayPrototypeEvery(recursiveExpansion, isAccepted); 3021cb0ef41Sopenharmony_ci } 3031cb0ef41Sopenharmony_ci return options.get(to).envVarSettings === kAllowedInEnvvar; 3041cb0ef41Sopenharmony_ci } 3051cb0ef41Sopenharmony_ci for (const { 0: from, 1: expansion } of aliases) { 3061cb0ef41Sopenharmony_ci if (ArrayPrototypeEvery(expansion, isAccepted)) { 3071cb0ef41Sopenharmony_ci let canonical = from; 3081cb0ef41Sopenharmony_ci if (StringPrototypeEndsWith(canonical, '=')) 3091cb0ef41Sopenharmony_ci canonical = StringPrototypeSlice(canonical, 0, canonical.length - 1); 3101cb0ef41Sopenharmony_ci if (StringPrototypeEndsWith(canonical, ' <arg>')) 3111cb0ef41Sopenharmony_ci canonical = StringPrototypeSlice(canonical, 0, canonical.length - 4); 3121cb0ef41Sopenharmony_ci ArrayPrototypePush(allowedNodeEnvironmentFlags, canonical); 3131cb0ef41Sopenharmony_ci } 3141cb0ef41Sopenharmony_ci } 3151cb0ef41Sopenharmony_ci 3161cb0ef41Sopenharmony_ci const trimLeadingDashes = 3171cb0ef41Sopenharmony_ci (flag) => StringPrototypeReplace(flag, leadingDashesRegex, ''); 3181cb0ef41Sopenharmony_ci 3191cb0ef41Sopenharmony_ci // Save these for comparison against flags provided to 3201cb0ef41Sopenharmony_ci // process.allowedNodeEnvironmentFlags.has() which lack leading dashes. 3211cb0ef41Sopenharmony_ci const nodeFlags = ArrayPrototypeMap(allowedNodeEnvironmentFlags, 3221cb0ef41Sopenharmony_ci trimLeadingDashes); 3231cb0ef41Sopenharmony_ci 3241cb0ef41Sopenharmony_ci class NodeEnvironmentFlagsSet extends Set { 3251cb0ef41Sopenharmony_ci constructor(array) { 3261cb0ef41Sopenharmony_ci super(); 3271cb0ef41Sopenharmony_ci this[kInternal] = { array }; 3281cb0ef41Sopenharmony_ci } 3291cb0ef41Sopenharmony_ci 3301cb0ef41Sopenharmony_ci add() { 3311cb0ef41Sopenharmony_ci // No-op, `Set` API compatible 3321cb0ef41Sopenharmony_ci return this; 3331cb0ef41Sopenharmony_ci } 3341cb0ef41Sopenharmony_ci 3351cb0ef41Sopenharmony_ci delete() { 3361cb0ef41Sopenharmony_ci // No-op, `Set` API compatible 3371cb0ef41Sopenharmony_ci return false; 3381cb0ef41Sopenharmony_ci } 3391cb0ef41Sopenharmony_ci 3401cb0ef41Sopenharmony_ci clear() { 3411cb0ef41Sopenharmony_ci // No-op, `Set` API compatible 3421cb0ef41Sopenharmony_ci } 3431cb0ef41Sopenharmony_ci 3441cb0ef41Sopenharmony_ci has(key) { 3451cb0ef41Sopenharmony_ci // This will return `true` based on various possible 3461cb0ef41Sopenharmony_ci // permutations of a flag, including present/missing leading 3471cb0ef41Sopenharmony_ci // dash(es) and/or underscores-for-dashes. 3481cb0ef41Sopenharmony_ci // Strips any values after `=`, inclusive. 3491cb0ef41Sopenharmony_ci // TODO(addaleax): It might be more flexible to run the option parser 3501cb0ef41Sopenharmony_ci // on a dummy option set and see whether it rejects the argument or 3511cb0ef41Sopenharmony_ci // not. 3521cb0ef41Sopenharmony_ci if (typeof key === 'string') { 3531cb0ef41Sopenharmony_ci key = StringPrototypeReplace(key, replaceUnderscoresRegex, '-'); 3541cb0ef41Sopenharmony_ci if (RegExpPrototypeExec(leadingDashesRegex, key) !== null) { 3551cb0ef41Sopenharmony_ci key = StringPrototypeReplace(key, trailingValuesRegex, ''); 3561cb0ef41Sopenharmony_ci return ArrayPrototypeIncludes(this[kInternal].array, key); 3571cb0ef41Sopenharmony_ci } 3581cb0ef41Sopenharmony_ci return ArrayPrototypeIncludes(nodeFlags, key); 3591cb0ef41Sopenharmony_ci } 3601cb0ef41Sopenharmony_ci return false; 3611cb0ef41Sopenharmony_ci } 3621cb0ef41Sopenharmony_ci 3631cb0ef41Sopenharmony_ci entries() { 3641cb0ef41Sopenharmony_ci this[kInternal].set ??= 3651cb0ef41Sopenharmony_ci new Set(new SafeArrayIterator(this[kInternal].array)); 3661cb0ef41Sopenharmony_ci return SetPrototypeEntries(this[kInternal].set); 3671cb0ef41Sopenharmony_ci } 3681cb0ef41Sopenharmony_ci 3691cb0ef41Sopenharmony_ci forEach(callback, thisArg = undefined) { 3701cb0ef41Sopenharmony_ci ArrayPrototypeForEach( 3711cb0ef41Sopenharmony_ci this[kInternal].array, 3721cb0ef41Sopenharmony_ci (v) => ReflectApply(callback, thisArg, [v, v, this]), 3731cb0ef41Sopenharmony_ci ); 3741cb0ef41Sopenharmony_ci } 3751cb0ef41Sopenharmony_ci 3761cb0ef41Sopenharmony_ci get size() { 3771cb0ef41Sopenharmony_ci return this[kInternal].array.length; 3781cb0ef41Sopenharmony_ci } 3791cb0ef41Sopenharmony_ci 3801cb0ef41Sopenharmony_ci values() { 3811cb0ef41Sopenharmony_ci this[kInternal].set ??= 3821cb0ef41Sopenharmony_ci new Set(new SafeArrayIterator(this[kInternal].array)); 3831cb0ef41Sopenharmony_ci return SetPrototypeValues(this[kInternal].set); 3841cb0ef41Sopenharmony_ci } 3851cb0ef41Sopenharmony_ci } 3861cb0ef41Sopenharmony_ci const flagSetValues = NodeEnvironmentFlagsSet.prototype.values; 3871cb0ef41Sopenharmony_ci ObjectDefineProperty(NodeEnvironmentFlagsSet.prototype, SymbolIterator, { 3881cb0ef41Sopenharmony_ci __proto__: null, 3891cb0ef41Sopenharmony_ci value: flagSetValues, 3901cb0ef41Sopenharmony_ci }); 3911cb0ef41Sopenharmony_ci ObjectDefineProperty(NodeEnvironmentFlagsSet.prototype, 'keys', { 3921cb0ef41Sopenharmony_ci __proto__: null, 3931cb0ef41Sopenharmony_ci value: flagSetValues, 3941cb0ef41Sopenharmony_ci }); 3951cb0ef41Sopenharmony_ci 3961cb0ef41Sopenharmony_ci ObjectFreeze(NodeEnvironmentFlagsSet.prototype.constructor); 3971cb0ef41Sopenharmony_ci ObjectFreeze(NodeEnvironmentFlagsSet.prototype); 3981cb0ef41Sopenharmony_ci 3991cb0ef41Sopenharmony_ci return ObjectFreeze(new NodeEnvironmentFlagsSet( 4001cb0ef41Sopenharmony_ci allowedNodeEnvironmentFlags, 4011cb0ef41Sopenharmony_ci )); 4021cb0ef41Sopenharmony_ci} 4031cb0ef41Sopenharmony_ci 4041cb0ef41Sopenharmony_ci// Lazy load internal/trace_events_async_hooks only if the async_hooks 4051cb0ef41Sopenharmony_ci// trace event category is enabled. 4061cb0ef41Sopenharmony_cilet traceEventsAsyncHook; 4071cb0ef41Sopenharmony_ci// Dynamically enable/disable the traceEventsAsyncHook 4081cb0ef41Sopenharmony_cifunction toggleTraceCategoryState(asyncHooksEnabled) { 4091cb0ef41Sopenharmony_ci if (asyncHooksEnabled) { 4101cb0ef41Sopenharmony_ci if (!traceEventsAsyncHook) { 4111cb0ef41Sopenharmony_ci traceEventsAsyncHook = 4121cb0ef41Sopenharmony_ci require('internal/trace_events_async_hooks').createHook(); 4131cb0ef41Sopenharmony_ci } 4141cb0ef41Sopenharmony_ci traceEventsAsyncHook.enable(); 4151cb0ef41Sopenharmony_ci } else if (traceEventsAsyncHook) { 4161cb0ef41Sopenharmony_ci traceEventsAsyncHook.disable(); 4171cb0ef41Sopenharmony_ci } 4181cb0ef41Sopenharmony_ci} 4191cb0ef41Sopenharmony_ci 4201cb0ef41Sopenharmony_cimodule.exports = { 4211cb0ef41Sopenharmony_ci toggleTraceCategoryState, 4221cb0ef41Sopenharmony_ci assert, 4231cb0ef41Sopenharmony_ci buildAllowedFlags, 4241cb0ef41Sopenharmony_ci wrapProcessMethods, 4251cb0ef41Sopenharmony_ci hrtime, 4261cb0ef41Sopenharmony_ci hrtimeBigInt, 4271cb0ef41Sopenharmony_ci refreshHrtimeBuffer, 4281cb0ef41Sopenharmony_ci}; 429