Sha256: 95f2d32dde538b7e2114a198fe9fe3b3bdeb78b1b5997e3d3aa6dc4c6d6870ce

Contents?: true

Size: 1.94 KB

Versions: 27

Compression:

Stored size: 1.94 KB

Contents

var t = require("../../index"); // func and call_indirect instructions can either define a signature inline, or
// reference a signature, e.g.
//
// ;; inline signature
// (func (result i64)
//   (i64.const 2)
// )
// ;; signature reference
// (type (func (result i64)))
// (func (type 0)
//   (i64.const 2))
// )
//
// this AST transform denormalises the type references, making all signatures within the module
// inline.


export function transform(ast) {
  var typeInstructions = [];
  t.traverse(ast, {
    TypeInstruction: function TypeInstruction(_ref) {
      var node = _ref.node;
      typeInstructions.push(node);
    }
  });

  if (!typeInstructions.length) {
    return;
  }

  function denormalizeSignature(signature) {
    // signature referenced by identifier
    if (signature.type === "Identifier") {
      var identifier = signature;
      var typeInstruction = typeInstructions.find(function (t) {
        return t.id.type === identifier.type && t.id.value === identifier.value;
      });

      if (!typeInstruction) {
        throw new Error("A type instruction reference was not found ".concat(JSON.stringify(signature)));
      }

      return typeInstruction.functype;
    } // signature referenced by index


    if (signature.type === "NumberLiteral") {
      var signatureRef = signature;
      var _typeInstruction = typeInstructions[signatureRef.value];
      return _typeInstruction.functype;
    }

    return signature;
  }

  t.traverse(ast, {
    Func: function (_Func) {
      function Func(_x) {
        return _Func.apply(this, arguments);
      }

      Func.toString = function () {
        return _Func.toString();
      };

      return Func;
    }(function (_ref2) {
      var node = _ref2.node;
      node.signature = denormalizeSignature(node.signature);
    }),
    CallIndirectInstruction: function CallIndirectInstruction(_ref3) {
      var node = _ref3.node;
      node.signature = denormalizeSignature(node.signature);
    }
  });
}

Version data entries

27 entries across 26 versions & 9 rubygems

Version Path
ezii-os-5.2.1 node_modules/@webassemblyjs/ast/esm/transform/denormalize-type-references/index.js
ezii-os-2.0.1 node_modules/@webassemblyjs/ast/esm/transform/denormalize-type-references/index.js
ezii-os-1.1.0 node_modules/@webassemblyjs/ast/esm/transform/denormalize-type-references/index.js
ezii-os-1.0.0 node_modules/@webassemblyjs/ast/esm/transform/denormalize-type-references/index.js
ezii-os-0.0.0.1.0 node_modules/@webassemblyjs/ast/esm/transform/denormalize-type-references/index.js
ezii-os-0.0.0.0.1 node_modules/@webassemblyjs/ast/esm/transform/denormalize-type-references/index.js
locomotivecms-4.0.0.alpha1 app/javascript/node_modules/@webassemblyjs/ast/esm/transform/denormalize-type-references/index.js