11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst assert = require('internal/assert'); 41cb0ef41Sopenharmony_ciconst { 51cb0ef41Sopenharmony_ci ArrayIsArray, 61cb0ef41Sopenharmony_ci ArrayPrototypeForEach, 71cb0ef41Sopenharmony_ci ArrayPrototypeIndexOf, 81cb0ef41Sopenharmony_ci ArrayPrototypeSome, 91cb0ef41Sopenharmony_ci ObjectCreate, 101cb0ef41Sopenharmony_ci ObjectDefineProperty, 111cb0ef41Sopenharmony_ci ObjectGetPrototypeOf, 121cb0ef41Sopenharmony_ci ObjectSetPrototypeOf, 131cb0ef41Sopenharmony_ci ReflectApply, 141cb0ef41Sopenharmony_ci SafePromiseAllReturnVoid, 151cb0ef41Sopenharmony_ci Symbol, 161cb0ef41Sopenharmony_ci SymbolToStringTag, 171cb0ef41Sopenharmony_ci TypeError, 181cb0ef41Sopenharmony_ci} = primordials; 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ciconst { isContext } = internalBinding('contextify'); 211cb0ef41Sopenharmony_ciconst { 221cb0ef41Sopenharmony_ci isModuleNamespaceObject, 231cb0ef41Sopenharmony_ci} = require('internal/util/types'); 241cb0ef41Sopenharmony_ciconst { 251cb0ef41Sopenharmony_ci customInspectSymbol, 261cb0ef41Sopenharmony_ci emitExperimentalWarning, 271cb0ef41Sopenharmony_ci getConstructorOf, 281cb0ef41Sopenharmony_ci kEmptyObject, 291cb0ef41Sopenharmony_ci} = require('internal/util'); 301cb0ef41Sopenharmony_ciconst { 311cb0ef41Sopenharmony_ci ERR_INVALID_ARG_TYPE, 321cb0ef41Sopenharmony_ci ERR_INVALID_ARG_VALUE, 331cb0ef41Sopenharmony_ci ERR_VM_MODULE_ALREADY_LINKED, 341cb0ef41Sopenharmony_ci ERR_VM_MODULE_DIFFERENT_CONTEXT, 351cb0ef41Sopenharmony_ci ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA, 361cb0ef41Sopenharmony_ci ERR_VM_MODULE_LINK_FAILURE, 371cb0ef41Sopenharmony_ci ERR_VM_MODULE_NOT_MODULE, 381cb0ef41Sopenharmony_ci ERR_VM_MODULE_STATUS, 391cb0ef41Sopenharmony_ci} = require('internal/errors').codes; 401cb0ef41Sopenharmony_ciconst { 411cb0ef41Sopenharmony_ci validateBoolean, 421cb0ef41Sopenharmony_ci validateBuffer, 431cb0ef41Sopenharmony_ci validateFunction, 441cb0ef41Sopenharmony_ci validateInt32, 451cb0ef41Sopenharmony_ci validateObject, 461cb0ef41Sopenharmony_ci validateUint32, 471cb0ef41Sopenharmony_ci validateString, 481cb0ef41Sopenharmony_ci} = require('internal/validators'); 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ciconst binding = internalBinding('module_wrap'); 511cb0ef41Sopenharmony_ciconst { 521cb0ef41Sopenharmony_ci ModuleWrap, 531cb0ef41Sopenharmony_ci kUninstantiated, 541cb0ef41Sopenharmony_ci kInstantiating, 551cb0ef41Sopenharmony_ci kInstantiated, 561cb0ef41Sopenharmony_ci kEvaluating, 571cb0ef41Sopenharmony_ci kEvaluated, 581cb0ef41Sopenharmony_ci kErrored, 591cb0ef41Sopenharmony_ci} = binding; 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ciconst STATUS_MAP = { 621cb0ef41Sopenharmony_ci [kUninstantiated]: 'unlinked', 631cb0ef41Sopenharmony_ci [kInstantiating]: 'linking', 641cb0ef41Sopenharmony_ci [kInstantiated]: 'linked', 651cb0ef41Sopenharmony_ci [kEvaluating]: 'evaluating', 661cb0ef41Sopenharmony_ci [kEvaluated]: 'evaluated', 671cb0ef41Sopenharmony_ci [kErrored]: 'errored', 681cb0ef41Sopenharmony_ci}; 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_cilet globalModuleId = 0; 711cb0ef41Sopenharmony_ciconst defaultModuleName = 'vm:module'; 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ciconst kWrap = Symbol('kWrap'); 741cb0ef41Sopenharmony_ciconst kContext = Symbol('kContext'); 751cb0ef41Sopenharmony_ciconst kPerContextModuleId = Symbol('kPerContextModuleId'); 761cb0ef41Sopenharmony_ciconst kLink = Symbol('kLink'); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ciclass Module { 791cb0ef41Sopenharmony_ci constructor(options) { 801cb0ef41Sopenharmony_ci emitExperimentalWarning('VM Modules'); 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci if (new.target === Module) { 831cb0ef41Sopenharmony_ci // eslint-disable-next-line no-restricted-syntax 841cb0ef41Sopenharmony_ci throw new TypeError('Module is not a constructor'); 851cb0ef41Sopenharmony_ci } 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci const { 881cb0ef41Sopenharmony_ci context, 891cb0ef41Sopenharmony_ci sourceText, 901cb0ef41Sopenharmony_ci syntheticExportNames, 911cb0ef41Sopenharmony_ci syntheticEvaluationSteps, 921cb0ef41Sopenharmony_ci } = options; 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci if (context !== undefined) { 951cb0ef41Sopenharmony_ci validateObject(context, 'context'); 961cb0ef41Sopenharmony_ci if (!isContext(context)) { 971cb0ef41Sopenharmony_ci throw new ERR_INVALID_ARG_TYPE('options.context', 'vm.Context', 981cb0ef41Sopenharmony_ci context); 991cb0ef41Sopenharmony_ci } 1001cb0ef41Sopenharmony_ci } 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci let { identifier } = options; 1031cb0ef41Sopenharmony_ci if (identifier !== undefined) { 1041cb0ef41Sopenharmony_ci validateString(identifier, 'options.identifier'); 1051cb0ef41Sopenharmony_ci } else if (context === undefined) { 1061cb0ef41Sopenharmony_ci identifier = `${defaultModuleName}(${globalModuleId++})`; 1071cb0ef41Sopenharmony_ci } else if (context[kPerContextModuleId] !== undefined) { 1081cb0ef41Sopenharmony_ci const curId = context[kPerContextModuleId]; 1091cb0ef41Sopenharmony_ci identifier = `${defaultModuleName}(${curId})`; 1101cb0ef41Sopenharmony_ci context[kPerContextModuleId] += 1; 1111cb0ef41Sopenharmony_ci } else { 1121cb0ef41Sopenharmony_ci identifier = `${defaultModuleName}(0)`; 1131cb0ef41Sopenharmony_ci ObjectDefineProperty(context, kPerContextModuleId, { 1141cb0ef41Sopenharmony_ci __proto__: null, 1151cb0ef41Sopenharmony_ci value: 1, 1161cb0ef41Sopenharmony_ci writable: true, 1171cb0ef41Sopenharmony_ci enumerable: false, 1181cb0ef41Sopenharmony_ci configurable: true, 1191cb0ef41Sopenharmony_ci }); 1201cb0ef41Sopenharmony_ci } 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci let registry = { __proto__: null }; 1231cb0ef41Sopenharmony_ci if (sourceText !== undefined) { 1241cb0ef41Sopenharmony_ci this[kWrap] = new ModuleWrap(identifier, context, sourceText, 1251cb0ef41Sopenharmony_ci options.lineOffset, options.columnOffset, 1261cb0ef41Sopenharmony_ci options.cachedData); 1271cb0ef41Sopenharmony_ci registry = { 1281cb0ef41Sopenharmony_ci __proto__: null, 1291cb0ef41Sopenharmony_ci initializeImportMeta: options.initializeImportMeta, 1301cb0ef41Sopenharmony_ci importModuleDynamically: options.importModuleDynamically ? 1311cb0ef41Sopenharmony_ci importModuleDynamicallyWrap(options.importModuleDynamically) : 1321cb0ef41Sopenharmony_ci undefined, 1331cb0ef41Sopenharmony_ci }; 1341cb0ef41Sopenharmony_ci } else { 1351cb0ef41Sopenharmony_ci assert(syntheticEvaluationSteps); 1361cb0ef41Sopenharmony_ci this[kWrap] = new ModuleWrap(identifier, context, 1371cb0ef41Sopenharmony_ci syntheticExportNames, 1381cb0ef41Sopenharmony_ci syntheticEvaluationSteps); 1391cb0ef41Sopenharmony_ci } 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ci // This will take precedence over the referrer as the object being 1421cb0ef41Sopenharmony_ci // passed into the callbacks. 1431cb0ef41Sopenharmony_ci registry.callbackReferrer = this; 1441cb0ef41Sopenharmony_ci const { registerModule } = require('internal/modules/esm/utils'); 1451cb0ef41Sopenharmony_ci registerModule(this[kWrap], registry); 1461cb0ef41Sopenharmony_ci 1471cb0ef41Sopenharmony_ci this[kContext] = context; 1481cb0ef41Sopenharmony_ci } 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_ci get identifier() { 1511cb0ef41Sopenharmony_ci if (this[kWrap] === undefined) { 1521cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_NOT_MODULE(); 1531cb0ef41Sopenharmony_ci } 1541cb0ef41Sopenharmony_ci return this[kWrap].url; 1551cb0ef41Sopenharmony_ci } 1561cb0ef41Sopenharmony_ci 1571cb0ef41Sopenharmony_ci get context() { 1581cb0ef41Sopenharmony_ci if (this[kWrap] === undefined) { 1591cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_NOT_MODULE(); 1601cb0ef41Sopenharmony_ci } 1611cb0ef41Sopenharmony_ci return this[kContext]; 1621cb0ef41Sopenharmony_ci } 1631cb0ef41Sopenharmony_ci 1641cb0ef41Sopenharmony_ci get namespace() { 1651cb0ef41Sopenharmony_ci if (this[kWrap] === undefined) { 1661cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_NOT_MODULE(); 1671cb0ef41Sopenharmony_ci } 1681cb0ef41Sopenharmony_ci if (this[kWrap].getStatus() < kInstantiated) { 1691cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_STATUS('must not be unlinked or linking'); 1701cb0ef41Sopenharmony_ci } 1711cb0ef41Sopenharmony_ci return this[kWrap].getNamespace(); 1721cb0ef41Sopenharmony_ci } 1731cb0ef41Sopenharmony_ci 1741cb0ef41Sopenharmony_ci get status() { 1751cb0ef41Sopenharmony_ci if (this[kWrap] === undefined) { 1761cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_NOT_MODULE(); 1771cb0ef41Sopenharmony_ci } 1781cb0ef41Sopenharmony_ci return STATUS_MAP[this[kWrap].getStatus()]; 1791cb0ef41Sopenharmony_ci } 1801cb0ef41Sopenharmony_ci 1811cb0ef41Sopenharmony_ci get error() { 1821cb0ef41Sopenharmony_ci if (this[kWrap] === undefined) { 1831cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_NOT_MODULE(); 1841cb0ef41Sopenharmony_ci } 1851cb0ef41Sopenharmony_ci if (this[kWrap].getStatus() !== kErrored) { 1861cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_STATUS('must be errored'); 1871cb0ef41Sopenharmony_ci } 1881cb0ef41Sopenharmony_ci return this[kWrap].getError(); 1891cb0ef41Sopenharmony_ci } 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_ci async link(linker) { 1921cb0ef41Sopenharmony_ci if (this[kWrap] === undefined) { 1931cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_NOT_MODULE(); 1941cb0ef41Sopenharmony_ci } 1951cb0ef41Sopenharmony_ci validateFunction(linker, 'linker'); 1961cb0ef41Sopenharmony_ci if (this.status === 'linked') { 1971cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_ALREADY_LINKED(); 1981cb0ef41Sopenharmony_ci } 1991cb0ef41Sopenharmony_ci if (this.status !== 'unlinked') { 2001cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_STATUS('must be unlinked'); 2011cb0ef41Sopenharmony_ci } 2021cb0ef41Sopenharmony_ci await this[kLink](linker); 2031cb0ef41Sopenharmony_ci this[kWrap].instantiate(); 2041cb0ef41Sopenharmony_ci } 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ci async evaluate(options = kEmptyObject) { 2071cb0ef41Sopenharmony_ci if (this[kWrap] === undefined) { 2081cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_NOT_MODULE(); 2091cb0ef41Sopenharmony_ci } 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ci validateObject(options, 'options'); 2121cb0ef41Sopenharmony_ci 2131cb0ef41Sopenharmony_ci let timeout = options.timeout; 2141cb0ef41Sopenharmony_ci if (timeout === undefined) { 2151cb0ef41Sopenharmony_ci timeout = -1; 2161cb0ef41Sopenharmony_ci } else { 2171cb0ef41Sopenharmony_ci validateUint32(timeout, 'options.timeout', true); 2181cb0ef41Sopenharmony_ci } 2191cb0ef41Sopenharmony_ci const { breakOnSigint = false } = options; 2201cb0ef41Sopenharmony_ci validateBoolean(breakOnSigint, 'options.breakOnSigint'); 2211cb0ef41Sopenharmony_ci const status = this[kWrap].getStatus(); 2221cb0ef41Sopenharmony_ci if (status !== kInstantiated && 2231cb0ef41Sopenharmony_ci status !== kEvaluated && 2241cb0ef41Sopenharmony_ci status !== kErrored) { 2251cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_STATUS( 2261cb0ef41Sopenharmony_ci 'must be one of linked, evaluated, or errored', 2271cb0ef41Sopenharmony_ci ); 2281cb0ef41Sopenharmony_ci } 2291cb0ef41Sopenharmony_ci await this[kWrap].evaluate(timeout, breakOnSigint); 2301cb0ef41Sopenharmony_ci } 2311cb0ef41Sopenharmony_ci 2321cb0ef41Sopenharmony_ci [customInspectSymbol](depth, options) { 2331cb0ef41Sopenharmony_ci if (this[kWrap] === undefined) { 2341cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_NOT_MODULE(); 2351cb0ef41Sopenharmony_ci } 2361cb0ef41Sopenharmony_ci if (typeof depth === 'number' && depth < 0) 2371cb0ef41Sopenharmony_ci return this; 2381cb0ef41Sopenharmony_ci 2391cb0ef41Sopenharmony_ci const constructor = getConstructorOf(this) || Module; 2401cb0ef41Sopenharmony_ci const o = ObjectCreate({ constructor }); 2411cb0ef41Sopenharmony_ci o.status = this.status; 2421cb0ef41Sopenharmony_ci o.identifier = this.identifier; 2431cb0ef41Sopenharmony_ci o.context = this.context; 2441cb0ef41Sopenharmony_ci 2451cb0ef41Sopenharmony_ci ObjectSetPrototypeOf(o, ObjectGetPrototypeOf(this)); 2461cb0ef41Sopenharmony_ci ObjectDefineProperty(o, SymbolToStringTag, { 2471cb0ef41Sopenharmony_ci __proto__: null, 2481cb0ef41Sopenharmony_ci value: constructor.name, 2491cb0ef41Sopenharmony_ci configurable: true, 2501cb0ef41Sopenharmony_ci }); 2511cb0ef41Sopenharmony_ci 2521cb0ef41Sopenharmony_ci // Lazy to avoid circular dependency 2531cb0ef41Sopenharmony_ci const { inspect } = require('internal/util/inspect'); 2541cb0ef41Sopenharmony_ci return inspect(o, { ...options, customInspect: false }); 2551cb0ef41Sopenharmony_ci } 2561cb0ef41Sopenharmony_ci} 2571cb0ef41Sopenharmony_ci 2581cb0ef41Sopenharmony_ciconst kDependencySpecifiers = Symbol('kDependencySpecifiers'); 2591cb0ef41Sopenharmony_ciconst kNoError = Symbol('kNoError'); 2601cb0ef41Sopenharmony_ci 2611cb0ef41Sopenharmony_ciclass SourceTextModule extends Module { 2621cb0ef41Sopenharmony_ci #error = kNoError; 2631cb0ef41Sopenharmony_ci #statusOverride; 2641cb0ef41Sopenharmony_ci 2651cb0ef41Sopenharmony_ci constructor(sourceText, options = kEmptyObject) { 2661cb0ef41Sopenharmony_ci validateString(sourceText, 'sourceText'); 2671cb0ef41Sopenharmony_ci validateObject(options, 'options'); 2681cb0ef41Sopenharmony_ci 2691cb0ef41Sopenharmony_ci const { 2701cb0ef41Sopenharmony_ci lineOffset = 0, 2711cb0ef41Sopenharmony_ci columnOffset = 0, 2721cb0ef41Sopenharmony_ci initializeImportMeta, 2731cb0ef41Sopenharmony_ci importModuleDynamically, 2741cb0ef41Sopenharmony_ci context, 2751cb0ef41Sopenharmony_ci identifier, 2761cb0ef41Sopenharmony_ci cachedData, 2771cb0ef41Sopenharmony_ci } = options; 2781cb0ef41Sopenharmony_ci 2791cb0ef41Sopenharmony_ci validateInt32(lineOffset, 'options.lineOffset'); 2801cb0ef41Sopenharmony_ci validateInt32(columnOffset, 'options.columnOffset'); 2811cb0ef41Sopenharmony_ci 2821cb0ef41Sopenharmony_ci if (initializeImportMeta !== undefined) { 2831cb0ef41Sopenharmony_ci validateFunction(initializeImportMeta, 'options.initializeImportMeta'); 2841cb0ef41Sopenharmony_ci } 2851cb0ef41Sopenharmony_ci 2861cb0ef41Sopenharmony_ci if (importModuleDynamically !== undefined) { 2871cb0ef41Sopenharmony_ci validateFunction(importModuleDynamically, 'options.importModuleDynamically'); 2881cb0ef41Sopenharmony_ci } 2891cb0ef41Sopenharmony_ci 2901cb0ef41Sopenharmony_ci if (cachedData !== undefined) { 2911cb0ef41Sopenharmony_ci validateBuffer(cachedData, 'options.cachedData'); 2921cb0ef41Sopenharmony_ci } 2931cb0ef41Sopenharmony_ci 2941cb0ef41Sopenharmony_ci super({ 2951cb0ef41Sopenharmony_ci sourceText, 2961cb0ef41Sopenharmony_ci context, 2971cb0ef41Sopenharmony_ci identifier, 2981cb0ef41Sopenharmony_ci lineOffset, 2991cb0ef41Sopenharmony_ci columnOffset, 3001cb0ef41Sopenharmony_ci cachedData, 3011cb0ef41Sopenharmony_ci initializeImportMeta, 3021cb0ef41Sopenharmony_ci importModuleDynamically, 3031cb0ef41Sopenharmony_ci }); 3041cb0ef41Sopenharmony_ci 3051cb0ef41Sopenharmony_ci this[kLink] = async (linker) => { 3061cb0ef41Sopenharmony_ci this.#statusOverride = 'linking'; 3071cb0ef41Sopenharmony_ci 3081cb0ef41Sopenharmony_ci const promises = this[kWrap].link(async (identifier, attributes) => { 3091cb0ef41Sopenharmony_ci const module = await linker(identifier, this, { attributes, assert: attributes }); 3101cb0ef41Sopenharmony_ci if (module[kWrap] === undefined) { 3111cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_NOT_MODULE(); 3121cb0ef41Sopenharmony_ci } 3131cb0ef41Sopenharmony_ci if (module.context !== this.context) { 3141cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_DIFFERENT_CONTEXT(); 3151cb0ef41Sopenharmony_ci } 3161cb0ef41Sopenharmony_ci if (module.status === 'errored') { 3171cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_LINK_FAILURE(`request for '${identifier}' resolved to an errored module`, module.error); 3181cb0ef41Sopenharmony_ci } 3191cb0ef41Sopenharmony_ci if (module.status === 'unlinked') { 3201cb0ef41Sopenharmony_ci await module[kLink](linker); 3211cb0ef41Sopenharmony_ci } 3221cb0ef41Sopenharmony_ci return module[kWrap]; 3231cb0ef41Sopenharmony_ci }); 3241cb0ef41Sopenharmony_ci 3251cb0ef41Sopenharmony_ci try { 3261cb0ef41Sopenharmony_ci if (promises !== undefined) { 3271cb0ef41Sopenharmony_ci await SafePromiseAllReturnVoid(promises); 3281cb0ef41Sopenharmony_ci } 3291cb0ef41Sopenharmony_ci } catch (e) { 3301cb0ef41Sopenharmony_ci this.#error = e; 3311cb0ef41Sopenharmony_ci throw e; 3321cb0ef41Sopenharmony_ci } finally { 3331cb0ef41Sopenharmony_ci this.#statusOverride = undefined; 3341cb0ef41Sopenharmony_ci } 3351cb0ef41Sopenharmony_ci }; 3361cb0ef41Sopenharmony_ci 3371cb0ef41Sopenharmony_ci this[kDependencySpecifiers] = undefined; 3381cb0ef41Sopenharmony_ci } 3391cb0ef41Sopenharmony_ci 3401cb0ef41Sopenharmony_ci get dependencySpecifiers() { 3411cb0ef41Sopenharmony_ci if (this[kWrap] === undefined) { 3421cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_NOT_MODULE(); 3431cb0ef41Sopenharmony_ci } 3441cb0ef41Sopenharmony_ci if (this[kDependencySpecifiers] === undefined) { 3451cb0ef41Sopenharmony_ci this[kDependencySpecifiers] = this[kWrap].getStaticDependencySpecifiers(); 3461cb0ef41Sopenharmony_ci } 3471cb0ef41Sopenharmony_ci return this[kDependencySpecifiers]; 3481cb0ef41Sopenharmony_ci } 3491cb0ef41Sopenharmony_ci 3501cb0ef41Sopenharmony_ci get status() { 3511cb0ef41Sopenharmony_ci if (this[kWrap] === undefined) { 3521cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_NOT_MODULE(); 3531cb0ef41Sopenharmony_ci } 3541cb0ef41Sopenharmony_ci if (this.#error !== kNoError) { 3551cb0ef41Sopenharmony_ci return 'errored'; 3561cb0ef41Sopenharmony_ci } 3571cb0ef41Sopenharmony_ci if (this.#statusOverride) { 3581cb0ef41Sopenharmony_ci return this.#statusOverride; 3591cb0ef41Sopenharmony_ci } 3601cb0ef41Sopenharmony_ci return super.status; 3611cb0ef41Sopenharmony_ci } 3621cb0ef41Sopenharmony_ci 3631cb0ef41Sopenharmony_ci get error() { 3641cb0ef41Sopenharmony_ci if (this[kWrap] === undefined) { 3651cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_NOT_MODULE(); 3661cb0ef41Sopenharmony_ci } 3671cb0ef41Sopenharmony_ci if (this.#error !== kNoError) { 3681cb0ef41Sopenharmony_ci return this.#error; 3691cb0ef41Sopenharmony_ci } 3701cb0ef41Sopenharmony_ci return super.error; 3711cb0ef41Sopenharmony_ci } 3721cb0ef41Sopenharmony_ci 3731cb0ef41Sopenharmony_ci createCachedData() { 3741cb0ef41Sopenharmony_ci const { status } = this; 3751cb0ef41Sopenharmony_ci if (status === 'evaluating' || 3761cb0ef41Sopenharmony_ci status === 'evaluated' || 3771cb0ef41Sopenharmony_ci status === 'errored') { 3781cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA(); 3791cb0ef41Sopenharmony_ci } 3801cb0ef41Sopenharmony_ci return this[kWrap].createCachedData(); 3811cb0ef41Sopenharmony_ci } 3821cb0ef41Sopenharmony_ci} 3831cb0ef41Sopenharmony_ci 3841cb0ef41Sopenharmony_ciclass SyntheticModule extends Module { 3851cb0ef41Sopenharmony_ci constructor(exportNames, evaluateCallback, options = kEmptyObject) { 3861cb0ef41Sopenharmony_ci if (!ArrayIsArray(exportNames) || 3871cb0ef41Sopenharmony_ci ArrayPrototypeSome(exportNames, (e) => typeof e !== 'string')) { 3881cb0ef41Sopenharmony_ci throw new ERR_INVALID_ARG_TYPE('exportNames', 3891cb0ef41Sopenharmony_ci 'Array of unique strings', 3901cb0ef41Sopenharmony_ci exportNames); 3911cb0ef41Sopenharmony_ci } else { 3921cb0ef41Sopenharmony_ci ArrayPrototypeForEach(exportNames, (name, i) => { 3931cb0ef41Sopenharmony_ci if (ArrayPrototypeIndexOf(exportNames, name, i + 1) !== -1) { 3941cb0ef41Sopenharmony_ci throw new ERR_INVALID_ARG_VALUE(`exportNames.${name}`, 3951cb0ef41Sopenharmony_ci name, 3961cb0ef41Sopenharmony_ci 'is duplicated'); 3971cb0ef41Sopenharmony_ci } 3981cb0ef41Sopenharmony_ci }); 3991cb0ef41Sopenharmony_ci } 4001cb0ef41Sopenharmony_ci validateFunction(evaluateCallback, 'evaluateCallback'); 4011cb0ef41Sopenharmony_ci 4021cb0ef41Sopenharmony_ci validateObject(options, 'options'); 4031cb0ef41Sopenharmony_ci 4041cb0ef41Sopenharmony_ci const { context, identifier } = options; 4051cb0ef41Sopenharmony_ci 4061cb0ef41Sopenharmony_ci super({ 4071cb0ef41Sopenharmony_ci syntheticExportNames: exportNames, 4081cb0ef41Sopenharmony_ci syntheticEvaluationSteps: evaluateCallback, 4091cb0ef41Sopenharmony_ci context, 4101cb0ef41Sopenharmony_ci identifier, 4111cb0ef41Sopenharmony_ci }); 4121cb0ef41Sopenharmony_ci 4131cb0ef41Sopenharmony_ci this[kLink] = () => this[kWrap].link(() => { 4141cb0ef41Sopenharmony_ci assert.fail('link callback should not be called'); 4151cb0ef41Sopenharmony_ci }); 4161cb0ef41Sopenharmony_ci } 4171cb0ef41Sopenharmony_ci 4181cb0ef41Sopenharmony_ci setExport(name, value) { 4191cb0ef41Sopenharmony_ci if (this[kWrap] === undefined) { 4201cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_NOT_MODULE(); 4211cb0ef41Sopenharmony_ci } 4221cb0ef41Sopenharmony_ci validateString(name, 'name'); 4231cb0ef41Sopenharmony_ci if (this[kWrap].getStatus() < kInstantiated) { 4241cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_STATUS('must be linked'); 4251cb0ef41Sopenharmony_ci } 4261cb0ef41Sopenharmony_ci this[kWrap].setExport(name, value); 4271cb0ef41Sopenharmony_ci } 4281cb0ef41Sopenharmony_ci} 4291cb0ef41Sopenharmony_ci 4301cb0ef41Sopenharmony_cifunction importModuleDynamicallyWrap(importModuleDynamically) { 4311cb0ef41Sopenharmony_ci const importModuleDynamicallyWrapper = async (...args) => { 4321cb0ef41Sopenharmony_ci const m = await ReflectApply(importModuleDynamically, this, args); 4331cb0ef41Sopenharmony_ci if (isModuleNamespaceObject(m)) { 4341cb0ef41Sopenharmony_ci return m; 4351cb0ef41Sopenharmony_ci } 4361cb0ef41Sopenharmony_ci if (!m || m[kWrap] === undefined) { 4371cb0ef41Sopenharmony_ci throw new ERR_VM_MODULE_NOT_MODULE(); 4381cb0ef41Sopenharmony_ci } 4391cb0ef41Sopenharmony_ci if (m.status === 'errored') { 4401cb0ef41Sopenharmony_ci throw m.error; 4411cb0ef41Sopenharmony_ci } 4421cb0ef41Sopenharmony_ci return m.namespace; 4431cb0ef41Sopenharmony_ci }; 4441cb0ef41Sopenharmony_ci return importModuleDynamicallyWrapper; 4451cb0ef41Sopenharmony_ci} 4461cb0ef41Sopenharmony_ci 4471cb0ef41Sopenharmony_cimodule.exports = { 4481cb0ef41Sopenharmony_ci Module, 4491cb0ef41Sopenharmony_ci SourceTextModule, 4501cb0ef41Sopenharmony_ci SyntheticModule, 4511cb0ef41Sopenharmony_ci importModuleDynamicallyWrap, 4521cb0ef41Sopenharmony_ci}; 453