| 
									
										
										
										
											2017-10-30 20:56:57 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-12-14 08:21:44 +08:00
										 |  |  | /* globals WebAssembly */ | 
					
						
							| 
									
										
										
										
											2017-10-30 20:56:57 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Syntax: https://developer.mozilla.org/en/SpiderMonkey/Parser_API
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 23:54:26 +08:00
										 |  |  | const Tapable = require("tapable").Tapable; | 
					
						
							| 
									
										
										
										
											2017-12-15 22:39:53 +08:00
										 |  |  | const WebAssemblyImportDependency = require("./dependencies/WebAssemblyImportDependency"); | 
					
						
							| 
									
										
										
										
											2017-10-30 20:56:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class WebAssemblyParser extends Tapable { | 
					
						
							|  |  |  | 	constructor(options) { | 
					
						
							|  |  |  | 		super(); | 
					
						
							| 
									
										
										
										
											2017-11-28 23:54:26 +08:00
										 |  |  | 		this.hooks = {}; | 
					
						
							| 
									
										
										
										
											2017-10-30 20:56:57 +08:00
										 |  |  | 		this.options = options; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 08:21:44 +08:00
										 |  |  | 	parse(source, state, callback) { | 
					
						
							| 
									
										
										
										
											2017-10-30 20:56:57 +08:00
										 |  |  | 		// TODO parse WASM AST and walk it
 | 
					
						
							| 
									
										
										
										
											2017-12-14 08:21:44 +08:00
										 |  |  | 		// TODO extract imports
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// flag it as ESM
 | 
					
						
							| 
									
										
										
										
											2017-12-23 01:23:20 +08:00
										 |  |  | 		state.module.buildMeta.exportsType = "namespace"; | 
					
						
							| 
									
										
										
										
											2017-12-14 08:21:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// extract exports
 | 
					
						
							|  |  |  | 		// TODO find more efficient way doing it
 | 
					
						
							|  |  |  | 		// TODO use Promises
 | 
					
						
							|  |  |  | 		if(typeof WebAssembly !== "undefined") { | 
					
						
							|  |  |  | 			WebAssembly.compile(source).then(module => { | 
					
						
							|  |  |  | 				state.module.buildMeta.providedExports = WebAssembly.Module.exports(module).map(exp => exp.name); | 
					
						
							| 
									
										
										
										
											2017-12-15 22:39:53 +08:00
										 |  |  | 				for(const imp of WebAssembly.Module.imports(module)) { | 
					
						
							|  |  |  | 					const dep = new WebAssemblyImportDependency(imp.module, imp.name, imp.kind); | 
					
						
							|  |  |  | 					state.module.addDependency(dep); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-12-14 08:21:44 +08:00
										 |  |  | 			}).then(() => callback(null, state), err => callback(err)); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2017-12-15 22:39:53 +08:00
										 |  |  | 			throw new Error("Can't compile WebAssembly modules without WebAssembly support in current node.js version (Update to latest node.js version)"); | 
					
						
							| 
									
										
										
										
											2017-12-14 08:21:44 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-10-30 20:56:57 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = WebAssemblyParser; |