Projekt

Obecné

Profil

Stáhnout (7.07 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 _index = require("../../index");
9

    
10
var _helperModuleContext = require("@webassemblyjs/helper-module-context");
11

    
12
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); }
13

    
14
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; }
15

    
16
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"); } }
17

    
18
// FIXME(sven): do the same with all block instructions, must be more generic here
19
function newUnexpectedFunction(i) {
20
  return new Error("unknown function at offset: " + i);
21
}
22

    
23
function transform(ast) {
24
  var module;
25
  (0, _index.traverse)(ast, {
26
    Module: function (_Module) {
27
      function Module(_x) {
28
        return _Module.apply(this, arguments);
29
      }
30

    
31
      Module.toString = function () {
32
        return _Module.toString();
33
      };
34

    
35
      return Module;
36
    }(function (path) {
37
      module = path.node;
38
    })
39
  });
40
  var moduleContext = (0, _helperModuleContext.moduleContextFromModuleAST)(module); // Transform the actual instruction in function bodies
41

    
42
  (0, _index.traverse)(ast, {
43
    Func: function (_Func) {
44
      function Func(_x2) {
45
        return _Func.apply(this, arguments);
46
      }
47

    
48
      Func.toString = function () {
49
        return _Func.toString();
50
      };
51

    
52
      return Func;
53
    }(function (path) {
54
      transformFuncPath(path, moduleContext);
55
    }),
56
    Start: function (_Start) {
57
      function Start(_x3) {
58
        return _Start.apply(this, arguments);
59
      }
60

    
61
      Start.toString = function () {
62
        return _Start.toString();
63
      };
64

    
65
      return Start;
66
    }(function (path) {
67
      var index = path.node.index;
68

    
69
      if ((0, _index.isIdentifier)(index) === true) {
70
        var offsetInModule = moduleContext.getFunctionOffsetByIdentifier(index.value);
71

    
72
        if (typeof offsetInModule === "undefined") {
73
          throw newUnexpectedFunction(index.value);
74
        } // Replace the index Identifier
75
        // $FlowIgnore: reference?
76

    
77

    
78
        path.node.index = (0, _index.numberLiteralFromRaw)(offsetInModule);
79
      }
80
    })
81
  });
82
}
83

    
84
function transformFuncPath(funcPath, moduleContext) {
85
  var funcNode = funcPath.node;
86
  var signature = funcNode.signature;
87

    
88
  if (signature.type !== "Signature") {
89
    throw new Error("Function signatures must be denormalised before execution");
90
  }
91

    
92
  var params = signature.params; // Add func locals in the context
93

    
94
  params.forEach(function (p) {
95
    return moduleContext.addLocal(p.valtype);
96
  });
97
  (0, _index.traverse)(funcNode, {
98
    Instr: function (_Instr) {
99
      function Instr(_x4) {
100
        return _Instr.apply(this, arguments);
101
      }
102

    
103
      Instr.toString = function () {
104
        return _Instr.toString();
105
      };
106

    
107
      return Instr;
108
    }(function (instrPath) {
109
      var instrNode = instrPath.node;
110
      /**
111
       * Local access
112
       */
113

    
114
      if (instrNode.id === "get_local" || instrNode.id === "set_local" || instrNode.id === "tee_local") {
115
        var _instrNode$args = _slicedToArray(instrNode.args, 1),
116
            firstArg = _instrNode$args[0];
117

    
118
        if (firstArg.type === "Identifier") {
119
          var offsetInParams = params.findIndex(function (_ref) {
120
            var id = _ref.id;
121
            return id === firstArg.value;
122
          });
123

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

    
128

    
129
          instrNode.args[0] = (0, _index.numberLiteralFromRaw)(offsetInParams);
130
        }
131
      }
132
      /**
133
       * Global access
134
       */
135

    
136

    
137
      if (instrNode.id === "get_global" || instrNode.id === "set_global") {
138
        var _instrNode$args2 = _slicedToArray(instrNode.args, 1),
139
            _firstArg = _instrNode$args2[0];
140

    
141
        if ((0, _index.isIdentifier)(_firstArg) === true) {
142
          var globalOffset = moduleContext.getGlobalOffsetByIdentifier( // $FlowIgnore: reference?
143
          _firstArg.value);
144

    
145
          if (typeof globalOffset === "undefined") {
146
            // $FlowIgnore: reference?
147
            throw new Error("global ".concat(_firstArg.value, " not found in module"));
148
          } // Replace the Identifer node by our new NumberLiteral node
149

    
150

    
151
          instrNode.args[0] = (0, _index.numberLiteralFromRaw)(globalOffset);
152
        }
153
      }
154
      /**
155
       * Labels lookup
156
       */
157

    
158

    
159
      if (instrNode.id === "br") {
160
        var _instrNode$args3 = _slicedToArray(instrNode.args, 1),
161
            _firstArg2 = _instrNode$args3[0];
162

    
163
        if ((0, _index.isIdentifier)(_firstArg2) === true) {
164
          // if the labels is not found it is going to be replaced with -1
165
          // which is invalid.
166
          var relativeBlockCount = -1; // $FlowIgnore: reference?
167

    
168
          instrPath.findParent(function (_ref2) {
169
            var node = _ref2.node;
170

    
171
            if ((0, _index.isBlock)(node)) {
172
              relativeBlockCount++; // $FlowIgnore: reference?
173

    
174
              var name = node.label || node.name;
175

    
176
              if (_typeof(name) === "object") {
177
                // $FlowIgnore: isIdentifier ensures that
178
                if (name.value === _firstArg2.value) {
179
                  // Found it
180
                  return false;
181
                }
182
              }
183
            }
184

    
185
            if ((0, _index.isFunc)(node)) {
186
              return false;
187
            }
188
          }); // Replace the Identifer node by our new NumberLiteral node
189

    
190
          instrNode.args[0] = (0, _index.numberLiteralFromRaw)(relativeBlockCount);
191
        }
192
      }
193
    }),
194

    
195
    /**
196
     * Func lookup
197
     */
198
    CallInstruction: function (_CallInstruction) {
199
      function CallInstruction(_x5) {
200
        return _CallInstruction.apply(this, arguments);
201
      }
202

    
203
      CallInstruction.toString = function () {
204
        return _CallInstruction.toString();
205
      };
206

    
207
      return CallInstruction;
208
    }(function (_ref3) {
209
      var node = _ref3.node;
210
      var index = node.index;
211

    
212
      if ((0, _index.isIdentifier)(index) === true) {
213
        var offsetInModule = moduleContext.getFunctionOffsetByIdentifier(index.value);
214

    
215
        if (typeof offsetInModule === "undefined") {
216
          throw newUnexpectedFunction(index.value);
217
        } // Replace the index Identifier
218
        // $FlowIgnore: reference?
219

    
220

    
221
        node.index = (0, _index.numberLiteralFromRaw)(offsetInModule);
222
      }
223
    })
224
  });
225
}
    (1-1/1)