保誠-保戶業務員媒合平台
Tomas
2022-05-19 957a1f10a06fdbb76f1a0ba94fe44126c613fee3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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);
    }
  });
}