How to use @webassemblyjs/validation - 4 common examples

To help you get started, we’ve selected a few @webassemblyjs/validation 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 / webassemblyjs / src / compiler / compile / module.js View on Github external
export function createCompiledModule(ast: Program): CompiledModule {
  const exports: Array = [];
  const imports = [];

  let start;

  // Do compile-time ast manipulation in order to remove WAST
  // semantics during execution
  denormalizeTypeReferences(ast);
  wastIdentifierToIndex(ast);

  validateAST(ast);

  t.traverse(ast, {
    ModuleExport({ node }: NodePath) {
      if (node.descr.exportType === "Func") {
        exports.push({
          name: node.name,
          kind: "function"
        });
      }
    },

    Start({ node }: NodePath) {
      if (typeof start !== "undefined") {
        throw new CompileError("Multiple start functions is not allowed");
      }
github xtuc / webassemblyjs / packages / webassemblyjs / src / interpreter / runtime / values / global.js View on Github external
export function createInstance(
  allocator: Allocator,
  node: Global
): GlobalInstance {
  let value;
  const { valtype, mutability } = node.globalType;

  // None or multiple constant expressions in the initializer seems not possible
  // TODO(sven): find a specification reference for that
  // FIXME(sven): +1 because of the implicit end, change the order of validations
  if (node.init.length > 2 || node.init.length === 1) {
    throw new CompileError("type mismatch");
  }

  // Validate the type
  const resultInferedType = getType(node.init);

  if (
    resultInferedType != null &&
    typeEq([node.globalType.valtype], resultInferedType) === false
  ) {
    throw new CompileError("type mismatch");
  }

  const res = evaluate(allocator, node.init);

  if (res != null) {
    value = res.value;
  }

  return {
    type: valtype,
github xtuc / webassemblyjs / packages / webassemblyjs / src / interpreter / runtime / values / global.js View on Github external
let value;
  const { valtype, mutability } = node.globalType;

  // None or multiple constant expressions in the initializer seems not possible
  // TODO(sven): find a specification reference for that
  // FIXME(sven): +1 because of the implicit end, change the order of validations
  if (node.init.length > 2 || node.init.length === 1) {
    throw new CompileError("type mismatch");
  }

  // Validate the type
  const resultInferedType = getType(node.init);

  if (
    resultInferedType != null &&
    typeEq([node.globalType.valtype], resultInferedType) === false
  ) {
    throw new CompileError("type mismatch");
  }

  const res = evaluate(allocator, node.init);

  if (res != null) {
    value = res.value;
  }

  return {
    type: valtype,
    mutability,
    value
  };
}
github xtuc / webassemblyjs / packages / repl / src / index.js View on Github external
function createModuleInstanceFromAst(moduleNode, enableTypeChecking = false) {
    const internalInstanceOptions = {
      checkForI64InSignature: false,
      returnStackLocal: true
    };

    const importObject = {
      _internalInstanceOptions: internalInstanceOptions
    };

    if (enableTypeChecking === true) {
      denormalizeTypeReferences(moduleNode);

      const typeErrors = typeCheck(t.program([moduleNode]));

      if (typeErrors.length > 0) {
        const containsImmutableGlobalViolation = typeErrors.some(
          x => x === "global is immutable"
        );

        if (containsImmutableGlobalViolation) {
          throw new Error("global is immutable");
        }

        throw new Error("type mismatch");
      }
    }

    const compiledModule = createCompiledModule(moduleNode);

@webassemblyjs/validation

Module AST validations

MIT
Latest version published 1 month ago

Package Health Score

83 / 100
Full package analysis