| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							|  |  |  | const RuntimeModule = require("../RuntimeModule"); | 
					
						
							|  |  |  | const Template = require("../Template"); | 
					
						
							| 
									
										
										
										
											2019-10-16 22:38:04 +08:00
										 |  |  | const { compareModulesByIdentifier } = require("../util/comparators"); | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | const WebAssemblyUtils = require("./WebAssemblyUtils"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-12 22:21:21 +08:00
										 |  |  | /** @typedef {import("@webassemblyjs/ast").Signature} Signature */ | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  | /** @typedef {import("../Chunk")} Chunk */ | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | /** @typedef {import("../ChunkGraph")} ChunkGraph */ | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | /** @typedef {import("../ChunkGraph").ModuleId} ModuleId */ | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | /** @typedef {import("../Compilation")} Compilation */ | 
					
						
							|  |  |  | /** @typedef {import("../Module")} Module */ | 
					
						
							| 
									
										
										
										
											2024-03-18 23:28:40 +08:00
										 |  |  | /** @typedef {import("../Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */ | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  | /** @typedef {import("../ModuleGraph")} ModuleGraph */ | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */ | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-09 15:23:01 +08:00
										 |  |  | // TODO webpack 6 remove the whole folder
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | // Get all wasm modules
 | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {ModuleGraph} moduleGraph the module graph | 
					
						
							|  |  |  |  * @param {ChunkGraph} chunkGraph the chunk graph | 
					
						
							|  |  |  |  * @param {Chunk} chunk the chunk | 
					
						
							|  |  |  |  * @returns {Module[]} all wasm modules | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | const getAllWasmModules = (moduleGraph, chunkGraph, chunk) => { | 
					
						
							|  |  |  | 	const wasmModules = chunk.getAllAsyncChunks(); | 
					
						
							|  |  |  | 	const array = []; | 
					
						
							|  |  |  | 	for (const chunk of wasmModules) { | 
					
						
							|  |  |  | 		for (const m of chunkGraph.getOrderedChunkModulesIterable( | 
					
						
							|  |  |  | 			chunk, | 
					
						
							| 
									
										
										
										
											2019-10-16 22:38:04 +08:00
										 |  |  | 			compareModulesByIdentifier | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 		)) { | 
					
						
							|  |  |  | 			if (m.type.startsWith("webassembly")) { | 
					
						
							|  |  |  | 				array.push(m); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return array; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * generates the import object function for a module | 
					
						
							|  |  |  |  * @param {ChunkGraph} chunkGraph the chunk graph | 
					
						
							|  |  |  |  * @param {Module} module the module | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  |  * @param {boolean | undefined} mangle mangle imports | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  |  * @param {string[]} declarations array where declarations are pushed to | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  |  * @param {RuntimeSpec} runtime the runtime | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  |  * @returns {string} source code | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | const generateImportObject = ( | 
					
						
							|  |  |  | 	chunkGraph, | 
					
						
							|  |  |  | 	module, | 
					
						
							|  |  |  | 	mangle, | 
					
						
							|  |  |  | 	declarations, | 
					
						
							|  |  |  | 	runtime | 
					
						
							|  |  |  | ) => { | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 	const moduleGraph = chunkGraph.moduleGraph; | 
					
						
							| 
									
										
										
										
											2024-03-18 01:15:44 +08:00
										 |  |  | 	/** @type {Map<string, string | number>} */ | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 	const waitForInstances = new Map(); | 
					
						
							|  |  |  | 	const properties = []; | 
					
						
							|  |  |  | 	const usedWasmDependencies = WebAssemblyUtils.getUsedDependencies( | 
					
						
							|  |  |  | 		moduleGraph, | 
					
						
							|  |  |  | 		module, | 
					
						
							|  |  |  | 		mangle | 
					
						
							|  |  |  | 	); | 
					
						
							|  |  |  | 	for (const usedDep of usedWasmDependencies) { | 
					
						
							|  |  |  | 		const dep = usedDep.dependency; | 
					
						
							|  |  |  | 		const importedModule = moduleGraph.getModule(dep); | 
					
						
							|  |  |  | 		const exportName = dep.name; | 
					
						
							|  |  |  | 		const usedName = | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 			importedModule && | 
					
						
							|  |  |  | 			moduleGraph | 
					
						
							|  |  |  | 				.getExportsInfo(importedModule) | 
					
						
							|  |  |  | 				.getUsedName(exportName, runtime); | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 		const description = dep.description; | 
					
						
							|  |  |  | 		const direct = dep.onlyDirectImport; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const module = usedDep.module; | 
					
						
							|  |  |  | 		const name = usedDep.name; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (direct) { | 
					
						
							|  |  |  | 			const instanceVar = `m${waitForInstances.size}`; | 
					
						
							| 
									
										
										
										
											2024-03-18 01:15:44 +08:00
										 |  |  | 			waitForInstances.set( | 
					
						
							|  |  |  | 				instanceVar, | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 				/** @type {ModuleId} */ | 
					
						
							|  |  |  | 				(chunkGraph.getModuleId(/** @type {Module} */ (importedModule))) | 
					
						
							| 
									
										
										
										
											2024-03-18 01:15:44 +08:00
										 |  |  | 			); | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 			properties.push({ | 
					
						
							|  |  |  | 				module, | 
					
						
							|  |  |  | 				name, | 
					
						
							|  |  |  | 				value: `${instanceVar}[${JSON.stringify(usedName)}]` | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2023-06-12 22:21:21 +08:00
										 |  |  | 			const params = | 
					
						
							|  |  |  | 				/** @type {Signature} */ | 
					
						
							|  |  |  | 				(description.signature).params.map( | 
					
						
							| 
									
										
										
										
											2024-07-31 10:39:30 +08:00
										 |  |  | 					(param, k) => `p${k}${param.valtype}` | 
					
						
							| 
									
										
										
										
											2023-06-12 22:21:21 +08:00
										 |  |  | 				); | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			const mod = `${RuntimeGlobals.moduleCache}[${JSON.stringify( | 
					
						
							| 
									
										
										
										
											2024-03-18 01:15:44 +08:00
										 |  |  | 				chunkGraph.getModuleId(/** @type {Module} */ (importedModule)) | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 			)}]`;
 | 
					
						
							|  |  |  | 			const modExports = `${mod}.exports`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const cache = `wasmImportedFuncCache${declarations.length}`; | 
					
						
							|  |  |  | 			declarations.push(`var ${cache};`); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 01:15:44 +08:00
										 |  |  | 			const modCode = | 
					
						
							|  |  |  | 				/** @type {Module} */ | 
					
						
							|  |  |  | 				(importedModule).type.startsWith("webassembly") | 
					
						
							|  |  |  | 					? `${mod} ? ${modExports}[${JSON.stringify(usedName)}] : ` | 
					
						
							|  |  |  | 					: ""; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 			properties.push({ | 
					
						
							|  |  |  | 				module, | 
					
						
							|  |  |  | 				name, | 
					
						
							|  |  |  | 				value: Template.asString([ | 
					
						
							| 
									
										
										
										
											2024-07-31 10:39:30 +08:00
										 |  |  | 					`${modCode}function(${params}) {`, | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 					Template.indent([ | 
					
						
							|  |  |  | 						`if(${cache} === undefined) ${cache} = ${modExports};`, | 
					
						
							|  |  |  | 						`return ${cache}[${JSON.stringify(usedName)}](${params});` | 
					
						
							|  |  |  | 					]), | 
					
						
							|  |  |  | 					"}" | 
					
						
							|  |  |  | 				]) | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	let importObject; | 
					
						
							|  |  |  | 	if (mangle) { | 
					
						
							|  |  |  | 		importObject = [ | 
					
						
							|  |  |  | 			"return {", | 
					
						
							|  |  |  | 			Template.indent([ | 
					
						
							|  |  |  | 				properties.map(p => `${JSON.stringify(p.name)}: ${p.value}`).join(",\n") | 
					
						
							|  |  |  | 			]), | 
					
						
							|  |  |  | 			"};" | 
					
						
							|  |  |  | 		]; | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2023-06-12 22:21:21 +08:00
										 |  |  | 		/** @type {Map<string, Array<{ name: string, value: string }>>} */ | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 		const propertiesByModule = new Map(); | 
					
						
							|  |  |  | 		for (const p of properties) { | 
					
						
							|  |  |  | 			let list = propertiesByModule.get(p.module); | 
					
						
							|  |  |  | 			if (list === undefined) { | 
					
						
							|  |  |  | 				propertiesByModule.set(p.module, (list = [])); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			list.push(p); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		importObject = [ | 
					
						
							|  |  |  | 			"return {", | 
					
						
							|  |  |  | 			Template.indent([ | 
					
						
							| 
									
										
										
										
											2024-07-31 11:31:11 +08:00
										 |  |  | 				Array.from(propertiesByModule, ([module, list]) => | 
					
						
							|  |  |  | 					Template.asString([ | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 						`${JSON.stringify(module)}: {`, | 
					
						
							|  |  |  | 						Template.indent([ | 
					
						
							|  |  |  | 							list.map(p => `${JSON.stringify(p.name)}: ${p.value}`).join(",\n") | 
					
						
							|  |  |  | 						]), | 
					
						
							|  |  |  | 						"}" | 
					
						
							| 
									
										
										
										
											2024-07-31 11:31:11 +08:00
										 |  |  | 					]) | 
					
						
							|  |  |  | 				).join(",\n") | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 			]), | 
					
						
							|  |  |  | 			"};" | 
					
						
							|  |  |  | 		]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const moduleIdStringified = JSON.stringify(chunkGraph.getModuleId(module)); | 
					
						
							|  |  |  | 	if (waitForInstances.size === 1) { | 
					
						
							|  |  |  | 		const moduleId = Array.from(waitForInstances.values())[0]; | 
					
						
							|  |  |  | 		const promise = `installedWasmModules[${JSON.stringify(moduleId)}]`; | 
					
						
							|  |  |  | 		const variable = Array.from(waitForInstances.keys())[0]; | 
					
						
							|  |  |  | 		return Template.asString([ | 
					
						
							|  |  |  | 			`${moduleIdStringified}: function() {`, | 
					
						
							|  |  |  | 			Template.indent([ | 
					
						
							|  |  |  | 				`return promiseResolve().then(function() { return ${promise}; }).then(function(${variable}) {`, | 
					
						
							|  |  |  | 				Template.indent(importObject), | 
					
						
							|  |  |  | 				"});" | 
					
						
							|  |  |  | 			]), | 
					
						
							|  |  |  | 			"}," | 
					
						
							|  |  |  | 		]); | 
					
						
							|  |  |  | 	} else if (waitForInstances.size > 0) { | 
					
						
							|  |  |  | 		const promises = Array.from( | 
					
						
							|  |  |  | 			waitForInstances.values(), | 
					
						
							|  |  |  | 			id => `installedWasmModules[${JSON.stringify(id)}]` | 
					
						
							|  |  |  | 		).join(", "); | 
					
						
							|  |  |  | 		const variables = Array.from( | 
					
						
							|  |  |  | 			waitForInstances.keys(), | 
					
						
							|  |  |  | 			(name, i) => `${name} = array[${i}]` | 
					
						
							|  |  |  | 		).join(", "); | 
					
						
							|  |  |  | 		return Template.asString([ | 
					
						
							|  |  |  | 			`${moduleIdStringified}: function() {`, | 
					
						
							|  |  |  | 			Template.indent([ | 
					
						
							|  |  |  | 				`return promiseResolve().then(function() { return Promise.all([${promises}]); }).then(function(array) {`, | 
					
						
							|  |  |  | 				Template.indent([`var ${variables};`, ...importObject]), | 
					
						
							|  |  |  | 				"});" | 
					
						
							|  |  |  | 			]), | 
					
						
							|  |  |  | 			"}," | 
					
						
							|  |  |  | 		]); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-07-31 04:21:27 +08:00
										 |  |  | 	return Template.asString([ | 
					
						
							|  |  |  | 		`${moduleIdStringified}: function() {`, | 
					
						
							|  |  |  | 		Template.indent(importObject), | 
					
						
							|  |  |  | 		"}," | 
					
						
							|  |  |  | 	]); | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2024-06-11 21:09:50 +08:00
										 |  |  |  * @typedef {object} WasmChunkLoadingRuntimeModuleOptions | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  |  * @property {(path: string) => string} generateLoadBinaryCode | 
					
						
							|  |  |  |  * @property {boolean} [supportsStreaming] | 
					
						
							|  |  |  |  * @property {boolean} [mangleImports] | 
					
						
							| 
									
										
										
										
											2024-03-18 23:28:40 +08:00
										 |  |  |  * @property {ReadOnlyRuntimeRequirements} runtimeRequirements | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-22 14:59:26 +08:00
										 |  |  | class WasmChunkLoadingRuntimeModule extends RuntimeModule { | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {WasmChunkLoadingRuntimeModuleOptions} options options | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2021-08-16 15:43:50 +08:00
										 |  |  | 	constructor({ | 
					
						
							|  |  |  | 		generateLoadBinaryCode, | 
					
						
							|  |  |  | 		supportsStreaming, | 
					
						
							|  |  |  | 		mangleImports, | 
					
						
							|  |  |  | 		runtimeRequirements | 
					
						
							|  |  |  | 	}) { | 
					
						
							| 
									
										
										
										
											2020-12-11 21:32:42 +08:00
										 |  |  | 		super("wasm chunk loading", RuntimeModule.STAGE_ATTACH); | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 		this.generateLoadBinaryCode = generateLoadBinaryCode; | 
					
						
							|  |  |  | 		this.supportsStreaming = supportsStreaming; | 
					
						
							|  |  |  | 		this.mangleImports = mangleImports; | 
					
						
							| 
									
										
										
										
											2021-08-16 15:43:50 +08:00
										 |  |  | 		this._runtimeRequirements = runtimeRequirements; | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 	 * @returns {string | null} runtime code | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	generate() { | 
					
						
							|  |  |  | 		const fn = RuntimeGlobals.ensureChunkHandlers; | 
					
						
							| 
									
										
										
										
											2021-08-16 15:43:50 +08:00
										 |  |  | 		const withHmr = this._runtimeRequirements.has( | 
					
						
							|  |  |  | 			RuntimeGlobals.hmrDownloadUpdateHandlers | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2023-06-12 22:21:21 +08:00
										 |  |  | 		const compilation = /** @type {Compilation} */ (this.compilation); | 
					
						
							|  |  |  | 		const { moduleGraph, outputOptions } = compilation; | 
					
						
							|  |  |  | 		const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph); | 
					
						
							|  |  |  | 		const chunk = /** @type {Chunk} */ (this.chunk); | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 		const wasmModules = getAllWasmModules(moduleGraph, chunkGraph, chunk); | 
					
						
							| 
									
										
										
										
											2023-06-12 22:21:21 +08:00
										 |  |  | 		const { mangleImports } = this; | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  | 		/** @type {string[]} */ | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 		const declarations = []; | 
					
						
							| 
									
										
										
										
											2024-07-31 11:31:11 +08:00
										 |  |  | 		const importObjects = wasmModules.map(module => | 
					
						
							|  |  |  | 			generateImportObject( | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 				chunkGraph, | 
					
						
							|  |  |  | 				module, | 
					
						
							| 
									
										
										
										
											2023-06-12 22:21:21 +08:00
										 |  |  | 				mangleImports, | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 				declarations, | 
					
						
							|  |  |  | 				chunk.runtime | 
					
						
							| 
									
										
										
										
											2024-07-31 11:31:11 +08:00
										 |  |  | 			) | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2020-07-28 23:08:22 +08:00
										 |  |  | 		const chunkModuleIdMap = chunkGraph.getChunkModuleIdMap(chunk, m => | 
					
						
							|  |  |  | 			m.type.startsWith("webassembly") | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 		); | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  | 		/** | 
					
						
							|  |  |  | 		 * @param {string} content content | 
					
						
							|  |  |  | 		 * @returns {string} created import object | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 		const createImportObject = content => | 
					
						
							|  |  |  | 			mangleImports | 
					
						
							|  |  |  | 				? `{ ${JSON.stringify(WebAssemblyUtils.MANGLED_MODULE)}: ${content} }` | 
					
						
							|  |  |  | 				: content; | 
					
						
							| 
									
										
										
										
											2019-10-04 18:24:52 +08:00
										 |  |  | 		const wasmModuleSrcPath = compilation.getPath( | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 			JSON.stringify(outputOptions.webassemblyModuleFilename), | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				hash: `" + ${RuntimeGlobals.getFullHash}() + "`, | 
					
						
							|  |  |  | 				hashWithLength: length => | 
					
						
							|  |  |  | 					`" + ${RuntimeGlobals.getFullHash}}().slice(0, ${length}) + "`, | 
					
						
							|  |  |  | 				module: { | 
					
						
							|  |  |  | 					id: '" + wasmModuleId + "', | 
					
						
							| 
									
										
										
										
											2020-07-28 23:08:22 +08:00
										 |  |  | 					hash: `" + ${JSON.stringify( | 
					
						
							|  |  |  | 						chunkGraph.getChunkModuleRenderedHashMap(chunk, m => | 
					
						
							|  |  |  | 							m.type.startsWith("webassembly") | 
					
						
							|  |  |  | 						) | 
					
						
							|  |  |  | 					)}[chunkId][wasmModuleId] + "`,
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 					hashWithLength(length) { | 
					
						
							| 
									
										
										
										
											2020-07-28 23:08:22 +08:00
										 |  |  | 						return `" + ${JSON.stringify( | 
					
						
							|  |  |  | 							chunkGraph.getChunkModuleRenderedHashMap( | 
					
						
							|  |  |  | 								chunk, | 
					
						
							|  |  |  | 								m => m.type.startsWith("webassembly"), | 
					
						
							|  |  |  | 								length | 
					
						
							|  |  |  | 							) | 
					
						
							|  |  |  | 						)}[chunkId][wasmModuleId] + "`;
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				runtime: chunk.runtime | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2021-08-16 15:43:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		const stateExpression = withHmr | 
					
						
							|  |  |  | 			? `${RuntimeGlobals.hmrRuntimeStatePrefix}_wasm` | 
					
						
							|  |  |  | 			: undefined; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 		return Template.asString([ | 
					
						
							|  |  |  | 			"// object to store loaded and loading wasm modules", | 
					
						
							| 
									
										
										
										
											2021-08-16 15:43:50 +08:00
										 |  |  | 			`var installedWasmModules = ${ | 
					
						
							|  |  |  | 				stateExpression ? `${stateExpression} = ${stateExpression} || ` : "" | 
					
						
							|  |  |  | 			}{};`,
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 			"", | 
					
						
							|  |  |  | 			// This function is used to delay reading the installed wasm module promises
 | 
					
						
							| 
									
										
										
										
											2020-03-13 00:51:26 +08:00
										 |  |  | 			// by a microtask. Sorting them doesn't help because there are edge cases where
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 			// sorting is not possible (modules splitted into different chunks).
 | 
					
						
							|  |  |  | 			// So we not even trying and solve this by a microtask delay.
 | 
					
						
							|  |  |  | 			"function promiseResolve() { return Promise.resolve(); }", | 
					
						
							|  |  |  | 			"", | 
					
						
							|  |  |  | 			Template.asString(declarations), | 
					
						
							|  |  |  | 			"var wasmImportObjects = {", | 
					
						
							|  |  |  | 			Template.indent(importObjects), | 
					
						
							|  |  |  | 			"};", | 
					
						
							|  |  |  | 			"", | 
					
						
							|  |  |  | 			`var wasmModuleMap = ${JSON.stringify( | 
					
						
							| 
									
										
										
										
											2020-07-28 23:08:22 +08:00
										 |  |  | 				chunkModuleIdMap, | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 				undefined, | 
					
						
							|  |  |  | 				"\t" | 
					
						
							|  |  |  | 			)};`,
 | 
					
						
							|  |  |  | 			"", | 
					
						
							|  |  |  | 			"// object with all WebAssembly.instance exports", | 
					
						
							|  |  |  | 			`${RuntimeGlobals.wasmInstances} = {};`, | 
					
						
							|  |  |  | 			"", | 
					
						
							|  |  |  | 			"// Fetch + compile chunk loading for webassembly", | 
					
						
							| 
									
										
										
										
											2018-11-29 21:42:16 +08:00
										 |  |  | 			`${fn}.wasm = function(chunkId, promises) {`, | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 			Template.indent([ | 
					
						
							|  |  |  | 				"", | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 				"var wasmModules = wasmModuleMap[chunkId] || [];", | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 				"", | 
					
						
							| 
									
										
										
										
											2020-07-28 23:08:22 +08:00
										 |  |  | 				"wasmModules.forEach(function(wasmModuleId, idx) {", | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 				Template.indent([ | 
					
						
							|  |  |  | 					"var installedWasmModuleData = installedWasmModules[wasmModuleId];", | 
					
						
							|  |  |  | 					"", | 
					
						
							|  |  |  | 					'// a Promise means "currently loading" or "already loaded".', | 
					
						
							|  |  |  | 					"if(installedWasmModuleData)", | 
					
						
							|  |  |  | 					Template.indent(["promises.push(installedWasmModuleData);"]), | 
					
						
							|  |  |  | 					"else {", | 
					
						
							|  |  |  | 					Template.indent([ | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 						"var importObject = wasmImportObjects[wasmModuleId]();", | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 						`var req = ${this.generateLoadBinaryCode(wasmModuleSrcPath)};`, | 
					
						
							|  |  |  | 						"var promise;", | 
					
						
							|  |  |  | 						this.supportsStreaming | 
					
						
							|  |  |  | 							? Template.asString([ | 
					
						
							| 
									
										
										
										
											2021-06-18 16:21:19 +08:00
										 |  |  | 									"if(importObject && typeof importObject.then === 'function' && typeof WebAssembly.compileStreaming === 'function') {", | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 									Template.indent([ | 
					
						
							|  |  |  | 										"promise = Promise.all([WebAssembly.compileStreaming(req), importObject]).then(function(items) {", | 
					
						
							|  |  |  | 										Template.indent([ | 
					
						
							|  |  |  | 											`return WebAssembly.instantiate(items[0], ${createImportObject( | 
					
						
							|  |  |  | 												"items[1]" | 
					
						
							|  |  |  | 											)});`
 | 
					
						
							|  |  |  | 										]), | 
					
						
							|  |  |  | 										"});" | 
					
						
							|  |  |  | 									]), | 
					
						
							|  |  |  | 									"} else if(typeof WebAssembly.instantiateStreaming === 'function') {", | 
					
						
							|  |  |  | 									Template.indent([ | 
					
						
							|  |  |  | 										`promise = WebAssembly.instantiateStreaming(req, ${createImportObject( | 
					
						
							|  |  |  | 											"importObject" | 
					
						
							|  |  |  | 										)});`
 | 
					
						
							|  |  |  | 									]) | 
					
						
							| 
									
										
										
										
											2024-07-31 05:43:19 +08:00
										 |  |  | 								]) | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 							: Template.asString([ | 
					
						
							| 
									
										
										
										
											2021-06-18 16:21:19 +08:00
										 |  |  | 									"if(importObject && typeof importObject.then === 'function') {", | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 									Template.indent([ | 
					
						
							|  |  |  | 										"var bytesPromise = req.then(function(x) { return x.arrayBuffer(); });", | 
					
						
							|  |  |  | 										"promise = Promise.all([", | 
					
						
							|  |  |  | 										Template.indent([ | 
					
						
							|  |  |  | 											"bytesPromise.then(function(bytes) { return WebAssembly.compile(bytes); }),", | 
					
						
							|  |  |  | 											"importObject" | 
					
						
							|  |  |  | 										]), | 
					
						
							|  |  |  | 										"]).then(function(items) {", | 
					
						
							|  |  |  | 										Template.indent([ | 
					
						
							|  |  |  | 											`return WebAssembly.instantiate(items[0], ${createImportObject( | 
					
						
							|  |  |  | 												"items[1]" | 
					
						
							|  |  |  | 											)});`
 | 
					
						
							|  |  |  | 										]), | 
					
						
							|  |  |  | 										"});" | 
					
						
							|  |  |  | 									]) | 
					
						
							| 
									
										
										
										
											2024-07-31 05:43:19 +08:00
										 |  |  | 								]), | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 						"} else {", | 
					
						
							|  |  |  | 						Template.indent([ | 
					
						
							|  |  |  | 							"var bytesPromise = req.then(function(x) { return x.arrayBuffer(); });", | 
					
						
							|  |  |  | 							"promise = bytesPromise.then(function(bytes) {", | 
					
						
							|  |  |  | 							Template.indent([ | 
					
						
							|  |  |  | 								`return WebAssembly.instantiate(bytes, ${createImportObject( | 
					
						
							|  |  |  | 									"importObject" | 
					
						
							|  |  |  | 								)});`
 | 
					
						
							|  |  |  | 							]), | 
					
						
							|  |  |  | 							"});" | 
					
						
							|  |  |  | 						]), | 
					
						
							|  |  |  | 						"}", | 
					
						
							|  |  |  | 						"promises.push(installedWasmModules[wasmModuleId] = promise.then(function(res) {", | 
					
						
							|  |  |  | 						Template.indent([ | 
					
						
							| 
									
										
										
										
											2019-06-13 16:51:12 +08:00
										 |  |  | 							`return ${RuntimeGlobals.wasmInstances}[wasmModuleId] = (res.instance || res).exports;` | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 						]), | 
					
						
							|  |  |  | 						"}));" | 
					
						
							|  |  |  | 					]), | 
					
						
							|  |  |  | 					"}" | 
					
						
							|  |  |  | 				]), | 
					
						
							|  |  |  | 				"});" | 
					
						
							|  |  |  | 			]), | 
					
						
							| 
									
										
										
										
											2018-11-29 21:42:16 +08:00
										 |  |  | 			"};" | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 		]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-22 14:59:26 +08:00
										 |  |  | module.exports = WasmChunkLoadingRuntimeModule; |