11cb0ef41Sopenharmony_ci// This file is executed in every realm that is created by Node.js, including
21cb0ef41Sopenharmony_ci// the context of main thread, worker threads, and ShadowRealms.
31cb0ef41Sopenharmony_ci// Only per-realm internal states and bindings should be bootstrapped in this
41cb0ef41Sopenharmony_ci// file and no globals should be exposed to the user code.
51cb0ef41Sopenharmony_ci//
61cb0ef41Sopenharmony_ci// This file creates the internal module & binding loaders used by built-in
71cb0ef41Sopenharmony_ci// modules. In contrast, user land modules are loaded using
81cb0ef41Sopenharmony_ci// lib/internal/modules/cjs/loader.js (CommonJS Modules) or
91cb0ef41Sopenharmony_ci// lib/internal/modules/esm/* (ES Modules).
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// This file is compiled and run by node.cc before bootstrap/node.js
121cb0ef41Sopenharmony_ci// was called, therefore the loaders are bootstrapped before we start to
131cb0ef41Sopenharmony_ci// actually bootstrap Node.js. It creates the following objects:
141cb0ef41Sopenharmony_ci//
151cb0ef41Sopenharmony_ci// C++ binding loaders:
161cb0ef41Sopenharmony_ci// - process.binding(): the legacy C++ binding loader, accessible from user land
171cb0ef41Sopenharmony_ci//   because it is an object attached to the global process object.
181cb0ef41Sopenharmony_ci//   These C++ bindings are created using NODE_BUILTIN_MODULE_CONTEXT_AWARE()
191cb0ef41Sopenharmony_ci//   and have their nm_flags set to NM_F_BUILTIN. We do not make any guarantees
201cb0ef41Sopenharmony_ci//   about the stability of these bindings, but still have to take care of
211cb0ef41Sopenharmony_ci//   compatibility issues caused by them from time to time.
221cb0ef41Sopenharmony_ci// - process._linkedBinding(): intended to be used by embedders to add
231cb0ef41Sopenharmony_ci//   additional C++ bindings in their applications. These C++ bindings
241cb0ef41Sopenharmony_ci//   can be created using NODE_BINDING_CONTEXT_AWARE_CPP() with the flag
251cb0ef41Sopenharmony_ci//   NM_F_LINKED.
261cb0ef41Sopenharmony_ci// - internalBinding(): the private internal C++ binding loader, inaccessible
271cb0ef41Sopenharmony_ci//   from user land unless through `require('internal/test/binding')`.
281cb0ef41Sopenharmony_ci//   These C++ bindings are created using NODE_BINDING_CONTEXT_AWARE_INTERNAL()
291cb0ef41Sopenharmony_ci//   and have their nm_flags set to NM_F_INTERNAL.
301cb0ef41Sopenharmony_ci//
311cb0ef41Sopenharmony_ci// Internal JavaScript module loader:
321cb0ef41Sopenharmony_ci// - BuiltinModule: a minimal module system used to load the JavaScript core
331cb0ef41Sopenharmony_ci//   modules found in lib/**/*.js and deps/**/*.js. All core modules are
341cb0ef41Sopenharmony_ci//   compiled into the node binary via node_javascript.cc generated by js2c.py,
351cb0ef41Sopenharmony_ci//   so they can be loaded faster without the cost of I/O. This class makes the
361cb0ef41Sopenharmony_ci//   lib/internal/*, deps/internal/* modules and internalBinding() available by
371cb0ef41Sopenharmony_ci//   default to core modules, and lets the core modules require itself via
381cb0ef41Sopenharmony_ci//   require('internal/bootstrap/realm') even when this file is not written in
391cb0ef41Sopenharmony_ci//   CommonJS style.
401cb0ef41Sopenharmony_ci//
411cb0ef41Sopenharmony_ci// Other objects:
421cb0ef41Sopenharmony_ci// - process.moduleLoadList: an array recording the bindings and the modules
431cb0ef41Sopenharmony_ci//   loaded in the process and the order in which they are loaded.
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci'use strict';
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci// This file is compiled as if it's wrapped in a function with arguments
481cb0ef41Sopenharmony_ci// passed by node::RunBootstrapping()
491cb0ef41Sopenharmony_ci/* global process, getLinkedBinding, getInternalBinding, primordials */
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ciconst {
521cb0ef41Sopenharmony_ci  ArrayFrom,
531cb0ef41Sopenharmony_ci  ArrayPrototypeMap,
541cb0ef41Sopenharmony_ci  ArrayPrototypePush,
551cb0ef41Sopenharmony_ci  ArrayPrototypeSlice,
561cb0ef41Sopenharmony_ci  Error,
571cb0ef41Sopenharmony_ci  ObjectCreate,
581cb0ef41Sopenharmony_ci  ObjectDefineProperty,
591cb0ef41Sopenharmony_ci  ObjectKeys,
601cb0ef41Sopenharmony_ci  ObjectPrototypeHasOwnProperty,
611cb0ef41Sopenharmony_ci  ObjectSetPrototypeOf,
621cb0ef41Sopenharmony_ci  ReflectGet,
631cb0ef41Sopenharmony_ci  SafeMap,
641cb0ef41Sopenharmony_ci  SafeSet,
651cb0ef41Sopenharmony_ci  String,
661cb0ef41Sopenharmony_ci  StringPrototypeSlice,
671cb0ef41Sopenharmony_ci  StringPrototypeStartsWith,
681cb0ef41Sopenharmony_ci  TypeError,
691cb0ef41Sopenharmony_ci} = primordials;
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci// Set up process.moduleLoadList.
721cb0ef41Sopenharmony_ciconst moduleLoadList = [];
731cb0ef41Sopenharmony_ciObjectDefineProperty(process, 'moduleLoadList', {
741cb0ef41Sopenharmony_ci  __proto__: null,
751cb0ef41Sopenharmony_ci  value: moduleLoadList,
761cb0ef41Sopenharmony_ci  configurable: true,
771cb0ef41Sopenharmony_ci  enumerable: true,
781cb0ef41Sopenharmony_ci  writable: false,
791cb0ef41Sopenharmony_ci});
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci// internalBindingAllowlist contains the name of internalBinding modules
831cb0ef41Sopenharmony_ci// that are allowed for access via process.binding()... This is used
841cb0ef41Sopenharmony_ci// to provide a transition path for modules that are being moved over to
851cb0ef41Sopenharmony_ci// internalBinding.
861cb0ef41Sopenharmony_ciconst internalBindingAllowlist = new SafeSet([
871cb0ef41Sopenharmony_ci  'async_wrap',
881cb0ef41Sopenharmony_ci  'buffer',
891cb0ef41Sopenharmony_ci  'cares_wrap',
901cb0ef41Sopenharmony_ci  'config',
911cb0ef41Sopenharmony_ci  'constants',
921cb0ef41Sopenharmony_ci  'contextify',
931cb0ef41Sopenharmony_ci  'crypto',
941cb0ef41Sopenharmony_ci  'fs',
951cb0ef41Sopenharmony_ci  'fs_event_wrap',
961cb0ef41Sopenharmony_ci  'http_parser',
971cb0ef41Sopenharmony_ci  'icu',
981cb0ef41Sopenharmony_ci  'inspector',
991cb0ef41Sopenharmony_ci  'js_stream',
1001cb0ef41Sopenharmony_ci  'natives',
1011cb0ef41Sopenharmony_ci  'os',
1021cb0ef41Sopenharmony_ci  'pipe_wrap',
1031cb0ef41Sopenharmony_ci  'process_wrap',
1041cb0ef41Sopenharmony_ci  'signal_wrap',
1051cb0ef41Sopenharmony_ci  'spawn_sync',
1061cb0ef41Sopenharmony_ci  'stream_wrap',
1071cb0ef41Sopenharmony_ci  'tcp_wrap',
1081cb0ef41Sopenharmony_ci  'tls_wrap',
1091cb0ef41Sopenharmony_ci  'tty_wrap',
1101cb0ef41Sopenharmony_ci  'udp_wrap',
1111cb0ef41Sopenharmony_ci  'url',
1121cb0ef41Sopenharmony_ci  'util',
1131cb0ef41Sopenharmony_ci  'uv',
1141cb0ef41Sopenharmony_ci  'v8',
1151cb0ef41Sopenharmony_ci  'zlib',
1161cb0ef41Sopenharmony_ci]);
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ciconst runtimeDeprecatedList = new SafeSet([
1191cb0ef41Sopenharmony_ci  'async_wrap',
1201cb0ef41Sopenharmony_ci  'crypto',
1211cb0ef41Sopenharmony_ci  'http_parser',
1221cb0ef41Sopenharmony_ci  'signal_wrap',
1231cb0ef41Sopenharmony_ci  'url',
1241cb0ef41Sopenharmony_ci  'v8',
1251cb0ef41Sopenharmony_ci]);
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ciconst legacyWrapperList = new SafeSet([
1281cb0ef41Sopenharmony_ci  'util',
1291cb0ef41Sopenharmony_ci]);
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci// The code bellow assumes that the two lists must not contain any modules
1321cb0ef41Sopenharmony_ci// beginning with "internal/".
1331cb0ef41Sopenharmony_ci// Modules that can only be imported via the node: scheme.
1341cb0ef41Sopenharmony_ciconst schemelessBlockList = new SafeSet([
1351cb0ef41Sopenharmony_ci  'test',
1361cb0ef41Sopenharmony_ci  'test/reporters',
1371cb0ef41Sopenharmony_ci]);
1381cb0ef41Sopenharmony_ci// Modules that will only be enabled at run time.
1391cb0ef41Sopenharmony_ciconst experimentalModuleList = new SafeSet();
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci// Set up process.binding() and process._linkedBinding().
1421cb0ef41Sopenharmony_ci{
1431cb0ef41Sopenharmony_ci  const bindingObj = ObjectCreate(null);
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci  process.binding = function binding(module) {
1461cb0ef41Sopenharmony_ci    module = String(module);
1471cb0ef41Sopenharmony_ci    // Deprecated specific process.binding() modules, but not all, allow
1481cb0ef41Sopenharmony_ci    // selective fallback to internalBinding for the deprecated ones.
1491cb0ef41Sopenharmony_ci    if (internalBindingAllowlist.has(module)) {
1501cb0ef41Sopenharmony_ci      if (runtimeDeprecatedList.has(module)) {
1511cb0ef41Sopenharmony_ci        runtimeDeprecatedList.delete(module);
1521cb0ef41Sopenharmony_ci        process.emitWarning(
1531cb0ef41Sopenharmony_ci          `Access to process.binding('${module}') is deprecated.`,
1541cb0ef41Sopenharmony_ci          'DeprecationWarning',
1551cb0ef41Sopenharmony_ci          'DEP0111');
1561cb0ef41Sopenharmony_ci      }
1571cb0ef41Sopenharmony_ci      if (legacyWrapperList.has(module)) {
1581cb0ef41Sopenharmony_ci        return requireBuiltin('internal/legacy/processbinding')[module]();
1591cb0ef41Sopenharmony_ci      }
1601cb0ef41Sopenharmony_ci      return internalBinding(module);
1611cb0ef41Sopenharmony_ci    }
1621cb0ef41Sopenharmony_ci    // eslint-disable-next-line no-restricted-syntax
1631cb0ef41Sopenharmony_ci    throw new Error(`No such module: ${module}`);
1641cb0ef41Sopenharmony_ci  };
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_ci  process._linkedBinding = function _linkedBinding(module) {
1671cb0ef41Sopenharmony_ci    module = String(module);
1681cb0ef41Sopenharmony_ci    let mod = bindingObj[module];
1691cb0ef41Sopenharmony_ci    if (typeof mod !== 'object')
1701cb0ef41Sopenharmony_ci      mod = bindingObj[module] = getLinkedBinding(module);
1711cb0ef41Sopenharmony_ci    return mod;
1721cb0ef41Sopenharmony_ci  };
1731cb0ef41Sopenharmony_ci}
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci// Set up internalBinding() in the closure.
1761cb0ef41Sopenharmony_ci/**
1771cb0ef41Sopenharmony_ci * @type {InternalBinding}
1781cb0ef41Sopenharmony_ci */
1791cb0ef41Sopenharmony_cilet internalBinding;
1801cb0ef41Sopenharmony_ci{
1811cb0ef41Sopenharmony_ci  const bindingObj = ObjectCreate(null);
1821cb0ef41Sopenharmony_ci  // eslint-disable-next-line no-global-assign
1831cb0ef41Sopenharmony_ci  internalBinding = function internalBinding(module) {
1841cb0ef41Sopenharmony_ci    let mod = bindingObj[module];
1851cb0ef41Sopenharmony_ci    if (typeof mod !== 'object') {
1861cb0ef41Sopenharmony_ci      mod = bindingObj[module] = getInternalBinding(module);
1871cb0ef41Sopenharmony_ci      ArrayPrototypePush(moduleLoadList, `Internal Binding ${module}`);
1881cb0ef41Sopenharmony_ci    }
1891cb0ef41Sopenharmony_ci    return mod;
1901cb0ef41Sopenharmony_ci  };
1911cb0ef41Sopenharmony_ci}
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ciconst selfId = 'internal/bootstrap/realm';
1941cb0ef41Sopenharmony_ciconst {
1951cb0ef41Sopenharmony_ci  builtinIds,
1961cb0ef41Sopenharmony_ci  compileFunction,
1971cb0ef41Sopenharmony_ci  setInternalLoaders,
1981cb0ef41Sopenharmony_ci} = internalBinding('builtins');
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ciconst getOwn = (target, property, receiver) => {
2011cb0ef41Sopenharmony_ci  return ObjectPrototypeHasOwnProperty(target, property) ?
2021cb0ef41Sopenharmony_ci    ReflectGet(target, property, receiver) :
2031cb0ef41Sopenharmony_ci    undefined;
2041cb0ef41Sopenharmony_ci};
2051cb0ef41Sopenharmony_ci
2061cb0ef41Sopenharmony_ciconst publicBuiltinIds = builtinIds
2071cb0ef41Sopenharmony_ci  .filter((id) =>
2081cb0ef41Sopenharmony_ci    !StringPrototypeStartsWith(id, 'internal/') &&
2091cb0ef41Sopenharmony_ci      !experimentalModuleList.has(id),
2101cb0ef41Sopenharmony_ci  );
2111cb0ef41Sopenharmony_ci// Do not expose the loaders to user land even with --expose-internals.
2121cb0ef41Sopenharmony_ciconst internalBuiltinIds = builtinIds
2131cb0ef41Sopenharmony_ci  .filter((id) => StringPrototypeStartsWith(id, 'internal/') && id !== selfId);
2141cb0ef41Sopenharmony_ci
2151cb0ef41Sopenharmony_ci// When --expose-internals is on we'll add the internal builtin ids to these.
2161cb0ef41Sopenharmony_ciconst canBeRequiredByUsersList = new SafeSet(publicBuiltinIds);
2171cb0ef41Sopenharmony_ciconst canBeRequiredByUsersWithoutSchemeList =
2181cb0ef41Sopenharmony_ci  new SafeSet(publicBuiltinIds.filter((id) => !schemelessBlockList.has(id)));
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ci/**
2211cb0ef41Sopenharmony_ci * An internal abstraction for the built-in JavaScript modules of Node.js.
2221cb0ef41Sopenharmony_ci * Be careful not to expose this to user land unless --expose-internals is
2231cb0ef41Sopenharmony_ci * used, in which case there is no compatibility guarantee about this class.
2241cb0ef41Sopenharmony_ci */
2251cb0ef41Sopenharmony_ciclass BuiltinModule {
2261cb0ef41Sopenharmony_ci  /**
2271cb0ef41Sopenharmony_ci   * A map from the module IDs to the module instances.
2281cb0ef41Sopenharmony_ci   * @type {Map<string, BuiltinModule>}
2291cb0ef41Sopenharmony_ci   */
2301cb0ef41Sopenharmony_ci  static map = new SafeMap(
2311cb0ef41Sopenharmony_ci    ArrayPrototypeMap(builtinIds, (id) => [id, new BuiltinModule(id)]),
2321cb0ef41Sopenharmony_ci  );
2331cb0ef41Sopenharmony_ci
2341cb0ef41Sopenharmony_ci  constructor(id) {
2351cb0ef41Sopenharmony_ci    this.filename = `${id}.js`;
2361cb0ef41Sopenharmony_ci    this.id = id;
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_ci    // The CJS exports object of the module.
2391cb0ef41Sopenharmony_ci    this.exports = {};
2401cb0ef41Sopenharmony_ci    // States used to work around circular dependencies.
2411cb0ef41Sopenharmony_ci    this.loaded = false;
2421cb0ef41Sopenharmony_ci    this.loading = false;
2431cb0ef41Sopenharmony_ci
2441cb0ef41Sopenharmony_ci    // The following properties are used by the ESM implementation and only
2451cb0ef41Sopenharmony_ci    // initialized when the built-in module is loaded by users.
2461cb0ef41Sopenharmony_ci    /**
2471cb0ef41Sopenharmony_ci     * The C++ ModuleWrap binding used to interface with the ESM implementation.
2481cb0ef41Sopenharmony_ci     * @type {ModuleWrap|undefined}
2491cb0ef41Sopenharmony_ci     */
2501cb0ef41Sopenharmony_ci    this.module = undefined;
2511cb0ef41Sopenharmony_ci    /**
2521cb0ef41Sopenharmony_ci     * Exported names for the ESM imports.
2531cb0ef41Sopenharmony_ci     * @type {string[]|undefined}
2541cb0ef41Sopenharmony_ci     */
2551cb0ef41Sopenharmony_ci    this.exportKeys = undefined;
2561cb0ef41Sopenharmony_ci  }
2571cb0ef41Sopenharmony_ci
2581cb0ef41Sopenharmony_ci  static allowRequireByUsers(id) {
2591cb0ef41Sopenharmony_ci    if (id === selfId) {
2601cb0ef41Sopenharmony_ci      // No code because this is an assertion against bugs.
2611cb0ef41Sopenharmony_ci      // eslint-disable-next-line no-restricted-syntax
2621cb0ef41Sopenharmony_ci      throw new Error(`Should not allow ${id}`);
2631cb0ef41Sopenharmony_ci    }
2641cb0ef41Sopenharmony_ci    canBeRequiredByUsersList.add(id);
2651cb0ef41Sopenharmony_ci    if (!schemelessBlockList.has(id)) {
2661cb0ef41Sopenharmony_ci      canBeRequiredByUsersWithoutSchemeList.add(id);
2671cb0ef41Sopenharmony_ci    }
2681cb0ef41Sopenharmony_ci  }
2691cb0ef41Sopenharmony_ci
2701cb0ef41Sopenharmony_ci  // To be called during pre-execution when --expose-internals is on.
2711cb0ef41Sopenharmony_ci  // Enables the user-land module loader to access internal modules.
2721cb0ef41Sopenharmony_ci  static exposeInternals() {
2731cb0ef41Sopenharmony_ci    for (let i = 0; i < internalBuiltinIds.length; ++i) {
2741cb0ef41Sopenharmony_ci      BuiltinModule.allowRequireByUsers(internalBuiltinIds[i]);
2751cb0ef41Sopenharmony_ci    }
2761cb0ef41Sopenharmony_ci  }
2771cb0ef41Sopenharmony_ci
2781cb0ef41Sopenharmony_ci  static exists(id) {
2791cb0ef41Sopenharmony_ci    return BuiltinModule.map.has(id);
2801cb0ef41Sopenharmony_ci  }
2811cb0ef41Sopenharmony_ci
2821cb0ef41Sopenharmony_ci  static canBeRequiredByUsers(id) {
2831cb0ef41Sopenharmony_ci    return canBeRequiredByUsersList.has(id);
2841cb0ef41Sopenharmony_ci  }
2851cb0ef41Sopenharmony_ci
2861cb0ef41Sopenharmony_ci  static canBeRequiredWithoutScheme(id) {
2871cb0ef41Sopenharmony_ci    return canBeRequiredByUsersWithoutSchemeList.has(id);
2881cb0ef41Sopenharmony_ci  }
2891cb0ef41Sopenharmony_ci
2901cb0ef41Sopenharmony_ci  static isBuiltin(id) {
2911cb0ef41Sopenharmony_ci    return BuiltinModule.canBeRequiredWithoutScheme(id) || (
2921cb0ef41Sopenharmony_ci      typeof id === 'string' &&
2931cb0ef41Sopenharmony_ci        StringPrototypeStartsWith(id, 'node:') &&
2941cb0ef41Sopenharmony_ci        BuiltinModule.canBeRequiredByUsers(StringPrototypeSlice(id, 5))
2951cb0ef41Sopenharmony_ci    );
2961cb0ef41Sopenharmony_ci  }
2971cb0ef41Sopenharmony_ci
2981cb0ef41Sopenharmony_ci  static getCanBeRequiredByUsersWithoutSchemeList() {
2991cb0ef41Sopenharmony_ci    return ArrayFrom(canBeRequiredByUsersWithoutSchemeList);
3001cb0ef41Sopenharmony_ci  }
3011cb0ef41Sopenharmony_ci
3021cb0ef41Sopenharmony_ci  static normalizeRequirableId(id) {
3031cb0ef41Sopenharmony_ci    if (StringPrototypeStartsWith(id, 'node:')) {
3041cb0ef41Sopenharmony_ci      const normalizedId = StringPrototypeSlice(id, 5);
3051cb0ef41Sopenharmony_ci      if (BuiltinModule.canBeRequiredByUsers(normalizedId)) {
3061cb0ef41Sopenharmony_ci        return normalizedId;
3071cb0ef41Sopenharmony_ci      }
3081cb0ef41Sopenharmony_ci    } else if (BuiltinModule.canBeRequiredWithoutScheme(id)) {
3091cb0ef41Sopenharmony_ci      return id;
3101cb0ef41Sopenharmony_ci    }
3111cb0ef41Sopenharmony_ci
3121cb0ef41Sopenharmony_ci    return undefined;
3131cb0ef41Sopenharmony_ci  }
3141cb0ef41Sopenharmony_ci
3151cb0ef41Sopenharmony_ci  static getSchemeOnlyModuleNames() {
3161cb0ef41Sopenharmony_ci    return ArrayFrom(schemelessBlockList);
3171cb0ef41Sopenharmony_ci  }
3181cb0ef41Sopenharmony_ci
3191cb0ef41Sopenharmony_ci  // Used by user-land module loaders to compile and load builtins.
3201cb0ef41Sopenharmony_ci  compileForPublicLoader() {
3211cb0ef41Sopenharmony_ci    if (!BuiltinModule.canBeRequiredByUsers(this.id)) {
3221cb0ef41Sopenharmony_ci      // No code because this is an assertion against bugs
3231cb0ef41Sopenharmony_ci      // eslint-disable-next-line no-restricted-syntax
3241cb0ef41Sopenharmony_ci      throw new Error(`Should not compile ${this.id} for public use`);
3251cb0ef41Sopenharmony_ci    }
3261cb0ef41Sopenharmony_ci    this.compileForInternalLoader();
3271cb0ef41Sopenharmony_ci    if (!this.exportKeys) {
3281cb0ef41Sopenharmony_ci      // When using --expose-internals, we do not want to reflect the named
3291cb0ef41Sopenharmony_ci      // exports from core modules as this can trigger unnecessary getters.
3301cb0ef41Sopenharmony_ci      const internal = StringPrototypeStartsWith(this.id, 'internal/');
3311cb0ef41Sopenharmony_ci      this.exportKeys = internal ? [] : ObjectKeys(this.exports);
3321cb0ef41Sopenharmony_ci    }
3331cb0ef41Sopenharmony_ci    this.getESMFacade();
3341cb0ef41Sopenharmony_ci    this.syncExports();
3351cb0ef41Sopenharmony_ci    return this.exports;
3361cb0ef41Sopenharmony_ci  }
3371cb0ef41Sopenharmony_ci
3381cb0ef41Sopenharmony_ci  getESMFacade() {
3391cb0ef41Sopenharmony_ci    if (this.module) return this.module;
3401cb0ef41Sopenharmony_ci    const { ModuleWrap } = internalBinding('module_wrap');
3411cb0ef41Sopenharmony_ci    // TODO(aduh95): move this to C++, alongside the initialization of the class.
3421cb0ef41Sopenharmony_ci    ObjectSetPrototypeOf(ModuleWrap.prototype, null);
3431cb0ef41Sopenharmony_ci    const url = `node:${this.id}`;
3441cb0ef41Sopenharmony_ci    const builtin = this;
3451cb0ef41Sopenharmony_ci    const exportsKeys = ArrayPrototypeSlice(this.exportKeys);
3461cb0ef41Sopenharmony_ci    ArrayPrototypePush(exportsKeys, 'default');
3471cb0ef41Sopenharmony_ci    this.module = new ModuleWrap(
3481cb0ef41Sopenharmony_ci      url, undefined, exportsKeys,
3491cb0ef41Sopenharmony_ci      function() {
3501cb0ef41Sopenharmony_ci        builtin.syncExports();
3511cb0ef41Sopenharmony_ci        this.setExport('default', builtin.exports);
3521cb0ef41Sopenharmony_ci      });
3531cb0ef41Sopenharmony_ci    // Ensure immediate sync execution to capture exports now
3541cb0ef41Sopenharmony_ci    this.module.instantiate();
3551cb0ef41Sopenharmony_ci    this.module.evaluate(-1, false);
3561cb0ef41Sopenharmony_ci    return this.module;
3571cb0ef41Sopenharmony_ci  }
3581cb0ef41Sopenharmony_ci
3591cb0ef41Sopenharmony_ci  // Provide named exports for all builtin libraries so that the libraries
3601cb0ef41Sopenharmony_ci  // may be imported in a nicer way for ESM users. The default export is left
3611cb0ef41Sopenharmony_ci  // as the entire namespace (module.exports) and updates when this function is
3621cb0ef41Sopenharmony_ci  // called so that APMs and other behavior are supported.
3631cb0ef41Sopenharmony_ci  syncExports() {
3641cb0ef41Sopenharmony_ci    const names = this.exportKeys;
3651cb0ef41Sopenharmony_ci    if (this.module) {
3661cb0ef41Sopenharmony_ci      for (let i = 0; i < names.length; i++) {
3671cb0ef41Sopenharmony_ci        const exportName = names[i];
3681cb0ef41Sopenharmony_ci        if (exportName === 'default') continue;
3691cb0ef41Sopenharmony_ci        this.module.setExport(exportName,
3701cb0ef41Sopenharmony_ci                              getOwn(this.exports, exportName, this.exports));
3711cb0ef41Sopenharmony_ci      }
3721cb0ef41Sopenharmony_ci    }
3731cb0ef41Sopenharmony_ci  }
3741cb0ef41Sopenharmony_ci
3751cb0ef41Sopenharmony_ci  compileForInternalLoader() {
3761cb0ef41Sopenharmony_ci    if (this.loaded || this.loading) {
3771cb0ef41Sopenharmony_ci      return this.exports;
3781cb0ef41Sopenharmony_ci    }
3791cb0ef41Sopenharmony_ci
3801cb0ef41Sopenharmony_ci    const id = this.id;
3811cb0ef41Sopenharmony_ci    this.loading = true;
3821cb0ef41Sopenharmony_ci
3831cb0ef41Sopenharmony_ci    try {
3841cb0ef41Sopenharmony_ci      const requireFn = StringPrototypeStartsWith(this.id, 'internal/deps/') ?
3851cb0ef41Sopenharmony_ci        requireWithFallbackInDeps : requireBuiltin;
3861cb0ef41Sopenharmony_ci
3871cb0ef41Sopenharmony_ci      const fn = compileFunction(id);
3881cb0ef41Sopenharmony_ci      // Arguments must match the parameters specified in
3891cb0ef41Sopenharmony_ci      // BuiltinLoader::LookupAndCompile().
3901cb0ef41Sopenharmony_ci      fn(this.exports, requireFn, this, process, internalBinding, primordials);
3911cb0ef41Sopenharmony_ci
3921cb0ef41Sopenharmony_ci      this.loaded = true;
3931cb0ef41Sopenharmony_ci    } finally {
3941cb0ef41Sopenharmony_ci      this.loading = false;
3951cb0ef41Sopenharmony_ci    }
3961cb0ef41Sopenharmony_ci
3971cb0ef41Sopenharmony_ci    // "NativeModule" is a legacy name of "BuiltinModule". We keep it
3981cb0ef41Sopenharmony_ci    // here to avoid breaking users who parse process.moduleLoadList.
3991cb0ef41Sopenharmony_ci    ArrayPrototypePush(moduleLoadList, `NativeModule ${id}`);
4001cb0ef41Sopenharmony_ci    return this.exports;
4011cb0ef41Sopenharmony_ci  }
4021cb0ef41Sopenharmony_ci}
4031cb0ef41Sopenharmony_ci
4041cb0ef41Sopenharmony_ci// Think of this as module.exports in this file even though it is not
4051cb0ef41Sopenharmony_ci// written in CommonJS style.
4061cb0ef41Sopenharmony_ciconst loaderExports = {
4071cb0ef41Sopenharmony_ci  internalBinding,
4081cb0ef41Sopenharmony_ci  BuiltinModule,
4091cb0ef41Sopenharmony_ci  require: requireBuiltin,
4101cb0ef41Sopenharmony_ci};
4111cb0ef41Sopenharmony_ci
4121cb0ef41Sopenharmony_cifunction requireBuiltin(id) {
4131cb0ef41Sopenharmony_ci  if (id === selfId) {
4141cb0ef41Sopenharmony_ci    return loaderExports;
4151cb0ef41Sopenharmony_ci  }
4161cb0ef41Sopenharmony_ci
4171cb0ef41Sopenharmony_ci  const mod = BuiltinModule.map.get(id);
4181cb0ef41Sopenharmony_ci  // Can't load the internal errors module from here, have to use a raw error.
4191cb0ef41Sopenharmony_ci  // eslint-disable-next-line no-restricted-syntax
4201cb0ef41Sopenharmony_ci  if (!mod) throw new TypeError(`Missing internal module '${id}'`);
4211cb0ef41Sopenharmony_ci  return mod.compileForInternalLoader();
4221cb0ef41Sopenharmony_ci}
4231cb0ef41Sopenharmony_ci
4241cb0ef41Sopenharmony_ci// Allow internal modules from dependencies to require
4251cb0ef41Sopenharmony_ci// other modules from dependencies by providing fallbacks.
4261cb0ef41Sopenharmony_cifunction requireWithFallbackInDeps(request) {
4271cb0ef41Sopenharmony_ci  if (StringPrototypeStartsWith(request, 'node:')) {
4281cb0ef41Sopenharmony_ci    request = StringPrototypeSlice(request, 5);
4291cb0ef41Sopenharmony_ci  } else if (!BuiltinModule.map.has(request)) {
4301cb0ef41Sopenharmony_ci    request = `internal/deps/${request}`;
4311cb0ef41Sopenharmony_ci  }
4321cb0ef41Sopenharmony_ci  return requireBuiltin(request);
4331cb0ef41Sopenharmony_ci}
4341cb0ef41Sopenharmony_ci
4351cb0ef41Sopenharmony_cifunction setupPrepareStackTrace() {
4361cb0ef41Sopenharmony_ci  const {
4371cb0ef41Sopenharmony_ci    setEnhanceStackForFatalException,
4381cb0ef41Sopenharmony_ci    setPrepareStackTraceCallback,
4391cb0ef41Sopenharmony_ci  } = internalBinding('errors');
4401cb0ef41Sopenharmony_ci  const {
4411cb0ef41Sopenharmony_ci    prepareStackTrace,
4421cb0ef41Sopenharmony_ci    fatalExceptionStackEnhancers: {
4431cb0ef41Sopenharmony_ci      beforeInspector,
4441cb0ef41Sopenharmony_ci      afterInspector,
4451cb0ef41Sopenharmony_ci    },
4461cb0ef41Sopenharmony_ci  } = requireBuiltin('internal/errors');
4471cb0ef41Sopenharmony_ci  // Tell our PrepareStackTraceCallback passed to the V8 API
4481cb0ef41Sopenharmony_ci  // to call prepareStackTrace().
4491cb0ef41Sopenharmony_ci  setPrepareStackTraceCallback(prepareStackTrace);
4501cb0ef41Sopenharmony_ci  // Set the function used to enhance the error stack for printing
4511cb0ef41Sopenharmony_ci  setEnhanceStackForFatalException(beforeInspector, afterInspector);
4521cb0ef41Sopenharmony_ci}
4531cb0ef41Sopenharmony_ci
4541cb0ef41Sopenharmony_ci// Store the internal loaders in C++.
4551cb0ef41Sopenharmony_cisetInternalLoaders(internalBinding, requireBuiltin);
4561cb0ef41Sopenharmony_ci
4571cb0ef41Sopenharmony_ci// Setup per-realm bindings.
4581cb0ef41Sopenharmony_cisetupPrepareStackTrace();
459