11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst {
41cb0ef41Sopenharmony_ci  ArrayBufferPrototypeGetByteLength,
51cb0ef41Sopenharmony_ci  ArrayFrom,
61cb0ef41Sopenharmony_ci  ArrayIsArray,
71cb0ef41Sopenharmony_ci  ArrayPrototypePush,
81cb0ef41Sopenharmony_ci  ArrayPrototypeSlice,
91cb0ef41Sopenharmony_ci  ArrayPrototypeSort,
101cb0ef41Sopenharmony_ci  Error,
111cb0ef41Sopenharmony_ci  FunctionPrototypeCall,
121cb0ef41Sopenharmony_ci  ObjectCreate,
131cb0ef41Sopenharmony_ci  ObjectDefineProperties,
141cb0ef41Sopenharmony_ci  ObjectDefineProperty,
151cb0ef41Sopenharmony_ci  ObjectGetOwnPropertyDescriptor,
161cb0ef41Sopenharmony_ci  ObjectGetOwnPropertyDescriptors,
171cb0ef41Sopenharmony_ci  ObjectGetPrototypeOf,
181cb0ef41Sopenharmony_ci  ObjectFreeze,
191cb0ef41Sopenharmony_ci  ObjectPrototypeHasOwnProperty,
201cb0ef41Sopenharmony_ci  ObjectSetPrototypeOf,
211cb0ef41Sopenharmony_ci  ObjectValues,
221cb0ef41Sopenharmony_ci  Promise,
231cb0ef41Sopenharmony_ci  ReflectApply,
241cb0ef41Sopenharmony_ci  ReflectConstruct,
251cb0ef41Sopenharmony_ci  RegExpPrototypeExec,
261cb0ef41Sopenharmony_ci  RegExpPrototypeGetDotAll,
271cb0ef41Sopenharmony_ci  RegExpPrototypeGetGlobal,
281cb0ef41Sopenharmony_ci  RegExpPrototypeGetHasIndices,
291cb0ef41Sopenharmony_ci  RegExpPrototypeGetIgnoreCase,
301cb0ef41Sopenharmony_ci  RegExpPrototypeGetMultiline,
311cb0ef41Sopenharmony_ci  RegExpPrototypeGetSticky,
321cb0ef41Sopenharmony_ci  RegExpPrototypeGetUnicode,
331cb0ef41Sopenharmony_ci  RegExpPrototypeGetSource,
341cb0ef41Sopenharmony_ci  SafeMap,
351cb0ef41Sopenharmony_ci  SafeSet,
361cb0ef41Sopenharmony_ci  SafeWeakMap,
371cb0ef41Sopenharmony_ci  StringPrototypeReplace,
381cb0ef41Sopenharmony_ci  StringPrototypeToLowerCase,
391cb0ef41Sopenharmony_ci  StringPrototypeToUpperCase,
401cb0ef41Sopenharmony_ci  Symbol,
411cb0ef41Sopenharmony_ci  SymbolFor,
421cb0ef41Sopenharmony_ci  SymbolReplace,
431cb0ef41Sopenharmony_ci  SymbolSplit,
441cb0ef41Sopenharmony_ci} = primordials;
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ciconst {
471cb0ef41Sopenharmony_ci  hideStackFrames,
481cb0ef41Sopenharmony_ci  codes: {
491cb0ef41Sopenharmony_ci    ERR_NO_CRYPTO,
501cb0ef41Sopenharmony_ci    ERR_UNKNOWN_SIGNAL,
511cb0ef41Sopenharmony_ci  },
521cb0ef41Sopenharmony_ci  uvErrmapGet,
531cb0ef41Sopenharmony_ci  overrideStackTrace,
541cb0ef41Sopenharmony_ci} = require('internal/errors');
551cb0ef41Sopenharmony_ciconst { signals } = internalBinding('constants').os;
561cb0ef41Sopenharmony_ciconst {
571cb0ef41Sopenharmony_ci  isArrayBufferDetached: _isArrayBufferDetached,
581cb0ef41Sopenharmony_ci  privateSymbols: {
591cb0ef41Sopenharmony_ci    arrow_message_private_symbol,
601cb0ef41Sopenharmony_ci    decorated_private_symbol,
611cb0ef41Sopenharmony_ci  },
621cb0ef41Sopenharmony_ci  sleep: _sleep,
631cb0ef41Sopenharmony_ci  toUSVString: _toUSVString,
641cb0ef41Sopenharmony_ci} = internalBinding('util');
651cb0ef41Sopenharmony_ciconst { isNativeError } = internalBinding('types');
661cb0ef41Sopenharmony_ciconst { getOptionValue } = require('internal/options');
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciconst noCrypto = !process.versions.openssl;
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ciconst experimentalWarnings = new SafeSet();
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ciconst colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ciconst unpairedSurrogateRe =
751cb0ef41Sopenharmony_ci  /(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/;
761cb0ef41Sopenharmony_cifunction toUSVString(val) {
771cb0ef41Sopenharmony_ci  const str = `${val}`;
781cb0ef41Sopenharmony_ci  // As of V8 5.5, `str.search()` (and `unpairedSurrogateRe[@@search]()`) are
791cb0ef41Sopenharmony_ci  // slower than `unpairedSurrogateRe.exec()`.
801cb0ef41Sopenharmony_ci  const match = RegExpPrototypeExec(unpairedSurrogateRe, str);
811cb0ef41Sopenharmony_ci  if (!match)
821cb0ef41Sopenharmony_ci    return str;
831cb0ef41Sopenharmony_ci  return _toUSVString(str, match.index);
841cb0ef41Sopenharmony_ci}
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_cilet uvBinding;
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_cifunction lazyUv() {
891cb0ef41Sopenharmony_ci  uvBinding ??= internalBinding('uv');
901cb0ef41Sopenharmony_ci  return uvBinding;
911cb0ef41Sopenharmony_ci}
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_cifunction removeColors(str) {
941cb0ef41Sopenharmony_ci  return StringPrototypeReplace(str, colorRegExp, '');
951cb0ef41Sopenharmony_ci}
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_cifunction isError(e) {
981cb0ef41Sopenharmony_ci  // An error could be an instance of Error while not being a native error
991cb0ef41Sopenharmony_ci  // or could be from a different realm and not be instance of Error but still
1001cb0ef41Sopenharmony_ci  // be a native error.
1011cb0ef41Sopenharmony_ci  return isNativeError(e) || e instanceof Error;
1021cb0ef41Sopenharmony_ci}
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci// Keep a list of deprecation codes that have been warned on so we only warn on
1051cb0ef41Sopenharmony_ci// each one once.
1061cb0ef41Sopenharmony_ciconst codesWarned = new SafeSet();
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_cilet validateString;
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_cifunction getDeprecationWarningEmitter(
1111cb0ef41Sopenharmony_ci  code, msg, deprecated, useEmitSync,
1121cb0ef41Sopenharmony_ci  shouldEmitWarning = () => true,
1131cb0ef41Sopenharmony_ci) {
1141cb0ef41Sopenharmony_ci  let warned = false;
1151cb0ef41Sopenharmony_ci  return function() {
1161cb0ef41Sopenharmony_ci    if (!warned && shouldEmitWarning()) {
1171cb0ef41Sopenharmony_ci      warned = true;
1181cb0ef41Sopenharmony_ci      if (code !== undefined) {
1191cb0ef41Sopenharmony_ci        if (!codesWarned.has(code)) {
1201cb0ef41Sopenharmony_ci          const emitWarning = useEmitSync ?
1211cb0ef41Sopenharmony_ci            require('internal/process/warning').emitWarningSync :
1221cb0ef41Sopenharmony_ci            process.emitWarning;
1231cb0ef41Sopenharmony_ci          emitWarning(msg, 'DeprecationWarning', code, deprecated);
1241cb0ef41Sopenharmony_ci          codesWarned.add(code);
1251cb0ef41Sopenharmony_ci        }
1261cb0ef41Sopenharmony_ci      } else {
1271cb0ef41Sopenharmony_ci        process.emitWarning(msg, 'DeprecationWarning', deprecated);
1281cb0ef41Sopenharmony_ci      }
1291cb0ef41Sopenharmony_ci    }
1301cb0ef41Sopenharmony_ci  };
1311cb0ef41Sopenharmony_ci}
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_cifunction isPendingDeprecation() {
1341cb0ef41Sopenharmony_ci  return getOptionValue('--pending-deprecation') &&
1351cb0ef41Sopenharmony_ci    !getOptionValue('--no-deprecation');
1361cb0ef41Sopenharmony_ci}
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci// Internal deprecator for pending --pending-deprecation. This can be invoked
1391cb0ef41Sopenharmony_ci// at snapshot building time as the warning permission is only queried at
1401cb0ef41Sopenharmony_ci// run time.
1411cb0ef41Sopenharmony_cifunction pendingDeprecate(fn, msg, code) {
1421cb0ef41Sopenharmony_ci  const emitDeprecationWarning = getDeprecationWarningEmitter(
1431cb0ef41Sopenharmony_ci    code, msg, deprecated, false, isPendingDeprecation,
1441cb0ef41Sopenharmony_ci  );
1451cb0ef41Sopenharmony_ci  function deprecated(...args) {
1461cb0ef41Sopenharmony_ci    emitDeprecationWarning();
1471cb0ef41Sopenharmony_ci    return ReflectApply(fn, this, args);
1481cb0ef41Sopenharmony_ci  }
1491cb0ef41Sopenharmony_ci  return deprecated;
1501cb0ef41Sopenharmony_ci}
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci// Mark that a method should not be used.
1531cb0ef41Sopenharmony_ci// Returns a modified function which warns once by default.
1541cb0ef41Sopenharmony_ci// If --no-deprecation is set, then it is a no-op.
1551cb0ef41Sopenharmony_cifunction deprecate(fn, msg, code, useEmitSync) {
1561cb0ef41Sopenharmony_ci  if (process.noDeprecation === true) {
1571cb0ef41Sopenharmony_ci    return fn;
1581cb0ef41Sopenharmony_ci  }
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_ci  // Lazy-load to avoid a circular dependency.
1611cb0ef41Sopenharmony_ci  if (validateString === undefined)
1621cb0ef41Sopenharmony_ci    ({ validateString } = require('internal/validators'));
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci  if (code !== undefined)
1651cb0ef41Sopenharmony_ci    validateString(code, 'code');
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci  const emitDeprecationWarning = getDeprecationWarningEmitter(
1681cb0ef41Sopenharmony_ci    code, msg, deprecated, useEmitSync,
1691cb0ef41Sopenharmony_ci  );
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci  function deprecated(...args) {
1721cb0ef41Sopenharmony_ci    emitDeprecationWarning();
1731cb0ef41Sopenharmony_ci    if (new.target) {
1741cb0ef41Sopenharmony_ci      return ReflectConstruct(fn, args, new.target);
1751cb0ef41Sopenharmony_ci    }
1761cb0ef41Sopenharmony_ci    return ReflectApply(fn, this, args);
1771cb0ef41Sopenharmony_ci  }
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ci  // The wrapper will keep the same prototype as fn to maintain prototype chain
1801cb0ef41Sopenharmony_ci  ObjectSetPrototypeOf(deprecated, fn);
1811cb0ef41Sopenharmony_ci  if (fn.prototype) {
1821cb0ef41Sopenharmony_ci    // Setting this (rather than using Object.setPrototype, as above) ensures
1831cb0ef41Sopenharmony_ci    // that calling the unwrapped constructor gives an instanceof the wrapped
1841cb0ef41Sopenharmony_ci    // constructor.
1851cb0ef41Sopenharmony_ci    deprecated.prototype = fn.prototype;
1861cb0ef41Sopenharmony_ci  }
1871cb0ef41Sopenharmony_ci
1881cb0ef41Sopenharmony_ci  return deprecated;
1891cb0ef41Sopenharmony_ci}
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_cifunction decorateErrorStack(err) {
1921cb0ef41Sopenharmony_ci  if (!(isError(err) && err.stack) || err[decorated_private_symbol])
1931cb0ef41Sopenharmony_ci    return;
1941cb0ef41Sopenharmony_ci
1951cb0ef41Sopenharmony_ci  const arrow = err[arrow_message_private_symbol];
1961cb0ef41Sopenharmony_ci
1971cb0ef41Sopenharmony_ci  if (arrow) {
1981cb0ef41Sopenharmony_ci    err.stack = arrow + err.stack;
1991cb0ef41Sopenharmony_ci    err[decorated_private_symbol] = true;
2001cb0ef41Sopenharmony_ci  }
2011cb0ef41Sopenharmony_ci}
2021cb0ef41Sopenharmony_ci
2031cb0ef41Sopenharmony_cifunction assertCrypto() {
2041cb0ef41Sopenharmony_ci  if (noCrypto)
2051cb0ef41Sopenharmony_ci    throw new ERR_NO_CRYPTO();
2061cb0ef41Sopenharmony_ci}
2071cb0ef41Sopenharmony_ci
2081cb0ef41Sopenharmony_ci// Return undefined if there is no match.
2091cb0ef41Sopenharmony_ci// Move the "slow cases" to a separate function to make sure this function gets
2101cb0ef41Sopenharmony_ci// inlined properly. That prioritizes the common case.
2111cb0ef41Sopenharmony_cifunction normalizeEncoding(enc) {
2121cb0ef41Sopenharmony_ci  if (enc == null || enc === 'utf8' || enc === 'utf-8') return 'utf8';
2131cb0ef41Sopenharmony_ci  return slowCases(enc);
2141cb0ef41Sopenharmony_ci}
2151cb0ef41Sopenharmony_ci
2161cb0ef41Sopenharmony_cifunction slowCases(enc) {
2171cb0ef41Sopenharmony_ci  switch (enc.length) {
2181cb0ef41Sopenharmony_ci    case 4:
2191cb0ef41Sopenharmony_ci      if (enc === 'UTF8') return 'utf8';
2201cb0ef41Sopenharmony_ci      if (enc === 'ucs2' || enc === 'UCS2') return 'utf16le';
2211cb0ef41Sopenharmony_ci      enc = `${enc}`.toLowerCase();
2221cb0ef41Sopenharmony_ci      if (enc === 'utf8') return 'utf8';
2231cb0ef41Sopenharmony_ci      if (enc === 'ucs2') return 'utf16le';
2241cb0ef41Sopenharmony_ci      break;
2251cb0ef41Sopenharmony_ci    case 3:
2261cb0ef41Sopenharmony_ci      if (enc === 'hex' || enc === 'HEX' ||
2271cb0ef41Sopenharmony_ci          `${enc}`.toLowerCase() === 'hex')
2281cb0ef41Sopenharmony_ci        return 'hex';
2291cb0ef41Sopenharmony_ci      break;
2301cb0ef41Sopenharmony_ci    case 5:
2311cb0ef41Sopenharmony_ci      if (enc === 'ascii') return 'ascii';
2321cb0ef41Sopenharmony_ci      if (enc === 'ucs-2') return 'utf16le';
2331cb0ef41Sopenharmony_ci      if (enc === 'UTF-8') return 'utf8';
2341cb0ef41Sopenharmony_ci      if (enc === 'ASCII') return 'ascii';
2351cb0ef41Sopenharmony_ci      if (enc === 'UCS-2') return 'utf16le';
2361cb0ef41Sopenharmony_ci      enc = `${enc}`.toLowerCase();
2371cb0ef41Sopenharmony_ci      if (enc === 'utf-8') return 'utf8';
2381cb0ef41Sopenharmony_ci      if (enc === 'ascii') return 'ascii';
2391cb0ef41Sopenharmony_ci      if (enc === 'ucs-2') return 'utf16le';
2401cb0ef41Sopenharmony_ci      break;
2411cb0ef41Sopenharmony_ci    case 6:
2421cb0ef41Sopenharmony_ci      if (enc === 'base64') return 'base64';
2431cb0ef41Sopenharmony_ci      if (enc === 'latin1' || enc === 'binary') return 'latin1';
2441cb0ef41Sopenharmony_ci      if (enc === 'BASE64') return 'base64';
2451cb0ef41Sopenharmony_ci      if (enc === 'LATIN1' || enc === 'BINARY') return 'latin1';
2461cb0ef41Sopenharmony_ci      enc = `${enc}`.toLowerCase();
2471cb0ef41Sopenharmony_ci      if (enc === 'base64') return 'base64';
2481cb0ef41Sopenharmony_ci      if (enc === 'latin1' || enc === 'binary') return 'latin1';
2491cb0ef41Sopenharmony_ci      break;
2501cb0ef41Sopenharmony_ci    case 7:
2511cb0ef41Sopenharmony_ci      if (enc === 'utf16le' || enc === 'UTF16LE' ||
2521cb0ef41Sopenharmony_ci          `${enc}`.toLowerCase() === 'utf16le')
2531cb0ef41Sopenharmony_ci        return 'utf16le';
2541cb0ef41Sopenharmony_ci      break;
2551cb0ef41Sopenharmony_ci    case 8:
2561cb0ef41Sopenharmony_ci      if (enc === 'utf-16le' || enc === 'UTF-16LE' ||
2571cb0ef41Sopenharmony_ci        `${enc}`.toLowerCase() === 'utf-16le')
2581cb0ef41Sopenharmony_ci        return 'utf16le';
2591cb0ef41Sopenharmony_ci      break;
2601cb0ef41Sopenharmony_ci    case 9:
2611cb0ef41Sopenharmony_ci      if (enc === 'base64url' || enc === 'BASE64URL' ||
2621cb0ef41Sopenharmony_ci          `${enc}`.toLowerCase() === 'base64url')
2631cb0ef41Sopenharmony_ci        return 'base64url';
2641cb0ef41Sopenharmony_ci      break;
2651cb0ef41Sopenharmony_ci    default:
2661cb0ef41Sopenharmony_ci      if (enc === '') return 'utf8';
2671cb0ef41Sopenharmony_ci  }
2681cb0ef41Sopenharmony_ci}
2691cb0ef41Sopenharmony_ci
2701cb0ef41Sopenharmony_cifunction emitExperimentalWarning(feature) {
2711cb0ef41Sopenharmony_ci  if (experimentalWarnings.has(feature)) return;
2721cb0ef41Sopenharmony_ci  const msg = `${feature} is an experimental feature and might change at any time`;
2731cb0ef41Sopenharmony_ci  experimentalWarnings.add(feature);
2741cb0ef41Sopenharmony_ci  process.emitWarning(msg, 'ExperimentalWarning');
2751cb0ef41Sopenharmony_ci}
2761cb0ef41Sopenharmony_ci
2771cb0ef41Sopenharmony_cifunction filterDuplicateStrings(items, low) {
2781cb0ef41Sopenharmony_ci  const map = new SafeMap();
2791cb0ef41Sopenharmony_ci  for (let i = 0; i < items.length; i++) {
2801cb0ef41Sopenharmony_ci    const item = items[i];
2811cb0ef41Sopenharmony_ci    const key = StringPrototypeToLowerCase(item);
2821cb0ef41Sopenharmony_ci    if (low) {
2831cb0ef41Sopenharmony_ci      map.set(key, key);
2841cb0ef41Sopenharmony_ci    } else {
2851cb0ef41Sopenharmony_ci      map.set(key, item);
2861cb0ef41Sopenharmony_ci    }
2871cb0ef41Sopenharmony_ci  }
2881cb0ef41Sopenharmony_ci  return ArrayPrototypeSort(ArrayFrom(map.values()));
2891cb0ef41Sopenharmony_ci}
2901cb0ef41Sopenharmony_ci
2911cb0ef41Sopenharmony_cifunction cachedResult(fn) {
2921cb0ef41Sopenharmony_ci  let result;
2931cb0ef41Sopenharmony_ci  return () => {
2941cb0ef41Sopenharmony_ci    if (result === undefined)
2951cb0ef41Sopenharmony_ci      result = fn();
2961cb0ef41Sopenharmony_ci    return ArrayPrototypeSlice(result);
2971cb0ef41Sopenharmony_ci  };
2981cb0ef41Sopenharmony_ci}
2991cb0ef41Sopenharmony_ci
3001cb0ef41Sopenharmony_ci// Useful for Wrapping an ES6 Class with a constructor Function that
3011cb0ef41Sopenharmony_ci// does not require the new keyword. For instance:
3021cb0ef41Sopenharmony_ci//   class A { constructor(x) {this.x = x;}}
3031cb0ef41Sopenharmony_ci//   const B = createClassWrapper(A);
3041cb0ef41Sopenharmony_ci//   B() instanceof A // true
3051cb0ef41Sopenharmony_ci//   B() instanceof B // true
3061cb0ef41Sopenharmony_cifunction createClassWrapper(type) {
3071cb0ef41Sopenharmony_ci  function fn(...args) {
3081cb0ef41Sopenharmony_ci    return ReflectConstruct(type, args, new.target || type);
3091cb0ef41Sopenharmony_ci  }
3101cb0ef41Sopenharmony_ci  // Mask the wrapper function name and length values
3111cb0ef41Sopenharmony_ci  ObjectDefineProperties(fn, {
3121cb0ef41Sopenharmony_ci    name: { __proto__: null, value: type.name },
3131cb0ef41Sopenharmony_ci    length: { __proto__: null, value: type.length },
3141cb0ef41Sopenharmony_ci  });
3151cb0ef41Sopenharmony_ci  ObjectSetPrototypeOf(fn, type);
3161cb0ef41Sopenharmony_ci  fn.prototype = type.prototype;
3171cb0ef41Sopenharmony_ci  return fn;
3181cb0ef41Sopenharmony_ci}
3191cb0ef41Sopenharmony_ci
3201cb0ef41Sopenharmony_cilet signalsToNamesMapping;
3211cb0ef41Sopenharmony_cifunction getSignalsToNamesMapping() {
3221cb0ef41Sopenharmony_ci  if (signalsToNamesMapping !== undefined)
3231cb0ef41Sopenharmony_ci    return signalsToNamesMapping;
3241cb0ef41Sopenharmony_ci
3251cb0ef41Sopenharmony_ci  signalsToNamesMapping = ObjectCreate(null);
3261cb0ef41Sopenharmony_ci  for (const key in signals) {
3271cb0ef41Sopenharmony_ci    signalsToNamesMapping[signals[key]] = key;
3281cb0ef41Sopenharmony_ci  }
3291cb0ef41Sopenharmony_ci
3301cb0ef41Sopenharmony_ci  return signalsToNamesMapping;
3311cb0ef41Sopenharmony_ci}
3321cb0ef41Sopenharmony_ci
3331cb0ef41Sopenharmony_cifunction convertToValidSignal(signal) {
3341cb0ef41Sopenharmony_ci  if (typeof signal === 'number' && getSignalsToNamesMapping()[signal])
3351cb0ef41Sopenharmony_ci    return signal;
3361cb0ef41Sopenharmony_ci
3371cb0ef41Sopenharmony_ci  if (typeof signal === 'string') {
3381cb0ef41Sopenharmony_ci    const signalName = signals[StringPrototypeToUpperCase(signal)];
3391cb0ef41Sopenharmony_ci    if (signalName) return signalName;
3401cb0ef41Sopenharmony_ci  }
3411cb0ef41Sopenharmony_ci
3421cb0ef41Sopenharmony_ci  throw new ERR_UNKNOWN_SIGNAL(signal);
3431cb0ef41Sopenharmony_ci}
3441cb0ef41Sopenharmony_ci
3451cb0ef41Sopenharmony_cifunction getConstructorOf(obj) {
3461cb0ef41Sopenharmony_ci  while (obj) {
3471cb0ef41Sopenharmony_ci    const descriptor = ObjectGetOwnPropertyDescriptor(obj, 'constructor');
3481cb0ef41Sopenharmony_ci    if (descriptor !== undefined &&
3491cb0ef41Sopenharmony_ci        typeof descriptor.value === 'function' &&
3501cb0ef41Sopenharmony_ci        descriptor.value.name !== '') {
3511cb0ef41Sopenharmony_ci      return descriptor.value;
3521cb0ef41Sopenharmony_ci    }
3531cb0ef41Sopenharmony_ci
3541cb0ef41Sopenharmony_ci    obj = ObjectGetPrototypeOf(obj);
3551cb0ef41Sopenharmony_ci  }
3561cb0ef41Sopenharmony_ci
3571cb0ef41Sopenharmony_ci  return null;
3581cb0ef41Sopenharmony_ci}
3591cb0ef41Sopenharmony_ci
3601cb0ef41Sopenharmony_cilet cachedURL;
3611cb0ef41Sopenharmony_cilet cachedCWD;
3621cb0ef41Sopenharmony_ci
3631cb0ef41Sopenharmony_ci/**
3641cb0ef41Sopenharmony_ci * Get the current working directory while accounting for the possibility that it has been deleted.
3651cb0ef41Sopenharmony_ci * `process.cwd()` can fail if the parent directory is deleted while the process runs.
3661cb0ef41Sopenharmony_ci * @returns {URL} The current working directory or the volume root if it cannot be determined.
3671cb0ef41Sopenharmony_ci */
3681cb0ef41Sopenharmony_cifunction getCWDURL() {
3691cb0ef41Sopenharmony_ci  const { sep } = require('path');
3701cb0ef41Sopenharmony_ci  const { pathToFileURL } = require('internal/url');
3711cb0ef41Sopenharmony_ci
3721cb0ef41Sopenharmony_ci  let cwd;
3731cb0ef41Sopenharmony_ci
3741cb0ef41Sopenharmony_ci  try {
3751cb0ef41Sopenharmony_ci    // The implementation of `process.cwd()` already uses proper cache when it can.
3761cb0ef41Sopenharmony_ci    // It's a relatively cheap call performance-wise for the most common use case.
3771cb0ef41Sopenharmony_ci    cwd = process.cwd();
3781cb0ef41Sopenharmony_ci  } catch {
3791cb0ef41Sopenharmony_ci    cachedURL ??= pathToFileURL(sep);
3801cb0ef41Sopenharmony_ci  }
3811cb0ef41Sopenharmony_ci
3821cb0ef41Sopenharmony_ci  if (cwd != null && cwd !== cachedCWD) {
3831cb0ef41Sopenharmony_ci    cachedURL = pathToFileURL(cwd + sep);
3841cb0ef41Sopenharmony_ci    cachedCWD = cwd;
3851cb0ef41Sopenharmony_ci  }
3861cb0ef41Sopenharmony_ci
3871cb0ef41Sopenharmony_ci  return cachedURL;
3881cb0ef41Sopenharmony_ci}
3891cb0ef41Sopenharmony_ci
3901cb0ef41Sopenharmony_cifunction getSystemErrorName(err) {
3911cb0ef41Sopenharmony_ci  const entry = uvErrmapGet(err);
3921cb0ef41Sopenharmony_ci  return entry ? entry[0] : `Unknown system error ${err}`;
3931cb0ef41Sopenharmony_ci}
3941cb0ef41Sopenharmony_ci
3951cb0ef41Sopenharmony_cifunction getSystemErrorMap() {
3961cb0ef41Sopenharmony_ci  return lazyUv().getErrorMap();
3971cb0ef41Sopenharmony_ci}
3981cb0ef41Sopenharmony_ci
3991cb0ef41Sopenharmony_ciconst kCustomPromisifiedSymbol = SymbolFor('nodejs.util.promisify.custom');
4001cb0ef41Sopenharmony_ciconst kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs');
4011cb0ef41Sopenharmony_ci
4021cb0ef41Sopenharmony_cilet validateFunction;
4031cb0ef41Sopenharmony_ci
4041cb0ef41Sopenharmony_cifunction promisify(original) {
4051cb0ef41Sopenharmony_ci  // Lazy-load to avoid a circular dependency.
4061cb0ef41Sopenharmony_ci  if (validateFunction === undefined)
4071cb0ef41Sopenharmony_ci    ({ validateFunction } = require('internal/validators'));
4081cb0ef41Sopenharmony_ci
4091cb0ef41Sopenharmony_ci  validateFunction(original, 'original');
4101cb0ef41Sopenharmony_ci
4111cb0ef41Sopenharmony_ci  if (original[kCustomPromisifiedSymbol]) {
4121cb0ef41Sopenharmony_ci    const fn = original[kCustomPromisifiedSymbol];
4131cb0ef41Sopenharmony_ci
4141cb0ef41Sopenharmony_ci    validateFunction(fn, 'util.promisify.custom');
4151cb0ef41Sopenharmony_ci
4161cb0ef41Sopenharmony_ci    return ObjectDefineProperty(fn, kCustomPromisifiedSymbol, {
4171cb0ef41Sopenharmony_ci      __proto__: null,
4181cb0ef41Sopenharmony_ci      value: fn, enumerable: false, writable: false, configurable: true,
4191cb0ef41Sopenharmony_ci    });
4201cb0ef41Sopenharmony_ci  }
4211cb0ef41Sopenharmony_ci
4221cb0ef41Sopenharmony_ci  // Names to create an object from in case the callback receives multiple
4231cb0ef41Sopenharmony_ci  // arguments, e.g. ['bytesRead', 'buffer'] for fs.read.
4241cb0ef41Sopenharmony_ci  const argumentNames = original[kCustomPromisifyArgsSymbol];
4251cb0ef41Sopenharmony_ci
4261cb0ef41Sopenharmony_ci  function fn(...args) {
4271cb0ef41Sopenharmony_ci    return new Promise((resolve, reject) => {
4281cb0ef41Sopenharmony_ci      ArrayPrototypePush(args, (err, ...values) => {
4291cb0ef41Sopenharmony_ci        if (err) {
4301cb0ef41Sopenharmony_ci          return reject(err);
4311cb0ef41Sopenharmony_ci        }
4321cb0ef41Sopenharmony_ci        if (argumentNames !== undefined && values.length > 1) {
4331cb0ef41Sopenharmony_ci          const obj = {};
4341cb0ef41Sopenharmony_ci          for (let i = 0; i < argumentNames.length; i++)
4351cb0ef41Sopenharmony_ci            obj[argumentNames[i]] = values[i];
4361cb0ef41Sopenharmony_ci          resolve(obj);
4371cb0ef41Sopenharmony_ci        } else {
4381cb0ef41Sopenharmony_ci          resolve(values[0]);
4391cb0ef41Sopenharmony_ci        }
4401cb0ef41Sopenharmony_ci      });
4411cb0ef41Sopenharmony_ci      ReflectApply(original, this, args);
4421cb0ef41Sopenharmony_ci    });
4431cb0ef41Sopenharmony_ci  }
4441cb0ef41Sopenharmony_ci
4451cb0ef41Sopenharmony_ci  ObjectSetPrototypeOf(fn, ObjectGetPrototypeOf(original));
4461cb0ef41Sopenharmony_ci
4471cb0ef41Sopenharmony_ci  ObjectDefineProperty(fn, kCustomPromisifiedSymbol, {
4481cb0ef41Sopenharmony_ci    __proto__: null,
4491cb0ef41Sopenharmony_ci    value: fn, enumerable: false, writable: false, configurable: true,
4501cb0ef41Sopenharmony_ci  });
4511cb0ef41Sopenharmony_ci
4521cb0ef41Sopenharmony_ci  const descriptors = ObjectGetOwnPropertyDescriptors(original);
4531cb0ef41Sopenharmony_ci  const propertiesValues = ObjectValues(descriptors);
4541cb0ef41Sopenharmony_ci  for (let i = 0; i < propertiesValues.length; i++) {
4551cb0ef41Sopenharmony_ci    // We want to use null-prototype objects to not rely on globally mutable
4561cb0ef41Sopenharmony_ci    // %Object.prototype%.
4571cb0ef41Sopenharmony_ci    ObjectSetPrototypeOf(propertiesValues[i], null);
4581cb0ef41Sopenharmony_ci  }
4591cb0ef41Sopenharmony_ci  return ObjectDefineProperties(fn, descriptors);
4601cb0ef41Sopenharmony_ci}
4611cb0ef41Sopenharmony_ci
4621cb0ef41Sopenharmony_cipromisify.custom = kCustomPromisifiedSymbol;
4631cb0ef41Sopenharmony_ci
4641cb0ef41Sopenharmony_ci// The built-in Array#join is slower in v8 6.0
4651cb0ef41Sopenharmony_cifunction join(output, separator) {
4661cb0ef41Sopenharmony_ci  let str = '';
4671cb0ef41Sopenharmony_ci  if (output.length !== 0) {
4681cb0ef41Sopenharmony_ci    const lastIndex = output.length - 1;
4691cb0ef41Sopenharmony_ci    for (let i = 0; i < lastIndex; i++) {
4701cb0ef41Sopenharmony_ci      // It is faster not to use a template string here
4711cb0ef41Sopenharmony_ci      str += output[i];
4721cb0ef41Sopenharmony_ci      str += separator;
4731cb0ef41Sopenharmony_ci    }
4741cb0ef41Sopenharmony_ci    str += output[lastIndex];
4751cb0ef41Sopenharmony_ci  }
4761cb0ef41Sopenharmony_ci  return str;
4771cb0ef41Sopenharmony_ci}
4781cb0ef41Sopenharmony_ci
4791cb0ef41Sopenharmony_ci// As of V8 6.6, depending on the size of the array, this is anywhere
4801cb0ef41Sopenharmony_ci// between 1.5-10x faster than the two-arg version of Array#splice()
4811cb0ef41Sopenharmony_cifunction spliceOne(list, index) {
4821cb0ef41Sopenharmony_ci  for (; index + 1 < list.length; index++)
4831cb0ef41Sopenharmony_ci    list[index] = list[index + 1];
4841cb0ef41Sopenharmony_ci  list.pop();
4851cb0ef41Sopenharmony_ci}
4861cb0ef41Sopenharmony_ci
4871cb0ef41Sopenharmony_ciconst kNodeModulesRE = /^(.*)[\\/]node_modules[\\/]/;
4881cb0ef41Sopenharmony_ci
4891cb0ef41Sopenharmony_cilet getStructuredStack;
4901cb0ef41Sopenharmony_ci
4911cb0ef41Sopenharmony_cifunction isInsideNodeModules() {
4921cb0ef41Sopenharmony_ci  if (getStructuredStack === undefined) {
4931cb0ef41Sopenharmony_ci    // Lazy-load to avoid a circular dependency.
4941cb0ef41Sopenharmony_ci    const { runInNewContext } = require('vm');
4951cb0ef41Sopenharmony_ci    // Use `runInNewContext()` to get something tamper-proof and
4961cb0ef41Sopenharmony_ci    // side-effect-free. Since this is currently only used for a deprecated API,
4971cb0ef41Sopenharmony_ci    // the perf implications should be okay.
4981cb0ef41Sopenharmony_ci    getStructuredStack = runInNewContext(`(function() {
4991cb0ef41Sopenharmony_ci      try { Error.stackTraceLimit = Infinity; } catch {}
5001cb0ef41Sopenharmony_ci      return function structuredStack() {
5011cb0ef41Sopenharmony_ci        const e = new Error();
5021cb0ef41Sopenharmony_ci        overrideStackTrace.set(e, (err, trace) => trace);
5031cb0ef41Sopenharmony_ci        return e.stack;
5041cb0ef41Sopenharmony_ci      };
5051cb0ef41Sopenharmony_ci    })()`, { overrideStackTrace }, { filename: 'structured-stack' });
5061cb0ef41Sopenharmony_ci  }
5071cb0ef41Sopenharmony_ci
5081cb0ef41Sopenharmony_ci  const stack = getStructuredStack();
5091cb0ef41Sopenharmony_ci
5101cb0ef41Sopenharmony_ci  // Iterate over all stack frames and look for the first one not coming
5111cb0ef41Sopenharmony_ci  // from inside Node.js itself:
5121cb0ef41Sopenharmony_ci  if (ArrayIsArray(stack)) {
5131cb0ef41Sopenharmony_ci    for (const frame of stack) {
5141cb0ef41Sopenharmony_ci      const filename = frame.getFileName();
5151cb0ef41Sopenharmony_ci      // If a filename does not start with / or contain \,
5161cb0ef41Sopenharmony_ci      // it's likely from Node.js core.
5171cb0ef41Sopenharmony_ci      if (RegExpPrototypeExec(/^\/|\\/, filename) === null)
5181cb0ef41Sopenharmony_ci        continue;
5191cb0ef41Sopenharmony_ci      return RegExpPrototypeExec(kNodeModulesRE, filename) !== null;
5201cb0ef41Sopenharmony_ci    }
5211cb0ef41Sopenharmony_ci  }
5221cb0ef41Sopenharmony_ci  return false;
5231cb0ef41Sopenharmony_ci}
5241cb0ef41Sopenharmony_ci
5251cb0ef41Sopenharmony_cifunction once(callback) {
5261cb0ef41Sopenharmony_ci  let called = false;
5271cb0ef41Sopenharmony_ci  return function(...args) {
5281cb0ef41Sopenharmony_ci    if (called) return;
5291cb0ef41Sopenharmony_ci    called = true;
5301cb0ef41Sopenharmony_ci    return ReflectApply(callback, this, args);
5311cb0ef41Sopenharmony_ci  };
5321cb0ef41Sopenharmony_ci}
5331cb0ef41Sopenharmony_ci
5341cb0ef41Sopenharmony_cilet validateUint32;
5351cb0ef41Sopenharmony_ci
5361cb0ef41Sopenharmony_cifunction sleep(msec) {
5371cb0ef41Sopenharmony_ci  // Lazy-load to avoid a circular dependency.
5381cb0ef41Sopenharmony_ci  if (validateUint32 === undefined)
5391cb0ef41Sopenharmony_ci    ({ validateUint32 } = require('internal/validators'));
5401cb0ef41Sopenharmony_ci
5411cb0ef41Sopenharmony_ci  validateUint32(msec, 'msec');
5421cb0ef41Sopenharmony_ci  _sleep(msec);
5431cb0ef41Sopenharmony_ci}
5441cb0ef41Sopenharmony_ci
5451cb0ef41Sopenharmony_cifunction createDeferredPromise() {
5461cb0ef41Sopenharmony_ci  let resolve;
5471cb0ef41Sopenharmony_ci  let reject;
5481cb0ef41Sopenharmony_ci  const promise = new Promise((res, rej) => {
5491cb0ef41Sopenharmony_ci    resolve = res;
5501cb0ef41Sopenharmony_ci    reject = rej;
5511cb0ef41Sopenharmony_ci  });
5521cb0ef41Sopenharmony_ci
5531cb0ef41Sopenharmony_ci  return { promise, resolve, reject };
5541cb0ef41Sopenharmony_ci}
5551cb0ef41Sopenharmony_ci
5561cb0ef41Sopenharmony_ci// https://heycam.github.io/webidl/#define-the-operations
5571cb0ef41Sopenharmony_cifunction defineOperation(target, name, method) {
5581cb0ef41Sopenharmony_ci  ObjectDefineProperty(target, name, {
5591cb0ef41Sopenharmony_ci    __proto__: null,
5601cb0ef41Sopenharmony_ci    writable: true,
5611cb0ef41Sopenharmony_ci    enumerable: true,
5621cb0ef41Sopenharmony_ci    configurable: true,
5631cb0ef41Sopenharmony_ci    value: method,
5641cb0ef41Sopenharmony_ci  });
5651cb0ef41Sopenharmony_ci}
5661cb0ef41Sopenharmony_ci
5671cb0ef41Sopenharmony_ci// https://heycam.github.io/webidl/#es-interfaces
5681cb0ef41Sopenharmony_cifunction exposeInterface(target, name, interfaceObject) {
5691cb0ef41Sopenharmony_ci  ObjectDefineProperty(target, name, {
5701cb0ef41Sopenharmony_ci    __proto__: null,
5711cb0ef41Sopenharmony_ci    writable: true,
5721cb0ef41Sopenharmony_ci    enumerable: false,
5731cb0ef41Sopenharmony_ci    configurable: true,
5741cb0ef41Sopenharmony_ci    value: interfaceObject,
5751cb0ef41Sopenharmony_ci  });
5761cb0ef41Sopenharmony_ci}
5771cb0ef41Sopenharmony_ci
5781cb0ef41Sopenharmony_cifunction defineLazyProperties(target, id, keys, enumerable = true) {
5791cb0ef41Sopenharmony_ci  const descriptors = { __proto__: null };
5801cb0ef41Sopenharmony_ci  let mod;
5811cb0ef41Sopenharmony_ci  for (let i = 0; i < keys.length; i++) {
5821cb0ef41Sopenharmony_ci    const key = keys[i];
5831cb0ef41Sopenharmony_ci    let lazyLoadedValue;
5841cb0ef41Sopenharmony_ci    function set(value) {
5851cb0ef41Sopenharmony_ci      ObjectDefineProperty(target, key, {
5861cb0ef41Sopenharmony_ci        __proto__: null,
5871cb0ef41Sopenharmony_ci        writable: true,
5881cb0ef41Sopenharmony_ci        value,
5891cb0ef41Sopenharmony_ci      });
5901cb0ef41Sopenharmony_ci    }
5911cb0ef41Sopenharmony_ci    ObjectDefineProperty(set, 'name', {
5921cb0ef41Sopenharmony_ci      __proto__: null,
5931cb0ef41Sopenharmony_ci      value: `set ${key}`,
5941cb0ef41Sopenharmony_ci    });
5951cb0ef41Sopenharmony_ci    function get() {
5961cb0ef41Sopenharmony_ci      mod ??= require(id);
5971cb0ef41Sopenharmony_ci      if (lazyLoadedValue === undefined) {
5981cb0ef41Sopenharmony_ci        lazyLoadedValue = mod[key];
5991cb0ef41Sopenharmony_ci        set(lazyLoadedValue);
6001cb0ef41Sopenharmony_ci      }
6011cb0ef41Sopenharmony_ci      return lazyLoadedValue;
6021cb0ef41Sopenharmony_ci    }
6031cb0ef41Sopenharmony_ci    ObjectDefineProperty(get, 'name', {
6041cb0ef41Sopenharmony_ci      __proto__: null,
6051cb0ef41Sopenharmony_ci      value: `get ${key}`,
6061cb0ef41Sopenharmony_ci    });
6071cb0ef41Sopenharmony_ci    descriptors[key] = {
6081cb0ef41Sopenharmony_ci      __proto__: null,
6091cb0ef41Sopenharmony_ci      configurable: true,
6101cb0ef41Sopenharmony_ci      enumerable,
6111cb0ef41Sopenharmony_ci      get,
6121cb0ef41Sopenharmony_ci      set,
6131cb0ef41Sopenharmony_ci    };
6141cb0ef41Sopenharmony_ci  }
6151cb0ef41Sopenharmony_ci  ObjectDefineProperties(target, descriptors);
6161cb0ef41Sopenharmony_ci}
6171cb0ef41Sopenharmony_ci
6181cb0ef41Sopenharmony_cifunction defineReplaceableLazyAttribute(target, id, keys, writable = true) {
6191cb0ef41Sopenharmony_ci  let mod;
6201cb0ef41Sopenharmony_ci  for (let i = 0; i < keys.length; i++) {
6211cb0ef41Sopenharmony_ci    const key = keys[i];
6221cb0ef41Sopenharmony_ci    let value;
6231cb0ef41Sopenharmony_ci    let setterCalled = false;
6241cb0ef41Sopenharmony_ci
6251cb0ef41Sopenharmony_ci    function get() {
6261cb0ef41Sopenharmony_ci      if (setterCalled) {
6271cb0ef41Sopenharmony_ci        return value;
6281cb0ef41Sopenharmony_ci      }
6291cb0ef41Sopenharmony_ci      mod ??= require(id);
6301cb0ef41Sopenharmony_ci      value ??= mod[key];
6311cb0ef41Sopenharmony_ci      return value;
6321cb0ef41Sopenharmony_ci    }
6331cb0ef41Sopenharmony_ci
6341cb0ef41Sopenharmony_ci    ObjectDefineProperty(get, 'name', {
6351cb0ef41Sopenharmony_ci      __proto__: null,
6361cb0ef41Sopenharmony_ci      value: `get ${key}`,
6371cb0ef41Sopenharmony_ci    });
6381cb0ef41Sopenharmony_ci
6391cb0ef41Sopenharmony_ci    function set(val) {
6401cb0ef41Sopenharmony_ci      setterCalled = true;
6411cb0ef41Sopenharmony_ci      value = val;
6421cb0ef41Sopenharmony_ci    }
6431cb0ef41Sopenharmony_ci    ObjectDefineProperty(set, 'name', {
6441cb0ef41Sopenharmony_ci      __proto__: null,
6451cb0ef41Sopenharmony_ci      value: `set ${key}`,
6461cb0ef41Sopenharmony_ci    });
6471cb0ef41Sopenharmony_ci
6481cb0ef41Sopenharmony_ci    ObjectDefineProperty(target, key, {
6491cb0ef41Sopenharmony_ci      __proto__: null,
6501cb0ef41Sopenharmony_ci      enumerable: true,
6511cb0ef41Sopenharmony_ci      configurable: true,
6521cb0ef41Sopenharmony_ci      get,
6531cb0ef41Sopenharmony_ci      set: writable ? set : undefined,
6541cb0ef41Sopenharmony_ci    });
6551cb0ef41Sopenharmony_ci  }
6561cb0ef41Sopenharmony_ci}
6571cb0ef41Sopenharmony_ci
6581cb0ef41Sopenharmony_cifunction exposeLazyInterfaces(target, id, keys) {
6591cb0ef41Sopenharmony_ci  defineLazyProperties(target, id, keys, false);
6601cb0ef41Sopenharmony_ci}
6611cb0ef41Sopenharmony_ci
6621cb0ef41Sopenharmony_cilet _DOMException;
6631cb0ef41Sopenharmony_ciconst lazyDOMExceptionClass = () => {
6641cb0ef41Sopenharmony_ci  _DOMException ??= internalBinding('messaging').DOMException;
6651cb0ef41Sopenharmony_ci  return _DOMException;
6661cb0ef41Sopenharmony_ci};
6671cb0ef41Sopenharmony_ci
6681cb0ef41Sopenharmony_ciconst lazyDOMException = hideStackFrames((message, name) => {
6691cb0ef41Sopenharmony_ci  _DOMException ??= internalBinding('messaging').DOMException;
6701cb0ef41Sopenharmony_ci  return new _DOMException(message, name);
6711cb0ef41Sopenharmony_ci});
6721cb0ef41Sopenharmony_ci
6731cb0ef41Sopenharmony_ciconst kEnumerableProperty = ObjectCreate(null);
6741cb0ef41Sopenharmony_cikEnumerableProperty.enumerable = true;
6751cb0ef41Sopenharmony_ciObjectFreeze(kEnumerableProperty);
6761cb0ef41Sopenharmony_ci
6771cb0ef41Sopenharmony_ciconst kEmptyObject = ObjectFreeze(ObjectCreate(null));
6781cb0ef41Sopenharmony_ci
6791cb0ef41Sopenharmony_cifunction filterOwnProperties(source, keys) {
6801cb0ef41Sopenharmony_ci  const filtered = ObjectCreate(null);
6811cb0ef41Sopenharmony_ci  for (let i = 0; i < keys.length; i++) {
6821cb0ef41Sopenharmony_ci    const key = keys[i];
6831cb0ef41Sopenharmony_ci    if (ObjectPrototypeHasOwnProperty(source, key)) {
6841cb0ef41Sopenharmony_ci      filtered[key] = source[key];
6851cb0ef41Sopenharmony_ci    }
6861cb0ef41Sopenharmony_ci  }
6871cb0ef41Sopenharmony_ci
6881cb0ef41Sopenharmony_ci  return filtered;
6891cb0ef41Sopenharmony_ci}
6901cb0ef41Sopenharmony_ci
6911cb0ef41Sopenharmony_ci/**
6921cb0ef41Sopenharmony_ci * Mimics `obj[key] = value` but ignoring potential prototype inheritance.
6931cb0ef41Sopenharmony_ci * @param {any} obj
6941cb0ef41Sopenharmony_ci * @param {string} key
6951cb0ef41Sopenharmony_ci * @param {any} value
6961cb0ef41Sopenharmony_ci * @returns {any}
6971cb0ef41Sopenharmony_ci */
6981cb0ef41Sopenharmony_cifunction setOwnProperty(obj, key, value) {
6991cb0ef41Sopenharmony_ci  return ObjectDefineProperty(obj, key, {
7001cb0ef41Sopenharmony_ci    __proto__: null,
7011cb0ef41Sopenharmony_ci    configurable: true,
7021cb0ef41Sopenharmony_ci    enumerable: true,
7031cb0ef41Sopenharmony_ci    value,
7041cb0ef41Sopenharmony_ci    writable: true,
7051cb0ef41Sopenharmony_ci  });
7061cb0ef41Sopenharmony_ci}
7071cb0ef41Sopenharmony_ci
7081cb0ef41Sopenharmony_cilet internalGlobal;
7091cb0ef41Sopenharmony_cifunction getInternalGlobal() {
7101cb0ef41Sopenharmony_ci  if (internalGlobal == null) {
7111cb0ef41Sopenharmony_ci    // Lazy-load to avoid a circular dependency.
7121cb0ef41Sopenharmony_ci    const { runInNewContext } = require('vm');
7131cb0ef41Sopenharmony_ci    internalGlobal = runInNewContext('this', undefined, { contextName: 'internal' });
7141cb0ef41Sopenharmony_ci  }
7151cb0ef41Sopenharmony_ci  return internalGlobal;
7161cb0ef41Sopenharmony_ci}
7171cb0ef41Sopenharmony_ci
7181cb0ef41Sopenharmony_cifunction SideEffectFreeRegExpPrototypeExec(regex, string) {
7191cb0ef41Sopenharmony_ci  const { RegExp: RegExpFromAnotherRealm } = getInternalGlobal();
7201cb0ef41Sopenharmony_ci  return FunctionPrototypeCall(RegExpFromAnotherRealm.prototype.exec, regex, string);
7211cb0ef41Sopenharmony_ci}
7221cb0ef41Sopenharmony_ci
7231cb0ef41Sopenharmony_ciconst crossRelmRegexes = new SafeWeakMap();
7241cb0ef41Sopenharmony_cifunction getCrossRelmRegex(regex) {
7251cb0ef41Sopenharmony_ci  const cached = crossRelmRegexes.get(regex);
7261cb0ef41Sopenharmony_ci  if (cached) return cached;
7271cb0ef41Sopenharmony_ci
7281cb0ef41Sopenharmony_ci  let flagString = '';
7291cb0ef41Sopenharmony_ci  if (RegExpPrototypeGetHasIndices(regex)) flagString += 'd';
7301cb0ef41Sopenharmony_ci  if (RegExpPrototypeGetGlobal(regex)) flagString += 'g';
7311cb0ef41Sopenharmony_ci  if (RegExpPrototypeGetIgnoreCase(regex)) flagString += 'i';
7321cb0ef41Sopenharmony_ci  if (RegExpPrototypeGetMultiline(regex)) flagString += 'm';
7331cb0ef41Sopenharmony_ci  if (RegExpPrototypeGetDotAll(regex)) flagString += 's';
7341cb0ef41Sopenharmony_ci  if (RegExpPrototypeGetUnicode(regex)) flagString += 'u';
7351cb0ef41Sopenharmony_ci  if (RegExpPrototypeGetSticky(regex)) flagString += 'y';
7361cb0ef41Sopenharmony_ci
7371cb0ef41Sopenharmony_ci  const { RegExp: RegExpFromAnotherRealm } = getInternalGlobal();
7381cb0ef41Sopenharmony_ci  const crossRelmRegex = new RegExpFromAnotherRealm(RegExpPrototypeGetSource(regex), flagString);
7391cb0ef41Sopenharmony_ci  crossRelmRegexes.set(regex, crossRelmRegex);
7401cb0ef41Sopenharmony_ci  return crossRelmRegex;
7411cb0ef41Sopenharmony_ci}
7421cb0ef41Sopenharmony_ci
7431cb0ef41Sopenharmony_cifunction SideEffectFreeRegExpPrototypeSymbolReplace(regex, string, replacement) {
7441cb0ef41Sopenharmony_ci  return getCrossRelmRegex(regex)[SymbolReplace](string, replacement);
7451cb0ef41Sopenharmony_ci}
7461cb0ef41Sopenharmony_ci
7471cb0ef41Sopenharmony_cifunction SideEffectFreeRegExpPrototypeSymbolSplit(regex, string, limit = undefined) {
7481cb0ef41Sopenharmony_ci  return getCrossRelmRegex(regex)[SymbolSplit](string, limit);
7491cb0ef41Sopenharmony_ci}
7501cb0ef41Sopenharmony_ci
7511cb0ef41Sopenharmony_ci
7521cb0ef41Sopenharmony_cifunction isArrayBufferDetached(value) {
7531cb0ef41Sopenharmony_ci  if (ArrayBufferPrototypeGetByteLength(value) === 0) {
7541cb0ef41Sopenharmony_ci    return _isArrayBufferDetached(value);
7551cb0ef41Sopenharmony_ci  }
7561cb0ef41Sopenharmony_ci
7571cb0ef41Sopenharmony_ci  return false;
7581cb0ef41Sopenharmony_ci}
7591cb0ef41Sopenharmony_ci
7601cb0ef41Sopenharmony_ci/**
7611cb0ef41Sopenharmony_ci * Helper function to lazy-load an initialize-once value.
7621cb0ef41Sopenharmony_ci * @template T Return value of initializer
7631cb0ef41Sopenharmony_ci * @param {()=>T} initializer Initializer of the lazily loaded value.
7641cb0ef41Sopenharmony_ci * @returns {()=>T}
7651cb0ef41Sopenharmony_ci */
7661cb0ef41Sopenharmony_cifunction getLazy(initializer) {
7671cb0ef41Sopenharmony_ci  let value;
7681cb0ef41Sopenharmony_ci  let initialized = false;
7691cb0ef41Sopenharmony_ci  return function() {
7701cb0ef41Sopenharmony_ci    if (initialized === false) {
7711cb0ef41Sopenharmony_ci      value = initializer();
7721cb0ef41Sopenharmony_ci      initialized = true;
7731cb0ef41Sopenharmony_ci    }
7741cb0ef41Sopenharmony_ci    return value;
7751cb0ef41Sopenharmony_ci  };
7761cb0ef41Sopenharmony_ci}
7771cb0ef41Sopenharmony_ci
7781cb0ef41Sopenharmony_ci// Setup user-facing NODE_V8_COVERAGE environment variable that writes
7791cb0ef41Sopenharmony_ci// ScriptCoverage objects to a specified directory.
7801cb0ef41Sopenharmony_cifunction setupCoverageHooks(dir) {
7811cb0ef41Sopenharmony_ci  const cwd = require('internal/process/execution').tryGetCwd();
7821cb0ef41Sopenharmony_ci  const { resolve } = require('path');
7831cb0ef41Sopenharmony_ci  const coverageDirectory = resolve(cwd, dir);
7841cb0ef41Sopenharmony_ci  const { sourceMapCacheToObject } =
7851cb0ef41Sopenharmony_ci    require('internal/source_map/source_map_cache');
7861cb0ef41Sopenharmony_ci
7871cb0ef41Sopenharmony_ci  if (process.features.inspector) {
7881cb0ef41Sopenharmony_ci    internalBinding('profiler').setCoverageDirectory(coverageDirectory);
7891cb0ef41Sopenharmony_ci    internalBinding('profiler').setSourceMapCacheGetter(sourceMapCacheToObject);
7901cb0ef41Sopenharmony_ci  } else {
7911cb0ef41Sopenharmony_ci    process.emitWarning('The inspector is disabled, ' +
7921cb0ef41Sopenharmony_ci                        'coverage could not be collected',
7931cb0ef41Sopenharmony_ci                        'Warning');
7941cb0ef41Sopenharmony_ci    return '';
7951cb0ef41Sopenharmony_ci  }
7961cb0ef41Sopenharmony_ci  return coverageDirectory;
7971cb0ef41Sopenharmony_ci}
7981cb0ef41Sopenharmony_ci
7991cb0ef41Sopenharmony_cimodule.exports = {
8001cb0ef41Sopenharmony_ci  getLazy,
8011cb0ef41Sopenharmony_ci  assertCrypto,
8021cb0ef41Sopenharmony_ci  cachedResult,
8031cb0ef41Sopenharmony_ci  convertToValidSignal,
8041cb0ef41Sopenharmony_ci  createClassWrapper,
8051cb0ef41Sopenharmony_ci  createDeferredPromise,
8061cb0ef41Sopenharmony_ci  decorateErrorStack,
8071cb0ef41Sopenharmony_ci  defineOperation,
8081cb0ef41Sopenharmony_ci  defineLazyProperties,
8091cb0ef41Sopenharmony_ci  defineReplaceableLazyAttribute,
8101cb0ef41Sopenharmony_ci  deprecate,
8111cb0ef41Sopenharmony_ci  emitExperimentalWarning,
8121cb0ef41Sopenharmony_ci  exposeInterface,
8131cb0ef41Sopenharmony_ci  exposeLazyInterfaces,
8141cb0ef41Sopenharmony_ci  filterDuplicateStrings,
8151cb0ef41Sopenharmony_ci  filterOwnProperties,
8161cb0ef41Sopenharmony_ci  getConstructorOf,
8171cb0ef41Sopenharmony_ci  getCWDURL,
8181cb0ef41Sopenharmony_ci  getInternalGlobal,
8191cb0ef41Sopenharmony_ci  getSystemErrorMap,
8201cb0ef41Sopenharmony_ci  getSystemErrorName,
8211cb0ef41Sopenharmony_ci  isArrayBufferDetached,
8221cb0ef41Sopenharmony_ci  isError,
8231cb0ef41Sopenharmony_ci  isInsideNodeModules,
8241cb0ef41Sopenharmony_ci  join,
8251cb0ef41Sopenharmony_ci  lazyDOMException,
8261cb0ef41Sopenharmony_ci  lazyDOMExceptionClass,
8271cb0ef41Sopenharmony_ci  normalizeEncoding,
8281cb0ef41Sopenharmony_ci  once,
8291cb0ef41Sopenharmony_ci  promisify,
8301cb0ef41Sopenharmony_ci  SideEffectFreeRegExpPrototypeExec,
8311cb0ef41Sopenharmony_ci  SideEffectFreeRegExpPrototypeSymbolReplace,
8321cb0ef41Sopenharmony_ci  SideEffectFreeRegExpPrototypeSymbolSplit,
8331cb0ef41Sopenharmony_ci  sleep,
8341cb0ef41Sopenharmony_ci  spliceOne,
8351cb0ef41Sopenharmony_ci  setupCoverageHooks,
8361cb0ef41Sopenharmony_ci  toUSVString,
8371cb0ef41Sopenharmony_ci  removeColors,
8381cb0ef41Sopenharmony_ci
8391cb0ef41Sopenharmony_ci  // Symbol used to customize promisify conversion
8401cb0ef41Sopenharmony_ci  customPromisifyArgs: kCustomPromisifyArgsSymbol,
8411cb0ef41Sopenharmony_ci
8421cb0ef41Sopenharmony_ci  // Symbol used to provide a custom inspect function for an object as an
8431cb0ef41Sopenharmony_ci  // alternative to using 'inspect'
8441cb0ef41Sopenharmony_ci  customInspectSymbol: SymbolFor('nodejs.util.inspect.custom'),
8451cb0ef41Sopenharmony_ci
8461cb0ef41Sopenharmony_ci  // Used by the buffer module to capture an internal reference to the
8471cb0ef41Sopenharmony_ci  // default isEncoding implementation, just in case userland overrides it.
8481cb0ef41Sopenharmony_ci  kIsEncodingSymbol: Symbol('kIsEncodingSymbol'),
8491cb0ef41Sopenharmony_ci  kVmBreakFirstLineSymbol: Symbol('kVmBreakFirstLineSymbol'),
8501cb0ef41Sopenharmony_ci
8511cb0ef41Sopenharmony_ci  kEmptyObject,
8521cb0ef41Sopenharmony_ci  kEnumerableProperty,
8531cb0ef41Sopenharmony_ci  setOwnProperty,
8541cb0ef41Sopenharmony_ci  pendingDeprecate,
8551cb0ef41Sopenharmony_ci};
856