/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author Tobias Koppers @sokra
*/
"use strict";

const traverse = require("@webassemblyjs/ast").traverse;
const decode = require("@webassemblyjs/wasm-parser").decode;

const Tapable = require("tapable").Tapable;
const WebAssemblyImportDependency = require("./dependencies/WebAssemblyImportDependency");

const decoderOpts = {
	ignoreCodeSection: true,
	ignoreDataSection: true,
};

class WebAssemblyParser extends Tapable {
	constructor(options) {
		super();
		this.hooks = {};
		this.options = options;
	}

	parse(binary, state) {
		// flag it as ESM
		state.module.buildMeta.exportsType = "namespace";

		// parse it
		const ast = decode(binary, decoderOpts);

		// extract imports and exports
		const exports = state.module.buildMeta.providedExports = [];
		traverse(ast, {
			ModuleExport({
				node
			}) {
				exports.push(node.name);
			},

			ModuleImport({
				node
			}) {
				const dep = new WebAssemblyImportDependency(node.module, node.name);
				state.module.addDependency(dep);
			}
		});

		return state;
	}
}

module.exports = WebAssemblyParser;

@@ -114,24 +114,27 @@
 %0A%0Aconst 
+%7B%0A%09
 traverse
  = requi
@@ -125,16 +125,18 @@
 traverse
+%0A%7D
  = requi
@@ -163,25 +163,16 @@
 st%22)
-.traverse
 ;%0Aconst 
 deco
@@ -167,22 +167,27 @@
 ;%0Aconst 
+%7B%0A%09
 decode
+%0A%7D
  = requi
@@ -222,15 +222,8 @@
 er%22)
-.decode
 ;%0A%0Ac
@@ -227,23 +227,28 @@
 %0A%0Aconst 
+%7B%0A%09
 Tapable
+%0A%7D
  = requi
@@ -264,16 +264,8 @@
 le%22)
-.Tapable
 ;%0Aco

ES2015'ify