Projekt

Obecné

Profil

Stáhnout (6.84 KB) Statistiky
| Větev: | Revize:
1
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
2

    
3
function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
4

    
5
function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return _sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }
6

    
7
import { isBlock, isFunc, isIdentifier, numberLiteralFromRaw, traverse } from "../../index";
8
import { moduleContextFromModuleAST } from "@webassemblyjs/helper-module-context"; // FIXME(sven): do the same with all block instructions, must be more generic here
9

    
10
function newUnexpectedFunction(i) {
11
  return new Error("unknown function at offset: " + i);
12
}
13

    
14
export function transform(ast) {
15
  var module;
16
  traverse(ast, {
17
    Module: function (_Module) {
18
      function Module(_x) {
19
        return _Module.apply(this, arguments);
20
      }
21

    
22
      Module.toString = function () {
23
        return _Module.toString();
24
      };
25

    
26
      return Module;
27
    }(function (path) {
28
      module = path.node;
29
    })
30
  });
31
  var moduleContext = moduleContextFromModuleAST(module); // Transform the actual instruction in function bodies
32

    
33
  traverse(ast, {
34
    Func: function (_Func) {
35
      function Func(_x2) {
36
        return _Func.apply(this, arguments);
37
      }
38

    
39
      Func.toString = function () {
40
        return _Func.toString();
41
      };
42

    
43
      return Func;
44
    }(function (path) {
45
      transformFuncPath(path, moduleContext);
46
    }),
47
    Start: function (_Start) {
48
      function Start(_x3) {
49
        return _Start.apply(this, arguments);
50
      }
51

    
52
      Start.toString = function () {
53
        return _Start.toString();
54
      };
55

    
56
      return Start;
57
    }(function (path) {
58
      var index = path.node.index;
59

    
60
      if (isIdentifier(index) === true) {
61
        var offsetInModule = moduleContext.getFunctionOffsetByIdentifier(index.value);
62

    
63
        if (typeof offsetInModule === "undefined") {
64
          throw newUnexpectedFunction(index.value);
65
        } // Replace the index Identifier
66
        // $FlowIgnore: reference?
67

    
68

    
69
        path.node.index = numberLiteralFromRaw(offsetInModule);
70
      }
71
    })
72
  });
73
}
74

    
75
function transformFuncPath(funcPath, moduleContext) {
76
  var funcNode = funcPath.node;
77
  var signature = funcNode.signature;
78

    
79
  if (signature.type !== "Signature") {
80
    throw new Error("Function signatures must be denormalised before execution");
81
  }
82

    
83
  var params = signature.params; // Add func locals in the context
84

    
85
  params.forEach(function (p) {
86
    return moduleContext.addLocal(p.valtype);
87
  });
88
  traverse(funcNode, {
89
    Instr: function (_Instr) {
90
      function Instr(_x4) {
91
        return _Instr.apply(this, arguments);
92
      }
93

    
94
      Instr.toString = function () {
95
        return _Instr.toString();
96
      };
97

    
98
      return Instr;
99
    }(function (instrPath) {
100
      var instrNode = instrPath.node;
101
      /**
102
       * Local access
103
       */
104

    
105
      if (instrNode.id === "get_local" || instrNode.id === "set_local" || instrNode.id === "tee_local") {
106
        var _instrNode$args = _slicedToArray(instrNode.args, 1),
107
            firstArg = _instrNode$args[0];
108

    
109
        if (firstArg.type === "Identifier") {
110
          var offsetInParams = params.findIndex(function (_ref) {
111
            var id = _ref.id;
112
            return id === firstArg.value;
113
          });
114

    
115
          if (offsetInParams === -1) {
116
            throw new Error("".concat(firstArg.value, " not found in ").concat(instrNode.id, ": not declared in func params"));
117
          } // Replace the Identifer node by our new NumberLiteral node
118

    
119

    
120
          instrNode.args[0] = numberLiteralFromRaw(offsetInParams);
121
        }
122
      }
123
      /**
124
       * Global access
125
       */
126

    
127

    
128
      if (instrNode.id === "get_global" || instrNode.id === "set_global") {
129
        var _instrNode$args2 = _slicedToArray(instrNode.args, 1),
130
            _firstArg = _instrNode$args2[0];
131

    
132
        if (isIdentifier(_firstArg) === true) {
133
          var globalOffset = moduleContext.getGlobalOffsetByIdentifier( // $FlowIgnore: reference?
134
          _firstArg.value);
135

    
136
          if (typeof globalOffset === "undefined") {
137
            // $FlowIgnore: reference?
138
            throw new Error("global ".concat(_firstArg.value, " not found in module"));
139
          } // Replace the Identifer node by our new NumberLiteral node
140

    
141

    
142
          instrNode.args[0] = numberLiteralFromRaw(globalOffset);
143
        }
144
      }
145
      /**
146
       * Labels lookup
147
       */
148

    
149

    
150
      if (instrNode.id === "br") {
151
        var _instrNode$args3 = _slicedToArray(instrNode.args, 1),
152
            _firstArg2 = _instrNode$args3[0];
153

    
154
        if (isIdentifier(_firstArg2) === true) {
155
          // if the labels is not found it is going to be replaced with -1
156
          // which is invalid.
157
          var relativeBlockCount = -1; // $FlowIgnore: reference?
158

    
159
          instrPath.findParent(function (_ref2) {
160
            var node = _ref2.node;
161

    
162
            if (isBlock(node)) {
163
              relativeBlockCount++; // $FlowIgnore: reference?
164

    
165
              var name = node.label || node.name;
166

    
167
              if (_typeof(name) === "object") {
168
                // $FlowIgnore: isIdentifier ensures that
169
                if (name.value === _firstArg2.value) {
170
                  // Found it
171
                  return false;
172
                }
173
              }
174
            }
175

    
176
            if (isFunc(node)) {
177
              return false;
178
            }
179
          }); // Replace the Identifer node by our new NumberLiteral node
180

    
181
          instrNode.args[0] = numberLiteralFromRaw(relativeBlockCount);
182
        }
183
      }
184
    }),
185

    
186
    /**
187
     * Func lookup
188
     */
189
    CallInstruction: function (_CallInstruction) {
190
      function CallInstruction(_x5) {
191
        return _CallInstruction.apply(this, arguments);
192
      }
193

    
194
      CallInstruction.toString = function () {
195
        return _CallInstruction.toString();
196
      };
197

    
198
      return CallInstruction;
199
    }(function (_ref3) {
200
      var node = _ref3.node;
201
      var index = node.index;
202

    
203
      if (isIdentifier(index) === true) {
204
        var offsetInModule = moduleContext.getFunctionOffsetByIdentifier(index.value);
205

    
206
        if (typeof offsetInModule === "undefined") {
207
          throw newUnexpectedFunction(index.value);
208
        } // Replace the index Identifier
209
        // $FlowIgnore: reference?
210

    
211

    
212
        node.index = numberLiteralFromRaw(offsetInModule);
213
      }
214
    })
215
  });
216
}
    (1-1/1)