wasmContainer.jsView |
---|
35 | 35 | 0x40: 'block_type' |
36 | 36 | } |
37 | 37 | |
38 | 38 | class FunctionRef { |
39 | | - constructor (location, identifier, json, id) { |
| 39 | + constructor (location, identifier, params, id) { |
40 | 40 | this.location = location |
41 | 41 | this.destId = id |
42 | | - let funcIndex |
43 | | - if (location === 'export') { |
44 | | - this.indentifier = identifier |
45 | | - funcIndex = json.exports[identifier] |
46 | | - } else { |
47 | | - this.indentifier = identifier.tableIndex |
48 | | - funcIndex = Number(identifier.name) - 1 |
49 | | - } |
50 | | - const typeIndex = json.indexes[funcIndex] |
51 | | - this.type = json.types[typeIndex] |
| 42 | + this.params = params |
| 43 | + this.identifier = identifier |
52 | 44 | |
53 | | - const wrapper = typeCheckWrapper(this.type) |
| 45 | + const wrapper = typeCheckWrapper(params) |
54 | 46 | const wasm = json2wasm(wrapper) |
55 | 47 | const mod = WebAssembly.Module(wasm) |
56 | 48 | const self = this |
57 | 49 | this.wrapper = WebAssembly.Instance(mod, { |
82 | 74 | } |
83 | 75 | } |
84 | 76 | |
85 | 77 | class ModuleRef { |
86 | | - constructor (json, id) { |
87 | | - this._json = json |
| 78 | + constructor (ex, id) { |
| 79 | + this.exports = ex |
88 | 80 | this.id = id |
89 | 81 | } |
90 | 82 | |
91 | 83 | getFuncRef (name) { |
92 | | - return new FunctionRef('export', name, this._json, this.id) |
| 84 | + return new FunctionRef('export', name, this.exports[name], this.id) |
93 | 85 | } |
94 | 86 | |
95 | | - serialize () { |
96 | | - return this._json |
| 87 | + encodeCBOR (gen) { |
| 88 | + return gen.write({ |
| 89 | + '#': { |
| 90 | + exports: this.exports, |
| 91 | + '@': { |
| 92 | + id: this.id |
| 93 | + } |
| 94 | + } |
| 95 | + }) |
97 | 96 | } |
98 | 97 | |
| 98 | + static fromMetaJSON (json, id) { |
| 99 | + const exports = {} |
| 100 | + for (const ex in json.exports) { |
| 101 | + const type = json.types[json.indexes[json.exports[ex].toString()]].params |
| 102 | + exports[ex] = type |
| 103 | + } |
| 104 | + return new ModuleRef(exports, id) |
| 105 | + } |
| 106 | + |
99 | 107 | static deserialize (serialized) {} |
100 | 108 | } |
101 | 109 | |
102 | 110 | module.exports = class WasmContainer { |
125 | 133 | new Promise((resolve, reject) => { |
126 | 134 | cachedb.put(id.toString() + 'code', wasm.toString('hex'), resolve) |
127 | 135 | }) |
128 | 136 | ]) |
129 | | - return new ModuleRef(json, id) |
| 137 | + return ModuleRef.fromMetaJSON(json, id) |
130 | 138 | } |
131 | 139 | |
132 | 140 | getInterface (funcRef) { |
133 | 141 | const self = this |
143 | 151 | return self.refs.add(ref) |
144 | 152 | } |
145 | 153 | }, |
146 | 154 | internalize: (ref, index) => { |
147 | | - const funcRef = self.refs.get(ref) |
| 155 | + const funcRef = self.refs.get(ref, 'func') |
148 | 156 | funcRef.container = self |
149 | 157 | this.instance.exports.table.set(index, funcRef.wrapper.exports.check) |
150 | 158 | }, |
151 | 159 | catch: (ref, catchRef) => { |
178 | 186 | const funcRef = mod.getFuncRef(name) |
179 | 187 | return this.refs.add(funcRef, 'func') |
180 | 188 | }, |
181 | 189 | self: () => { |
182 | | - return this.refs.add(this.moduleObj, 'mod') |
| 190 | + return this.refs.add(this.modSelf, 'mod') |
183 | 191 | } |
184 | 192 | }, |
185 | 193 | memory: { |
186 | 194 | externalize: (index, length) => { |
223 | 231 | } |
224 | 232 | } |
225 | 233 | |
226 | 234 | async onMessage (message) { |
| 235 | + try { |
227 | 236 | const funcRef = message.funcRef |
228 | 237 | const intef = this.getInterface(funcRef) |
229 | 238 | this.instance = WebAssembly.Instance(this.mod, intef) |
230 | 239 | const table = this.instance.exports.table |
237 | 246 | } |
238 | 247 | } |
239 | 248 | } |
240 | 249 | const args = message.funcArguments.map((arg, index) => { |
241 | | - const type = funcRef.type.params[index] |
| 250 | + const type = funcRef.params[index] |
242 | 251 | if (nativeTypes.has(type)) { |
243 | 252 | return arg |
244 | 253 | } else { |
245 | 254 | return this.refs.add(arg, type) |
246 | 255 | } |
247 | 256 | }) |
248 | 257 | if (funcRef.location === 'export') { |
249 | | - this.instance.exports[funcRef.indentifier](...args) |
| 258 | + this.instance.exports[funcRef.identifier](...args) |
250 | 259 | } else { |
251 | | - this.instance.exports.table.get(funcRef.indentifier)(...args) |
| 260 | + this.instance.exports.table.get(funcRef.identifier)(...args) |
252 | 261 | } |
253 | 262 | await this.onDone() |
254 | 263 | |
255 | 264 | let numOfGlobals = this.json.globals.length |
264 | 273 | this.actor.state.set(Buffer.from([1]), storage) |
265 | 274 | } |
266 | 275 | |
267 | 276 | this.refs.clear() |
| 277 | + } catch (e) { |
| 278 | + console.log(e) |
| 279 | + } |
268 | 280 | } |
269 | 281 | |
270 | 282 | |
271 | 283 | * returns a promise that resolves when the wasm instance is done running |
313 | 325 | wasm = Buffer.from(wasm, 'hex') |
314 | 326 | json = JSON.parse(json) |
315 | 327 | this.mod = WebAssembly.Module(wasm) |
316 | 328 | this.json = json |
317 | | - this.moduleObj = new ModuleRef(json, this.actor.id) |
| 329 | + this.modSelf = ModuleRef.fromMetaJSON(json, this.actor.id) |
318 | 330 | } |
319 | 331 | |
320 | 332 | getMemory (offset, length) { |
321 | 333 | return new Uint8Array(this.instance.exports.memory.buffer, offset, length) |