2025-05-09 19:05:31 +07:00
|
|
|
import { _execInitSubs, _execReqSubs } from './subscriptions/_internal'
|
2025-04-15 15:02:44 +07:00
|
|
|
import { isModuleExportsBad } from './utils'
|
2025-03-30 23:32:17 +07:00
|
|
|
|
2025-05-09 19:05:31 +07:00
|
|
|
import type { Metro } from '../types'
|
2025-03-30 23:32:17 +07:00
|
|
|
|
|
|
|
|
/** Initializing ID */
|
2025-05-09 19:05:31 +07:00
|
|
|
export let _initing = -1
|
|
|
|
|
/** Known uninitialized IDs */
|
|
|
|
|
export const _uninits = new Set<Metro.ModuleID>()
|
2025-04-15 15:02:44 +07:00
|
|
|
/** Known initialized IDs (not blacklisted) */
|
2025-05-09 19:05:31 +07:00
|
|
|
export const _inits = new Set<Metro.ModuleID>()
|
2025-03-30 23:32:17 +07:00
|
|
|
|
2025-05-09 19:05:31 +07:00
|
|
|
export const _paths = new Map<string, Metro.ModuleID>()
|
|
|
|
|
export const _metas = new Map<
|
|
|
|
|
Metro.ModuleID,
|
|
|
|
|
[deps: Metro.DependencyMap, initialized: boolean, module?: Metro.Module]
|
|
|
|
|
>()
|
2025-03-30 23:32:17 +07:00
|
|
|
|
|
|
|
|
export function patchMetroDefine(metroDefine: Metro.DefineFn) {
|
|
|
|
|
return ((origFactory, id, deps) => {
|
|
|
|
|
// deps won't be undefined last time I checked
|
2025-05-09 19:05:31 +07:00
|
|
|
const metadata = [deps!, false] as typeof _metas extends Map<any, infer V> ? V : never
|
|
|
|
|
_metas.set(id, metadata)
|
|
|
|
|
_uninits.add(id)
|
2025-03-30 23:32:17 +07:00
|
|
|
|
2025-04-15 15:02:44 +07:00
|
|
|
metroDefine(
|
|
|
|
|
((global, req, importDefault, importAll, module, exports, deps) => {
|
2025-04-16 14:23:48 +07:00
|
|
|
metadata[2] = module
|
2025-03-30 23:32:17 +07:00
|
|
|
|
2025-05-09 19:05:31 +07:00
|
|
|
const prevIId = _initing
|
|
|
|
|
_initing = id
|
2025-03-30 23:32:17 +07:00
|
|
|
|
2025-05-09 19:05:31 +07:00
|
|
|
_execReqSubs(id)
|
2025-03-30 23:32:17 +07:00
|
|
|
|
2025-04-15 15:02:44 +07:00
|
|
|
try {
|
|
|
|
|
origFactory(global, req, importDefault, importAll, module, exports, deps)
|
2025-05-09 19:05:31 +07:00
|
|
|
metadata[1] = true
|
2025-03-30 23:32:17 +07:00
|
|
|
|
2025-04-15 15:02:44 +07:00
|
|
|
// Add the module to the initialized set only if the factory doesn't error or the exports aren't bad
|
|
|
|
|
// Don't use exports here, as modules can set module.exports to a different object
|
2025-05-09 19:05:31 +07:00
|
|
|
if (!isModuleExportsBad(module.exports)) _inits.add(id)
|
|
|
|
|
_execInitSubs(id, module.exports)
|
|
|
|
|
} catch {}
|
|
|
|
|
|
|
|
|
|
_initing = prevIId
|
|
|
|
|
_uninits.delete(id)
|
2025-04-15 15:02:44 +07:00
|
|
|
}) satisfies Metro.FactoryFn,
|
|
|
|
|
id,
|
|
|
|
|
deps,
|
|
|
|
|
)
|
2025-03-30 23:32:17 +07:00
|
|
|
}) satisfies Metro.DefineFn
|
|
|
|
|
}
|