wasmContainer.jsView |
---|
1 | 1 | const {wasm2json, json2wasm} = require('wasm-json-toolkit') |
| 2 | +const Message = require('./message.js') |
2 | 3 | const wasmMetering = require('wasm-metering') |
3 | 4 | const customTypes = require('./customTypes.js') |
4 | 5 | const typeCheckWrapper = require('./typeCheckWrapper.js') |
5 | 6 | const ReferanceMap = require('reference-map') |
59 | 60 | static deserialize (serialized) {} |
60 | 61 | } |
61 | 62 | |
62 | 63 | class FunctionRef { |
| 64 | + static get type () { |
| 65 | + return 'funcRef' |
| 66 | + } |
| 67 | + |
63 | 68 | constructor (name, json, id) { |
64 | 69 | this.name = name |
65 | 70 | this.destId = id |
66 | 71 | this.args = [] |
73 | 78 | const instance = WebAssembly.Instance(this.mod, { |
74 | 79 | 'env': { |
75 | 80 | 'checkTypes': function () { |
76 | 81 | const args = [...arguments] |
| 82 | + const checkedArgs = [] |
77 | 83 | while (args.length) { |
78 | 84 | const type = LANGUAGE_TYPES[args.shift()] |
79 | 85 | let arg = args.shift() |
80 | 86 | if (!nativeTypes.has(type)) { |
81 | 87 | arg = self._container.refs.get(arg, type) |
82 | 88 | } |
83 | | - self.args.push({ |
84 | | - arg, |
85 | | - type |
86 | | - }) |
| 89 | + checkedArgs.push(arg) |
87 | 90 | } |
88 | | - self._container.sendMessage(instance) |
| 91 | + const message = new Message({ |
| 92 | + funcRef: self, |
| 93 | + funcArguments: checkedArgs |
| 94 | + }) |
| 95 | + self._container.actor.send(message) |
89 | 96 | } |
90 | 97 | } |
91 | 98 | }) |
92 | 99 | this.wrapper = instance |
134 | 141 | return { |
135 | 142 | func: { |
136 | 143 | externalize: () => {}, |
137 | 144 | internalize: (ref, index) => { |
138 | | - const {type, arg} = self.refs.get(ref) |
139 | | - if (type !== 'funcRef') { |
140 | | - throw new Error('invalid type') |
141 | | - } |
142 | | - arg.container = self |
143 | | - this.instance.exports.table.set(index, arg.wrapper.exports.check) |
| 145 | + const funcRef = self.refs.get(ref, 'funcRef') |
| 146 | + funcRef.container = self |
| 147 | + this.instance.exports.table.set(index, funcRef.wrapper.exports.check) |
144 | 148 | }, |
145 | 149 | catch: (ref, catchRef) => { |
146 | 150 | const {funcRef} = self.refs.get(ref, FunctionRef) |
147 | 151 | const {funcRef: catchFunc} = self.refs.get(ref, FunctionRef) |
219 | 223 | const funcRef = message.funcRef |
220 | 224 | const intef = this.getInteface(funcRef) |
221 | 225 | this.instance = WebAssembly.Instance(this.mod, intef) |
222 | 226 | if (this.instance.exports.table) { |
223 | | - this._orginalTable = this.instance.exports.table.slice() |
| 227 | + this._orginalTable = this.instance.exports.table |
224 | 228 | } |
225 | 229 | const args = message.funcArguments.map(arg => { |
226 | 230 | if (typeof arg === 'number') { |
227 | 231 | return arg |
228 | 232 | } else { |
229 | | - return this.refs.add(arg) |
| 233 | + return this.refs.add(arg, arg.constructor.type) |
230 | 234 | } |
231 | 235 | }) |
232 | 236 | this.instance.exports[funcRef.name](...args) |
233 | 237 | await this.onDone() |
234 | | - this.referanceMap.clear() |
| 238 | + this.refs.clear() |
235 | 239 | } |
236 | 240 | |
237 | 241 | |
238 | 242 | * returns a promise that resolves when the wasm instance is done running |