| 
									
										
										
										
											2021-09-23 18:55:18 +08:00
										 |  |  | const path = require("path"); | 
					
						
							|  |  |  | const fs = require("fs"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // When --write is set, files will be written in place
 | 
					
						
							|  |  |  | // Otherwise it only prints outdated files
 | 
					
						
							|  |  |  | const doWrite = process.argv.includes("--write"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 00:13:49 +08:00
										 |  |  | const files = ["lib/util/hash/xxhash64.js", "lib/util/hash/md4.js"]; | 
					
						
							| 
									
										
										
										
											2021-09-23 18:55:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | (async () => { | 
					
						
							| 
									
										
										
										
											2023-04-08 09:01:26 +08:00
										 |  |  | 	// TODO: fix me after update typescript to v5
 | 
					
						
							|  |  |  | 	// eslint-disable-next-line no-warning-comments
 | 
					
						
							|  |  |  | 	// @ts-ignore
 | 
					
						
							| 
									
										
										
										
											2024-01-13 21:28:53 +08:00
										 |  |  | 	// eslint-disable-next-line n/no-unsupported-features/es-syntax
 | 
					
						
							| 
									
										
										
										
											2023-04-08 09:01:26 +08:00
										 |  |  | 	const asc = (await import("assemblyscript/asc")).default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-23 18:55:18 +08:00
										 |  |  | 	for (const file of files) { | 
					
						
							|  |  |  | 		const filePath = path.resolve(__dirname, "..", file); | 
					
						
							|  |  |  | 		const content = fs.readFileSync(filePath, "utf-8"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const regexp = | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 			/\n\/\/[\s]*#region wasm code: (.+) \((.+)\)(.*)\n[\s\S]+?\/\/[\s+]*#endregion\n/g; | 
					
						
							| 
									
										
										
										
											2021-09-23 18:55:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		const replaces = new Map(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		let match = regexp.exec(content); | 
					
						
							|  |  |  | 		while (match) { | 
					
						
							|  |  |  | 			const [fullMatch, identifier, name, flags] = match; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const sourcePath = path.resolve(filePath, "..", name); | 
					
						
							|  |  |  | 			const sourcePathBase = path.join( | 
					
						
							|  |  |  | 				path.dirname(sourcePath), | 
					
						
							|  |  |  | 				path.basename(sourcePath) | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 09:01:26 +08:00
										 |  |  | 			const { error } = await asc.main( | 
					
						
							|  |  |  | 				[ | 
					
						
							|  |  |  | 					sourcePath, | 
					
						
							|  |  |  | 					// cspell:word Ospeed
 | 
					
						
							|  |  |  | 					"-Ospeed", | 
					
						
							|  |  |  | 					"--noAssert", | 
					
						
							|  |  |  | 					"--converge", | 
					
						
							|  |  |  | 					"--textFile", | 
					
						
							| 
									
										
										
										
											2024-07-31 10:39:30 +08:00
										 |  |  | 					`${sourcePathBase}.wat`, | 
					
						
							| 
									
										
										
										
											2023-04-08 09:01:26 +08:00
										 |  |  | 					"--outFile", | 
					
						
							| 
									
										
										
										
											2024-07-31 10:39:30 +08:00
										 |  |  | 					`${sourcePathBase}.wasm`, | 
					
						
							| 
									
										
										
										
											2023-04-08 09:01:26 +08:00
										 |  |  | 					...flags.split(" ").filter(Boolean) | 
					
						
							|  |  |  | 				], | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					stdout: process.stdout, | 
					
						
							|  |  |  | 					stderr: process.stderr | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (error) { | 
					
						
							|  |  |  | 				throw error; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-09-23 18:55:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-31 10:39:30 +08:00
										 |  |  | 			const wasm = fs.readFileSync(`${sourcePathBase}.wasm`); | 
					
						
							| 
									
										
										
										
											2021-09-23 18:55:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			replaces.set( | 
					
						
							|  |  |  | 				fullMatch, | 
					
						
							|  |  |  | 				`
 | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | // #region wasm code: ${identifier} (${name})${flags}
 | 
					
						
							| 
									
										
										
										
											2021-09-23 18:55:18 +08:00
										 |  |  | const ${identifier} = new WebAssembly.Module( | 
					
						
							|  |  |  | 	Buffer.from( | 
					
						
							|  |  |  | 		// ${wasm.length} bytes
 | 
					
						
							|  |  |  | 		${JSON.stringify(wasm.toString("base64"))}, | 
					
						
							|  |  |  | 		"base64" | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | ); | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | // #endregion
 | 
					
						
							| 
									
										
										
										
											2021-09-23 18:55:18 +08:00
										 |  |  | `
 | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			match = regexp.exec(content); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const newContent = content.replace(regexp, match => replaces.get(match)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (newContent !== content) { | 
					
						
							|  |  |  | 			if (doWrite) { | 
					
						
							|  |  |  | 				fs.writeFileSync(filePath, newContent, "utf-8"); | 
					
						
							|  |  |  | 				console.error(`${file} updated`); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				console.error(`${file} need to be updated`); | 
					
						
							|  |  |  | 				process.exitCode = 1; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | })(); |