Projekt

Obecné

Profil

Stáhnout (2.04 KB) Statistiky
| Větev: | Revize:
1
"use strict";
2

    
3
Object.defineProperty(exports, "__esModule", {
4
  value: true
5
});
6
exports.transform = transform;
7

    
8
var t = require("../../index"); // func and call_indirect instructions can either define a signature inline, or
9
// reference a signature, e.g.
10
//
11
// ;; inline signature
12
// (func (result i64)
13
//   (i64.const 2)
14
// )
15
// ;; signature reference
16
// (type (func (result i64)))
17
// (func (type 0)
18
//   (i64.const 2))
19
// )
20
//
21
// this AST transform denormalises the type references, making all signatures within the module
22
// inline.
23

    
24

    
25
function transform(ast) {
26
  var typeInstructions = [];
27
  t.traverse(ast, {
28
    TypeInstruction: function TypeInstruction(_ref) {
29
      var node = _ref.node;
30
      typeInstructions.push(node);
31
    }
32
  });
33

    
34
  if (!typeInstructions.length) {
35
    return;
36
  }
37

    
38
  function denormalizeSignature(signature) {
39
    // signature referenced by identifier
40
    if (signature.type === "Identifier") {
41
      var identifier = signature;
42
      var typeInstruction = typeInstructions.find(function (t) {
43
        return t.id.type === identifier.type && t.id.value === identifier.value;
44
      });
45

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

    
50
      return typeInstruction.functype;
51
    } // signature referenced by index
52

    
53

    
54
    if (signature.type === "NumberLiteral") {
55
      var signatureRef = signature;
56
      var _typeInstruction = typeInstructions[signatureRef.value];
57
      return _typeInstruction.functype;
58
    }
59

    
60
    return signature;
61
  }
62

    
63
  t.traverse(ast, {
64
    Func: function (_Func) {
65
      function Func(_x) {
66
        return _Func.apply(this, arguments);
67
      }
68

    
69
      Func.toString = function () {
70
        return _Func.toString();
71
      };
72

    
73
      return Func;
74
    }(function (_ref2) {
75
      var node = _ref2.node;
76
      node.signature = denormalizeSignature(node.signature);
77
    }),
78
    CallIndirectInstruction: function CallIndirectInstruction(_ref3) {
79
      var node = _ref3.node;
80
      node.signature = denormalizeSignature(node.signature);
81
    }
82
  });
83
}
    (1-1/1)