Commit cd5644a350c66679340731cc00c9fcf16834bdb9
added globals injection script
wanderer committed on 2/20/2018, 11:42:18 PMParent: 9babf4f2b9100d276b3b4aa9195653cf32fc388d
Files changed
injectGlobals.js | added |
injectGlobals.js | ||
---|---|---|
@@ -1,0 +1,103 @@ | ||
1 | +const {findSections} = require('wasm-json-toolkit') | |
2 | +const wantedSections = ['type', 'import', 'function', 'export', 'code'] | |
3 | + | |
4 | +module.exports = function injectGlobals (json, globals) { | |
5 | + const iter = findSections(json, wantedSections) | |
6 | + const {value: type} = iter.next() | |
7 | + const getterType = type.entries.push(typeEntry()) - 1 | |
8 | + const setterType = type.entries.push(typeEntry(Array(globals.length).fill('i32'))) - 1 | |
9 | + | |
10 | + const {value: imports = {entries: []}} = iter.next() | |
11 | + const {value: func} = iter.next() | |
12 | + const getterIndex = func.entries.push(getterType) - 1 + imports.entries.length | |
13 | + const setterIndex = func.entries.push(setterType) - 1 + imports.entries.length | |
14 | + const {value: exports} = iter.next() | |
15 | + exports.entries.push(exportEntry('getter_globals', getterIndex)) | |
16 | + exports.entries.push(exportEntry('setter_globals', setterIndex)) | |
17 | + const {value: code} = iter.next() | |
18 | + const getterCode = [] | |
19 | + const setterCode = [] | |
20 | + globals.forEach((globalIndex, index) => { | |
21 | + // getter | |
22 | + getterCode.push(i32_const(index * 4)) | |
23 | + getterCode.push(get_global(globalIndex)) | |
24 | + getterCode.push(i32_store()) | |
25 | + // setter | |
26 | + setterCode.push(get_local(index)) | |
27 | + setterCode.push(set_global(globalIndex)) | |
28 | + }) | |
29 | + | |
30 | + getterCode.push(end()) | |
31 | + setterCode.push(end()) | |
32 | + code.entries.push(section_code([], getterCode)) | |
33 | + code.entries.push(section_code([], setterCode)) | |
34 | + return json | |
35 | +} | |
36 | + | |
37 | +function exportEntry (field_str, index) { | |
38 | + return { | |
39 | + field_str, | |
40 | + kind: 'function', | |
41 | + index | |
42 | + } | |
43 | +} | |
44 | + | |
45 | +function typeEntry (params = []) { | |
46 | + return { | |
47 | + form: 'func', | |
48 | + params: params | |
49 | + } | |
50 | +} | |
51 | + | |
52 | +function end () { | |
53 | + return { | |
54 | + name: 'end' | |
55 | + } | |
56 | +} | |
57 | + | |
58 | +function get_local (index) { | |
59 | + return { | |
60 | + name: 'get_local', | |
61 | + immediates: index | |
62 | + } | |
63 | +} | |
64 | + | |
65 | +function get_global (index) { | |
66 | + return { | |
67 | + name: 'get_global', | |
68 | + immediates: index | |
69 | + } | |
70 | +} | |
71 | + | |
72 | +function set_global (index) { | |
73 | + return { | |
74 | + name: 'set_global', | |
75 | + immediates: index | |
76 | + } | |
77 | +} | |
78 | + | |
79 | +function i32_const (num) { | |
80 | + return { | |
81 | + 'return_type': 'i32', | |
82 | + 'name': 'const', | |
83 | + 'immediates': num | |
84 | + } | |
85 | +} | |
86 | + | |
87 | +function i32_store () { | |
88 | + return { | |
89 | + 'return_type': 'i32', | |
90 | + 'name': 'store', | |
91 | + 'immediates': { | |
92 | + 'flags': 2, | |
93 | + 'offset': 0 | |
94 | + } | |
95 | + } | |
96 | +} | |
97 | + | |
98 | +function section_code (locals, code) { | |
99 | + return { | |
100 | + locals: locals, | |
101 | + code: code | |
102 | + } | |
103 | +} |
Built with git-ssb-web