Files: c1baf026f5fba05cb5619603b350445d6e40a0d5 / customTypes.js
5231 bytesRaw
1 | const Stream = require('buffer-pipe') |
2 | const leb = require('leb128') |
3 | |
4 | const LANGUAGE_TYPES = { |
5 | 'actor': 0x0, |
6 | 'buf': 0x1, |
7 | 'i32': 0x7f, |
8 | 'i64': 0x7e, |
9 | 'f32': 0x7d, |
10 | 'f64': 0x7c, |
11 | 'anyFunc': 0x70, |
12 | 'func': 0x60, |
13 | 'block_type': 0x40, |
14 | |
15 | 0x0: 'actor', |
16 | 0x1: 'buf', |
17 | 0x7f: 'i32', |
18 | 0x7e: 'i64', |
19 | 0x7d: 'f32', |
20 | 0x7c: 'f64', |
21 | 0x70: 'anyFunc', |
22 | 0x60: 'func', |
23 | 0x40: 'block_type' |
24 | } |
25 | |
26 | function encodeJSON (json) { |
27 | const stream = new Stream() |
28 | encodeCustomSection('type', json, stream, encodeType) |
29 | encodeCustomSection('typeMap', json, stream, encodeTypeMap) |
30 | |
31 | return stream.buffer |
32 | } |
33 | |
34 | function encodeCustomSection (name, json, stream, encodingFunc) { |
35 | stream.write([0]) |
36 | let payload = new Stream() |
37 | |
38 | // encode type |
39 | leb.unsigned.write(name.length, payload) |
40 | payload.write(name) |
41 | encodingFunc(json[name], payload) |
42 | // write the size of the payload |
43 | leb.unsigned.write(payload.bytesWrote, stream) |
44 | stream.write(payload.buffer) |
45 | return stream |
46 | } |
47 | |
48 | function encodeTypeMap (json, stream) { |
49 | leb.unsigned.write(json.length, stream) |
50 | for (let entry of json) { |
51 | leb.unsigned.write(entry.func, stream) |
52 | leb.unsigned.write(entry.type, stream) |
53 | } |
54 | return stream |
55 | } |
56 | |
57 | function decodeTypeMap (buf) { |
58 | const stream = new Stream(Buffer.from(buf)) |
59 | let numOfEntries = leb.unsigned.read(stream) |
60 | const json = [] |
61 | while (numOfEntries--) { |
62 | json.push({ |
63 | func: leb.unsigned.readBn(stream).toNumber(), |
64 | type: leb.unsigned.readBn(stream).toNumber() |
65 | }) |
66 | } |
67 | return json |
68 | } |
69 | |
70 | function encodeType (json, stream = new Stream()) { |
71 | let binEntries = new Stream() |
72 | |
73 | leb.unsigned.write(json.length, binEntries) |
74 | for (let entry of json) { |
75 | // a single type entry binary encoded |
76 | binEntries.write([LANGUAGE_TYPES[entry.form]]) // the form |
77 | |
78 | const len = entry.params.length // number of parameters |
79 | leb.unsigned.write(len, binEntries) |
80 | if (len !== 0) { |
81 | binEntries.write(entry.params.map(type => LANGUAGE_TYPES[type])) // the paramter types |
82 | } |
83 | |
84 | binEntries.write([entry.return_type ? 1 : 0]) // number of return types |
85 | |
86 | if (entry.return_type) { |
87 | binEntries.write([LANGUAGE_TYPES[entry.return_type]]) |
88 | } |
89 | } |
90 | |
91 | stream.write(binEntries.buffer) |
92 | return stream |
93 | } |
94 | |
95 | function decodeType (buf) { |
96 | const stream = new Stream(Buffer.from(buf)) |
97 | const numberOfEntries = leb.unsigned.readBn(stream).toNumber() |
98 | const json = [] |
99 | for (let i = 0; i < numberOfEntries; i++) { |
100 | let type = stream.read(1)[0] |
101 | const entry = { |
102 | form: LANGUAGE_TYPES[type], |
103 | params: [] |
104 | } |
105 | |
106 | let paramCount = leb.unsigned.readBn(stream).toNumber() |
107 | |
108 | // parse the entries |
109 | while (paramCount--) { |
110 | const type = stream.read(1)[0] |
111 | entry.params.push(LANGUAGE_TYPES[type]) |
112 | } |
113 | const numOfReturns = leb.unsigned.readBn(stream).toNumber() |
114 | if (numOfReturns) { |
115 | type = stream.read(1)[0] |
116 | entry.return_type = LANGUAGE_TYPES[type] |
117 | } |
118 | |
119 | json.push(entry) |
120 | } |
121 | return json |
122 | } |
123 | |
124 | function injectCustomSection (custom, wasm) { |
125 | const preramble = wasm.subarray(0, 8) |
126 | const body = wasm.subarray(8) |
127 | return Buffer.concat([preramble, custom, body]) |
128 | } |
129 | |
130 | function inject (wasm, json) { |
131 | const buf = encodeJSON(json) |
132 | return injectCustomSection(buf, wasm) |
133 | } |
134 | |
135 | function mergeTypeSections (json) { |
136 | const result = { |
137 | types: [], |
138 | indexes: {}, |
139 | exports: {} |
140 | } |
141 | const iterator = findSections(json) |
142 | const mappedFuncs = new Map() |
143 | const mappedTypes = new Map() |
144 | iterator.next() |
145 | const {value: customType} = iterator.next('custom') |
146 | if (customType) { |
147 | const type = decodeType(customType.payload) |
148 | result.types = type |
149 | } |
150 | let {value: typeMap} = iterator.next('custom') |
151 | if (typeMap) { |
152 | decodeTypeMap(typeMap.payload).forEach(map => mappedFuncs.set(map.func, map.type)) |
153 | } |
154 | |
155 | const {value: type} = iterator.next('type') |
156 | const {value: imports = {entries: []}} = iterator.next('import') |
157 | const {value: functions} = iterator.next('function') |
158 | functions.entries.forEach((typeIndex, funcIndex) => { |
159 | let customIndex = mappedFuncs.get(funcIndex) |
160 | if (customIndex === undefined) { |
161 | customIndex = mappedTypes.get(typeIndex) |
162 | } |
163 | if (customIndex === undefined) { |
164 | customIndex = result.types.push(type.entries[typeIndex]) - 1 |
165 | mappedTypes.set(typeIndex, customIndex) |
166 | } |
167 | result.indexes[funcIndex + imports.entries.length] = customIndex |
168 | }) |
169 | |
170 | const {value: exports = {entries: []}} = iterator.next('export') |
171 | exports.entries.forEach(entry => { |
172 | if (entry.kind === 'function') { |
173 | result.exports[entry.field_str] = entry.index |
174 | } |
175 | }) |
176 | return result |
177 | } |
178 | |
179 | const wantedSections = new Set(['custom', 'type', 'function', 'export', 'import']) |
180 | |
181 | function * findSections (array) { |
182 | let section = array[0] |
183 | let index = 0 |
184 | let nextSection = yield null |
185 | |
186 | while (section) { |
187 | if (!wantedSections.has(section.name)) { |
188 | index++ |
189 | section = array[index] |
190 | } else { |
191 | if (section.name === nextSection) { |
192 | nextSection = yield section |
193 | index++ |
194 | section = array[index] |
195 | } else { |
196 | nextSection = yield null |
197 | } |
198 | } |
199 | } |
200 | } |
201 | |
202 | module.exports = { |
203 | injectCustomSection, |
204 | inject, |
205 | decodeType, |
206 | decodeTypeMap, |
207 | encodeType, |
208 | encodeTypeMap, |
209 | encodeJSON, |
210 | mergeTypeSections |
211 | } |
212 |
Built with git-ssb-web