1
|
var t = require("../../index"); // func and call_indirect instructions can either define a signature inline, or
|
2
|
// reference a signature, e.g.
|
3
|
//
|
4
|
// ;; inline signature
|
5
|
// (func (result i64)
|
6
|
// (i64.const 2)
|
7
|
// )
|
8
|
// ;; signature reference
|
9
|
// (type (func (result i64)))
|
10
|
// (func (type 0)
|
11
|
// (i64.const 2))
|
12
|
// )
|
13
|
//
|
14
|
// this AST transform denormalises the type references, making all signatures within the module
|
15
|
// inline.
|
16
|
|
17
|
|
18
|
export function transform(ast) {
|
19
|
var typeInstructions = [];
|
20
|
t.traverse(ast, {
|
21
|
TypeInstruction: function TypeInstruction(_ref) {
|
22
|
var node = _ref.node;
|
23
|
typeInstructions.push(node);
|
24
|
}
|
25
|
});
|
26
|
|
27
|
if (!typeInstructions.length) {
|
28
|
return;
|
29
|
}
|
30
|
|
31
|
function denormalizeSignature(signature) {
|
32
|
// signature referenced by identifier
|
33
|
if (signature.type === "Identifier") {
|
34
|
var identifier = signature;
|
35
|
var typeInstruction = typeInstructions.find(function (t) {
|
36
|
return t.id.type === identifier.type && t.id.value === identifier.value;
|
37
|
});
|
38
|
|
39
|
if (!typeInstruction) {
|
40
|
throw new Error("A type instruction reference was not found ".concat(JSON.stringify(signature)));
|
41
|
}
|
42
|
|
43
|
return typeInstruction.functype;
|
44
|
} // signature referenced by index
|
45
|
|
46
|
|
47
|
if (signature.type === "NumberLiteral") {
|
48
|
var signatureRef = signature;
|
49
|
var _typeInstruction = typeInstructions[signatureRef.value];
|
50
|
return _typeInstruction.functype;
|
51
|
}
|
52
|
|
53
|
return signature;
|
54
|
}
|
55
|
|
56
|
t.traverse(ast, {
|
57
|
Func: function (_Func) {
|
58
|
function Func(_x) {
|
59
|
return _Func.apply(this, arguments);
|
60
|
}
|
61
|
|
62
|
Func.toString = function () {
|
63
|
return _Func.toString();
|
64
|
};
|
65
|
|
66
|
return Func;
|
67
|
}(function (_ref2) {
|
68
|
var node = _ref2.node;
|
69
|
node.signature = denormalizeSignature(node.signature);
|
70
|
}),
|
71
|
CallIndirectInstruction: function CallIndirectInstruction(_ref3) {
|
72
|
var node = _ref3.node;
|
73
|
node.signature = denormalizeSignature(node.signature);
|
74
|
}
|
75
|
});
|
76
|
}
|