How to use the @webassemblyjs/wasm-parser.decode function in @webassemblyjs/wasm-parser

To help you get started, we’ve selected a few @webassemblyjs/wasm-parser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github xtuc / webassemblyjs / packages / cli / src / get-producer-section.js View on Github external
}

const filename = process.argv[2];

if (typeof filename === "undefined") {
  throw new Error("Missing file");
}

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

// $FlowIgnore: this is correct but not correctly documented
const buff = toArrayBuffer(readFileSync(filename, null));
const ast = decode(buff, decoderOpts);

let found = false;

traverse(ast, {
  ProducersSectionMetadata({ node }) {
    node.producers.forEach(entry => {
      entry.forEach(producer => {
        console.log(producer.name, producer.version);
      });
    });

    found = true;
  }
});

if (found === false) {
github xtuc / webassemblyjs / packages / repl / src / index.js View on Github external
if (e === "\\") {
        // Start espace sequence
        const byte = chars[i + 1] + chars[i + 2];
        const hexInNumber = parseInt(byte, 16);

        out.push(hexInNumber);

        i = i + 2;
      } else {
        // ASCII
        const hexInNumber = Number(chars[i].charCodeAt(0));
        out.push(hexInNumber);
      }
    }

    decode(out);
  }
github johandb / svg-drawing-tool / node_modules / webpack / lib / wasm / WebAssemblyParser.js View on Github external
parse(binary, state) {
		// flag it as ESM
		state.module.buildMeta.exportsType = "namespace";

		// parse it
		const program = decode(binary, decoderOpts);
		const module = program.body[0];

		const moduleContext = moduleContextFromModuleAST(module);

		// extract imports and exports
		const exports = (state.module.buildMeta.providedExports = []);
		const jsIncompatibleExports = (state.module.buildMeta.jsIncompatibleExports = []);

		const importedGlobals = [];
		t.traverse(module, {
			ModuleExport({ node }) {
				const descriptor = node.descr;

				if (descriptor.exportType === "Func") {
					const funcidx = descriptor.id.value;
github johandb / svg-drawing-tool / node_modules / webpack / lib / wasm / WebAssemblyGenerator.js View on Github external
generate(module, dependencyTemplates, runtimeTemplate, type) {
		let bin = module.originalSource().source();

		const initFuncId = t.identifier(
			Array.isArray(module.usedExports)
				? Template.numberToIdentifer(module.usedExports.length)
				: "__webpack_init__"
		);

		// parse it
		const ast = decode(bin, {
			ignoreDataSection: true,
			ignoreCodeSection: true,
			ignoreCustomNameSection: true
		});

		const moduleContext = moduleContextFromModuleAST(ast.body[0]);

		const importedGlobals = getImportedGlobals(ast);
		const countImportedFunc = getCountImportedFunc(ast);
		const startAtFuncOffset = moduleContext.getStart();
		const nextFuncIndex = getNextFuncIndex(ast, countImportedFunc);
		const nextTypeIndex = getNextTypeIndex(ast);

		const usedDependencyMap = getUsedDependencyMap(
			module,
			this.options.mangleImports
github xtuc / webassemblyjs / packages / repl / src / index.js View on Github external
if (e === "\\") {
        // Start espace sequence
        const byte = chars[i + 1] + chars[i + 2];
        const hexInNumber = parseInt(byte, 16);

        out.push(hexInNumber);

        i = i + 2;
      } else {
        // ASCII
        const hexInNumber = Number(chars[i].charCodeAt(0));
        out.push(hexInNumber);
      }
    }

    decode(out);
  }
github xtuc / webassemblyjs / packages / wasm-edit / src / index.js View on Github external
export function edit(ab: ArrayBuffer, visitors: Object): ArrayBuffer {
  ab = preprocess(ab);

  const ast = decode(ab);
  return editWithAST(ast, ab, visitors);
}
github xtuc / webassemblyjs / packages / wasm-opt / src / index.js View on Github external
export function shrinkPaddedLEB128(uint8Buffer: Uint8Array): Uint8Array {
  try {
    const ast = decode(uint8Buffer.buffer, decoderOpts);
    return makeShrinkPaddedLEB128(ast, uint8Buffer);
  } catch (e) {
    throw new OptimizerError("shrinkPaddedLEB128", e);
  }
}
github TradaTech / icetea / icetea / helper / wasm.js View on Github external
exports.parseMetadata = (buffer) => {
  let importTableName
  let mainFnFound
  const operations = []
  const ast = decode(buffer, { dump: false, ignoreCodeSection: true })
  traverse(ast, {
    ModuleExport (path) {
      const fn = path.node.name
      if (fn === 'main') {
        mainFnFound = true
      } else if (!['memory', '__wbindgen_malloc', '__wbindgen_realloc', '__wbindgen_free', '__rustc_debug_gdb_scripts_section__', '__wbg_function_table'].includes(fn)) {
        operations.push(fn)
      }
    },
    ModuleImport (path) {
      if (!importTableName) {
        importTableName = path.node.module
      } else if (importTableName !== path.node.module) {
        if (path.node.module !== 'metering') {
          throw new Error('Invalid Rust wasm_bindgen WASM module: inconsistent import module name.')
        }
github xtuc / webassemblyjs / packages / cli / src / wasm2wast.js View on Github external
#!/usr/bin/env node
const { readFileSync } = require("fs");
const { decode } = require("@webassemblyjs/wasm-parser");
const { print } = require("@webassemblyjs/wast-printer");

function toArrayBuffer(buf) {
  return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
}

const filename = process.argv[2];

const buff = toArrayBuffer(readFileSync(filename, null));
const ast = decode(buff);

const wast = print(ast);

process.stdout.write(wast);

@webassemblyjs/wasm-parser

WebAssembly binary format parser

MIT
Latest version published 2 months ago

Package Health Score

88 / 100
Full package analysis