Commit 7c6e94b073aaac86243093e7d533ecd1cbdb587d
seprated out id
Signed-off-by: wanderer <mjbecze@gmail.com>wanderer committed on 2/28/2018, 8:30:54 PM
Parent: 918662450986924c9a68f772d475dc794e4d7127
Files changed
actor.js | changed |
index.js | changed |
message.js | changed |
package-lock.json | changed |
package.json | changed |
scheduler.js | changed |
tests/index.js | changed |
tests/wasmContainer.js | changed |
wasmContainer.js | changed |
actor.js | ||
---|---|---|
@@ -1,7 +1,4 @@ | ||
1 | -const Pipe = require('buffer-pipe') | |
2 | -const leb128 = require('leb128').unsigned | |
3 | - | |
4 | 1 | module.exports = class Actor { |
5 | 2 | /** |
6 | 3 | * the Actor manages the varous message passing functions and provides |
7 | 4 | * an interface for the containers to use |
@@ -105,18 +102,12 @@ | ||
105 | 102 | this.hypervisor.scheduler.queue([message]) |
106 | 103 | } |
107 | 104 | |
108 | 105 | static serializeMetaData (type, nonce = 0) { |
109 | - const p = new Pipe() | |
110 | - leb128.write(type, p) | |
111 | - leb128.write(nonce, p) | |
112 | - return p.buffer | |
106 | + return [type, nonce] | |
113 | 107 | } |
114 | 108 | |
115 | - static deserializeMetaData (buffer) { | |
116 | - const pipe = new Pipe(buffer) | |
117 | - const type = leb128.read(pipe) | |
118 | - const nonce = leb128.read(pipe) | |
109 | + static deserializeMetaData ([type, nonce]) { | |
119 | 110 | return { |
120 | 111 | nonce, |
121 | 112 | type |
122 | 113 | } |
index.js | ||
---|---|---|
@@ -1,6 +1,7 @@ | ||
1 | 1 | const Actor = require('./actor.js') |
2 | 2 | const Scheduler = require('./scheduler.js') |
3 | +const {ID} = require('./systemObjects.js') | |
3 | 4 | |
4 | 5 | module.exports = class Hypervisor { |
5 | 6 | /** |
6 | 7 | * The Hypervisor manages the container instances by instantiating them and |
@@ -27,9 +28,9 @@ | ||
27 | 28 | this.scheduler.queue(messages) |
28 | 29 | } |
29 | 30 | |
30 | 31 | async loadActor (id) { |
31 | - const state = await this.tree.getSubTree(id) | |
32 | + const state = await this.tree.getSubTree(id.id) | |
32 | 33 | const code = state.get(Buffer.from([0])) |
33 | 34 | const {type, nonce} = Actor.deserializeMetaData(state.root['/'][3]) |
34 | 35 | const Container = this._containerTypes[type] |
35 | 36 | |
@@ -57,16 +58,17 @@ | ||
57 | 58 | */ |
58 | 59 | async createActor (type, code, id = {nonce: this.nonce++, parent: null}) { |
59 | 60 | const Container = this._containerTypes[type] |
60 | 61 | const encoded = encodedID(id) |
61 | - const idHash = await this._getHashFromObj(encoded) | |
62 | - const module = await Container.onCreation(code, idHash, this.tree.dag._dag) | |
62 | + let idHash = await this._getHashFromObj(encoded) | |
63 | + idHash = new ID(idHash) | |
64 | + const module = await Container.onCreation(code, idHash, this.tree) | |
63 | 65 | const metaData = Actor.serializeMetaData(type) |
64 | 66 | |
65 | 67 | // save the container in the state |
66 | - this.tree.set(idHash, metaData) | |
68 | + this.tree.set(idHash.id, metaData) | |
67 | 69 | if (code) { |
68 | - this.tree.set(Buffer.concat([idHash, Buffer.from([0])]), code) | |
70 | + this.tree.set(Buffer.concat([idHash.id, Buffer.from([0])]), code) | |
69 | 71 | } |
70 | 72 | return { |
71 | 73 | id: idHash, |
72 | 74 | module: module |
@@ -105,9 +107,9 @@ | ||
105 | 107 | |
106 | 108 | function encodedID (id) { |
107 | 109 | const nonce = Buffer.from([id.nonce]) |
108 | 110 | if (id.parent) { |
109 | - return Buffer.concat([nonce, id.parent]) | |
111 | + return Buffer.concat([nonce, id.parent.id]) | |
110 | 112 | } else { |
111 | 113 | return nonce |
112 | 114 | } |
113 | 115 | } |
message.js | ||
---|---|---|
@@ -1,5 +1,6 @@ | ||
1 | 1 | const EventEmitter = require('events') |
2 | +const {ID} = require('./systemObjects.js') | |
2 | 3 | |
3 | 4 | /** |
4 | 5 | * This implements Messages for Primea |
5 | 6 | * @module primea-message |
@@ -31,9 +32,9 @@ | ||
31 | 32 | ticks: 0, |
32 | 33 | funcRef: null, |
33 | 34 | funcArguments: [], |
34 | 35 | funcParameters: [], |
35 | - _fromId: Buffer.alloc(20), | |
36 | + _fromId: new ID(Buffer.alloc(20)), | |
36 | 37 | _fromTicks: 0 |
37 | 38 | } |
38 | 39 | } |
39 | 40 | } |
package-lock.json | ||
---|---|---|
The diff is too large to show. Use a local git client to view these changes. Old file size: 336834 bytes New file size: 338011 bytes |
package.json | ||
---|---|---|
@@ -28,13 +28,11 @@ | ||
28 | 28 | "author": "mjbecze <mjbecze@gmail.com>", |
29 | 29 | "contributors": "Alex Beregszaszi <alex@rtfs.hu>", |
30 | 30 | "license": "MPL-2.0", |
31 | 31 | "dependencies": { |
32 | - "binary-search": "^1.3.3", | |
33 | 32 | "binary-search-insert": "^1.0.3", |
34 | - "buffer-pipe": "0.0.2", | |
33 | + "borc": "^2.0.2", | |
35 | 34 | "events": "^2.0.0", |
36 | - "leb128": "0.0.4", | |
37 | 35 | "levelup": "^2.0.2", |
38 | 36 | "reference-map": "^1.2.1", |
39 | 37 | "safe-buffer": "^5.1.1", |
40 | 38 | "wasm-json-toolkit": "^0.2.2", |
scheduler.js | ||
---|---|---|
@@ -6,9 +6,9 @@ | ||
6 | 6 | // order by number of ticks if messages have different number of ticks |
7 | 7 | if (messageA._fromTicks !== messageB._fromTicks) { |
8 | 8 | return messageA._fromTicks > messageB._fromTicks |
9 | 9 | } else { |
10 | - return Buffer.compare(messageA._fromId, messageB._fromId) | |
10 | + return Buffer.compare(messageA._fromId.id, messageB._fromId.id) | |
11 | 11 | } |
12 | 12 | } |
13 | 13 | |
14 | 14 | module.exports = class Scheduler extends EventEmitter { |
@@ -46,9 +46,9 @@ | ||
46 | 46 | this.emit('idle') |
47 | 47 | } |
48 | 48 | |
49 | 49 | async _processMessage (message) { |
50 | - const to = message.funcRef.destId.toString('hex') | |
50 | + const to = message.funcRef.destId.id.toString('hex') | |
51 | 51 | let actor = this.actors.get(to) |
52 | 52 | if (!actor) { |
53 | 53 | actor = await this.hypervisor.loadActor(message.funcRef.destId) |
54 | 54 | this.actors.set(to, actor) |
tests/index.js | ||
---|---|---|
@@ -31,9 +31,9 @@ | ||
31 | 31 | |
32 | 32 | tape('basic', async t => { |
33 | 33 | t.plan(2) |
34 | 34 | const expectedState = { |
35 | - '/': Buffer.from('926de6b7eb39cfa8d7f8a44d1ef191d3bcb765a7', 'hex') | |
35 | + '/': Buffer.from('a5e1aaebec14b7f144d6a7e007b148aa7c56804c', 'hex') | |
36 | 36 | } |
37 | 37 | |
38 | 38 | const tree = new RadixTree({ |
39 | 39 | db: db |
@@ -62,9 +62,9 @@ | ||
62 | 62 | |
63 | 63 | tape('two communicating actors', async t => { |
64 | 64 | t.plan(2) |
65 | 65 | const expectedState = { |
66 | - '/': Buffer.from('a4c7ceacd8c867ae1d0b472d8bffa3cb10048331', 'hex') | |
66 | + '/': Buffer.from('bc0af44b691ef57b362df8b11c274742147900cd', 'hex') | |
67 | 67 | } |
68 | 68 | |
69 | 69 | const tree = new RadixTree({ |
70 | 70 | db: db |
@@ -110,9 +110,9 @@ | ||
110 | 110 | |
111 | 111 | tape('three communicating actors', async t => { |
112 | 112 | t.plan(3) |
113 | 113 | const expectedState = { |
114 | - '/': Buffer.from('4633ac4b9f8212e501b6c56906039ec081fbe5a3', 'hex') | |
114 | + '/': Buffer.from('86e066f60fe2f722befbc29f128948f6487a207a', 'hex') | |
115 | 115 | } |
116 | 116 | |
117 | 117 | const tree = new RadixTree({ |
118 | 118 | db: db |
@@ -165,9 +165,9 @@ | ||
165 | 165 | |
166 | 166 | tape('three communicating actors, with tick counting', async t => { |
167 | 167 | t.plan(3) |
168 | 168 | const expectedState = { |
169 | - '/': Buffer.from('4633ac4b9f8212e501b6c56906039ec081fbe5a3', 'hex') | |
169 | + '/': Buffer.from('86e066f60fe2f722befbc29f128948f6487a207a', 'hex') | |
170 | 170 | } |
171 | 171 | |
172 | 172 | const tree = new RadixTree({ |
173 | 173 | db: db |
@@ -220,9 +220,9 @@ | ||
220 | 220 | |
221 | 221 | tape('errors', async t => { |
222 | 222 | t.plan(3) |
223 | 223 | const expectedState = { |
224 | - '/': Buffer.from('a4c7ceacd8c867ae1d0b472d8bffa3cb10048331', 'hex') | |
224 | + '/': Buffer.from('bc0af44b691ef57b362df8b11c274742147900cd', 'hex') | |
225 | 225 | } |
226 | 226 | |
227 | 227 | const tree = new RadixTree({ |
228 | 228 | db: db |
@@ -268,9 +268,9 @@ | ||
268 | 268 | |
269 | 269 | tape('actor creation', async t => { |
270 | 270 | t.plan(2) |
271 | 271 | const expectedState = { |
272 | - '/': Buffer.from('f47377a763c91247e62138408d706a09bccaaf36', 'hex') | |
272 | + '/': Buffer.from('544ae05bc8831cc69c5524bc3a51c0d4806e9d60', 'hex') | |
273 | 273 | } |
274 | 274 | |
275 | 275 | const tree = new RadixTree({ |
276 | 276 | db: db |
@@ -308,14 +308,15 @@ | ||
308 | 308 | await hypervisor.send(new Message({funcRef: module.start})) |
309 | 309 | |
310 | 310 | const stateRoot = await hypervisor.createStateRoot() |
311 | 311 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
312 | + t.end() | |
312 | 313 | }) |
313 | 314 | |
314 | 315 | tape('simple message arbiter test', async t => { |
315 | 316 | t.plan(4) |
316 | 317 | const expectedState = { |
317 | - '/': Buffer.from('a4c7ceacd8c867ae1d0b472d8bffa3cb10048331', 'hex') | |
318 | + '/': Buffer.from('bc0af44b691ef57b362df8b11c274742147900cd', 'hex') | |
318 | 319 | } |
319 | 320 | |
320 | 321 | const tree = new RadixTree({ |
321 | 322 | db: db |
@@ -382,9 +383,9 @@ | ||
382 | 383 | tape('arbiter test for id comparision', async t => { |
383 | 384 | t.plan(4) |
384 | 385 | let message |
385 | 386 | const expectedState = { |
386 | - '/': Buffer.from('4633ac4b9f8212e501b6c56906039ec081fbe5a3', 'hex') | |
387 | + '/': Buffer.from('86e066f60fe2f722befbc29f128948f6487a207a', 'hex') | |
387 | 388 | } |
388 | 389 | |
389 | 390 | const tree = new RadixTree({ |
390 | 391 | db: db |
@@ -449,9 +450,9 @@ | ||
449 | 450 | |
450 | 451 | tape('async work', async t => { |
451 | 452 | t.plan(3) |
452 | 453 | const expectedState = { |
453 | - '/': Buffer.from('a4c7ceacd8c867ae1d0b472d8bffa3cb10048331', 'hex') | |
454 | + '/': Buffer.from('bc0af44b691ef57b362df8b11c274742147900cd', 'hex') | |
454 | 455 | } |
455 | 456 | |
456 | 457 | const tree = new RadixTree({ |
457 | 458 | db: db |
tests/wasmContainer.js | ||
---|---|---|
@@ -157,9 +157,9 @@ | ||
157 | 157 | }) |
158 | 158 | hypervisor.send(message) |
159 | 159 | }) |
160 | 160 | |
161 | -tape.skip('store globals', async t => { | |
161 | +tape('load / store globals', async t => { | |
162 | 162 | t.plan(1) |
163 | 163 | tester = t |
164 | 164 | const tree = new RadixTree({ |
165 | 165 | db |
@@ -176,11 +176,8 @@ | ||
176 | 176 | const message = new Message({ |
177 | 177 | funcRef: module.getFuncRef('store') |
178 | 178 | }).on('done', actor => { |
179 | 179 | resolve() |
180 | - // const a = actor.container.getMemory(0, 8) | |
181 | - // const b = actor.container.getMemory(8, 8) | |
182 | - // t.deepEquals(a, b, 'should copy memory correctly') | |
183 | 180 | }) |
184 | 181 | hypervisor.send(message) |
185 | 182 | }) |
186 | 183 | |
@@ -188,11 +185,11 @@ | ||
188 | 185 | const message = new Message({ |
189 | 186 | funcRef: module.getFuncRef('load') |
190 | 187 | }).on('done', actor => { |
191 | 188 | resolve() |
192 | - // const a = actor.container.getMemory(0, 8) | |
193 | - // const b = actor.container.getMemory(8, 8) | |
194 | - // t.deepEquals(a, b, 'should copy memory correctly') | |
189 | + const b = actor.container.getMemory(5, 4) | |
190 | + const result = Buffer.from(b).toString() | |
191 | + t.deepEquals(result, 'test', 'should copy memory correctly') | |
195 | 192 | }) |
196 | 193 | hypervisor.send(message) |
197 | 194 | }) |
198 | 195 | }) |
wasmContainer.js | ||
---|---|---|
@@ -4,9 +4,27 @@ | ||
4 | 4 | const Message = require('./message.js') |
5 | 5 | const customTypes = require('./customTypes.js') |
6 | 6 | const injectGlobals = require('./injectGlobals.js') |
7 | 7 | const typeCheckWrapper = require('./typeCheckWrapper.js') |
8 | +const {FunctionRef} = require('./systemObjects.js') | |
9 | +const cbor = require('borc') | |
8 | 10 | |
11 | +const TAGS = { | |
12 | + link: 42, | |
13 | + id: 43, | |
14 | + func: 43, | |
15 | + mod: 44 | |
16 | +} | |
17 | + | |
18 | +const DEFAULTS = { | |
19 | + elem: [], | |
20 | + buf: Buffer.from([]), | |
21 | + id: new cbor.Tagged(TAGS.id, 0), | |
22 | + mod: new cbor.Tagged(TAGS.mod, [{}, new cbor.Tagged(TAGS.id, 0)]), | |
23 | + link: {'/': null}, | |
24 | + func: new cbor.Tagged(TAGS.func, 0) | |
25 | +} | |
26 | + | |
9 | 27 | const nativeTypes = new Set(['i32', 'i64', 'f32', 'f64']) |
10 | 28 | const LANGUAGE_TYPES = { |
11 | 29 | 'actor': 0x0, |
12 | 30 | 'buf': 0x1, |
@@ -34,56 +52,36 @@ | ||
34 | 52 | 0x60: 'func', |
35 | 53 | 0x40: 'block_type' |
36 | 54 | } |
37 | 55 | |
38 | -class FunctionRef { | |
39 | - constructor (location, identifier, params, id) { | |
40 | - this.location = location | |
41 | - this.identifier = identifier | |
42 | - this.destId = id | |
43 | - this.params = params | |
44 | - | |
45 | - } | |
46 | - | |
47 | - generateWrapper (container) { | |
48 | - let wrapper = typeCheckWrapper(this.params) | |
49 | - const wasm = json2wasm(wrapper) | |
50 | - const mod = WebAssembly.Module(wasm) | |
51 | - const self = this | |
52 | - wrapper = WebAssembly.Instance(mod, { | |
53 | - 'env': { | |
54 | - 'checkTypes': function () { | |
55 | - const args = [...arguments] | |
56 | - const checkedArgs = [] | |
57 | - while (args.length) { | |
58 | - const type = LANGUAGE_TYPES[args.shift()] | |
59 | - let arg = args.shift() | |
60 | - if (!nativeTypes.has(type)) { | |
61 | - arg = container.refs.get(arg, type) | |
62 | - } | |
63 | - checkedArgs.push(arg) | |
56 | +function generateWrapper (funcRef, container) { | |
57 | + let wrapper = typeCheckWrapper(funcRef.params) | |
58 | + const wasm = json2wasm(wrapper) | |
59 | + const mod = WebAssembly.Module(wasm) | |
60 | + const self = funcRef | |
61 | + wrapper = WebAssembly.Instance(mod, { | |
62 | + 'env': { | |
63 | + 'checkTypes': function () { | |
64 | + const args = [...arguments] | |
65 | + const checkedArgs = [] | |
66 | + while (args.length) { | |
67 | + const type = LANGUAGE_TYPES[args.shift()] | |
68 | + let arg = args.shift() | |
69 | + if (!nativeTypes.has(type)) { | |
70 | + arg = container.refs.get(arg, type) | |
64 | 71 | } |
65 | - const message = new Message({ | |
66 | - funcRef: self, | |
67 | - funcArguments: checkedArgs | |
68 | - }) | |
69 | - container.actor.send(message) | |
72 | + checkedArgs.push(arg) | |
70 | 73 | } |
74 | + const message = new Message({ | |
75 | + funcRef: self, | |
76 | + funcArguments: checkedArgs | |
77 | + }) | |
78 | + container.actor.send(message) | |
71 | 79 | } |
72 | - }) | |
73 | - wrapper.exports.check.object = this | |
74 | - return wrapper | |
75 | - } | |
76 | - | |
77 | - encodeCBOR (gen) { | |
78 | - return gen.write({ | |
79 | - '>': {} | |
80 | - }) | |
81 | - } | |
82 | - | |
83 | - set container (container) { | |
84 | - this._container = container | |
85 | - } | |
80 | + } | |
81 | + }) | |
82 | + wrapper.exports.check.object = funcRef | |
83 | + return wrapper | |
86 | 84 | } |
87 | 85 | |
88 | 86 | class ModuleRef { |
89 | 87 | constructor (ex, id) { |
@@ -91,20 +89,13 @@ | ||
91 | 89 | this.id = id |
92 | 90 | } |
93 | 91 | |
94 | 92 | getFuncRef (name) { |
95 | - return new FunctionRef('export', name, this.exports[name], this.id) | |
93 | + return new FunctionRef(false, name, this.exports[name], this.id) | |
96 | 94 | } |
97 | 95 | |
98 | 96 | encodeCBOR (gen) { |
99 | - return gen.write({ | |
100 | - '#': { | |
101 | - exports: this.exports, | |
102 | - id: { | |
103 | - '@': this.id | |
104 | - } | |
105 | - } | |
106 | - }) | |
97 | + return gen.write(new cbor.Tagged(TAGS.mod, [this.exports, new cbor.Tagged(TAGS.id, this.id)])) | |
107 | 98 | } |
108 | 99 | |
109 | 100 | static fromMetaJSON (json, id) { |
110 | 101 | const exports = {} |
@@ -123,27 +114,38 @@ | ||
123 | 114 | this.actor = actor |
124 | 115 | this.refs = new ReferanceMap() |
125 | 116 | } |
126 | 117 | |
127 | - static async onCreation (wasm, id, cachedb) { | |
118 | + static async onCreation (wasm, id, tree) { | |
119 | + const cachedb = tree.dag._dag | |
128 | 120 | if (!WebAssembly.validate(wasm)) { |
129 | 121 | throw new Error('invalid wasm binary') |
130 | 122 | } |
131 | 123 | let moduleJSON = wasm2json(wasm) |
132 | 124 | const json = customTypes.mergeTypeSections(moduleJSON) |
133 | 125 | moduleJSON = wasmMetering.meterJSON(moduleJSON, { |
134 | 126 | meterType: 'i32' |
135 | 127 | }) |
136 | - if (json.globals.length) { | |
128 | + | |
129 | + // initialize the globals | |
130 | + let numOfGlobals = json.globals.length | |
131 | + if (numOfGlobals) { | |
137 | 132 | moduleJSON = injectGlobals(moduleJSON, json.globals) |
133 | + const storage = [] | |
134 | + while (numOfGlobals--) { | |
135 | + const type = json.globals[numOfGlobals].type | |
136 | + storage.push(DEFAULTS[type]) | |
137 | + } | |
138 | + tree.set(Buffer.concat([id.id, Buffer.from([1])]), storage) | |
138 | 139 | } |
140 | + // recompile the wasm | |
139 | 141 | wasm = json2wasm(moduleJSON) |
140 | 142 | await Promise.all([ |
141 | 143 | new Promise((resolve, reject) => { |
142 | - cachedb.put(id.toString() + 'meta', JSON.stringify(json), resolve) | |
144 | + cachedb.put(id.id.toString() + 'meta', JSON.stringify(json), resolve) | |
143 | 145 | }), |
144 | 146 | new Promise((resolve, reject) => { |
145 | - cachedb.put(id.toString() + 'code', wasm.toString('hex'), resolve) | |
147 | + cachedb.put(id.id.toString() + 'code', wasm.toString('hex'), resolve) | |
146 | 148 | }) |
147 | 149 | ]) |
148 | 150 | return ModuleRef.fromMetaJSON(json, id) |
149 | 151 | } |
@@ -157,16 +159,20 @@ | ||
157 | 159 | const object = func.object |
158 | 160 | if (object) { |
159 | 161 | return self.refs.add(object) |
160 | 162 | } else { |
161 | - const ref = new FunctionRef('table', object.tableIndex, self.json, self.actor.id) | |
163 | + const ref = new FunctionRef(true, object.tableIndex, self.json, self.actor.id) | |
162 | 164 | return self.refs.add(ref) |
163 | 165 | } |
164 | 166 | }, |
165 | 167 | internalize: (ref, index) => { |
166 | 168 | const funcRef = self.refs.get(ref, 'func') |
167 | - const wrapper = funcRef.generateWrapper(self) | |
168 | - this.instance.exports.table.set(index, wrapper.exports.check) | |
169 | + try { | |
170 | + const wrapper = generateWrapper(funcRef, self) | |
171 | + this.instance.exports.table.set(index, wrapper.exports.check) | |
172 | + } catch (e) { | |
173 | + console.log(e) | |
174 | + } | |
169 | 175 | }, |
170 | 176 | catch: (ref, catchRef) => { |
171 | 177 | const {funcRef} = self.refs.get(ref, FunctionRef) |
172 | 178 | const {funcRef: catchFunc} = self.refs.get(ref, FunctionRef) |
@@ -242,12 +248,12 @@ | ||
242 | 248 | } |
243 | 249 | } |
244 | 250 | |
245 | 251 | async onMessage (message) { |
246 | - try { | |
247 | 252 | const funcRef = message.funcRef |
248 | 253 | const intef = this.getInterface(funcRef) |
249 | 254 | this.instance = WebAssembly.Instance(this.mod, intef) |
255 | + // map table indexes | |
250 | 256 | const table = this.instance.exports.table |
251 | 257 | if (table) { |
252 | 258 | let length = table.length |
253 | 259 | while (length--) { |
@@ -256,39 +262,51 @@ | ||
256 | 262 | func.tableIndex = length |
257 | 263 | } |
258 | 264 | } |
259 | 265 | } |
266 | + // import references | |
260 | 267 | const args = message.funcArguments.map((arg, index) => { |
261 | 268 | const type = funcRef.params[index] |
262 | 269 | if (nativeTypes.has(type)) { |
263 | 270 | return arg |
264 | 271 | } else { |
265 | 272 | return this.refs.add(arg, type) |
266 | 273 | } |
267 | 274 | }) |
268 | - if (funcRef.location === 'export') { | |
275 | + | |
276 | + // setup globals | |
277 | + let numOfGlobals = this.json.globals.length | |
278 | + if (numOfGlobals) { | |
279 | + const refs = [] | |
280 | + while (numOfGlobals--) { | |
281 | + const obj = this.storage[numOfGlobals] | |
282 | + refs.push(this.refs.add(obj, this.json.globals[numOfGlobals].type)) | |
283 | + } | |
284 | + this.instance.exports.setter_globals(...refs) | |
285 | + } | |
286 | + | |
287 | + // call entrypoint function | |
288 | + if (funcRef.private) { | |
289 | + this.instance.exports.table.get(funcRef.identifier)(...args) | |
290 | + } else { | |
269 | 291 | this.instance.exports[funcRef.identifier](...args) |
270 | - } else { | |
271 | - this.instance.exports.table.get(funcRef.identifier)(...args) | |
272 | 292 | } |
273 | 293 | await this.onDone() |
274 | 294 | |
275 | - let numOfGlobals = this.json.globals.length | |
295 | + numOfGlobals = this.json.globals.length | |
276 | 296 | if (numOfGlobals) { |
277 | - const storage = [] | |
297 | + this.storage = [] | |
278 | 298 | this.instance.exports.getter_globals() |
279 | 299 | const mem = new Uint32Array(this.instance.exports.memory.buffer, 0, numOfGlobals) |
280 | 300 | while (numOfGlobals--) { |
281 | 301 | const ref = mem[numOfGlobals] |
282 | - storage.push(this.refs.get(ref, this.json.globals[numOfGlobals].type)) | |
302 | + this.storage.push(this.refs.get(ref, this.json.globals[numOfGlobals].type)) | |
283 | 303 | } |
284 | - this.actor.state.set(Buffer.from([1]), storage) | |
304 | + | |
305 | + this.actor.state.set(Buffer.from([1]), this.storage) | |
285 | 306 | } |
286 | 307 | |
287 | 308 | this.refs.clear() |
288 | - } catch (e) { | |
289 | - console.log(e) | |
290 | - } | |
291 | 309 | } |
292 | 310 | |
293 | 311 | /** |
294 | 312 | * returns a promise that resolves when the wasm instance is done running |
@@ -312,35 +330,41 @@ | ||
312 | 330 | return this._opsQueue |
313 | 331 | } |
314 | 332 | |
315 | 333 | async onStartup () { |
316 | - let [json, wasm] = await Promise.all([ | |
334 | + let [json, wasm, storage] = await Promise.all([ | |
317 | 335 | new Promise((resolve, reject) => { |
318 | - this.actor.cachedb.get(this.actor.id.toString() + 'meta', (err, json) => { | |
336 | + this.actor.cachedb.get(this.actor.id.id.toString() + 'meta', (err, json) => { | |
319 | 337 | if (err) { |
320 | 338 | reject(err) |
321 | 339 | } else { |
322 | 340 | resolve(json) |
323 | 341 | } |
324 | 342 | }) |
325 | 343 | }), |
326 | 344 | new Promise((resolve, reject) => { |
327 | - this.actor.cachedb.get(this.actor.id.toString() + 'code', (err, wasm) => { | |
345 | + this.actor.cachedb.get(this.actor.id.id.toString() + 'code', (err, wasm) => { | |
328 | 346 | if (err) { |
329 | 347 | reject(err) |
330 | 348 | } else { |
331 | 349 | resolve(wasm) |
332 | 350 | } |
333 | 351 | }) |
334 | - }) | |
352 | + }), | |
353 | + this.actor.state.get(Buffer.from([1])) | |
335 | 354 | ]) |
355 | + this.storage = storage | |
336 | 356 | wasm = Buffer.from(wasm, 'hex') |
337 | 357 | json = JSON.parse(json) |
338 | 358 | this.mod = WebAssembly.Module(wasm) |
339 | 359 | this.json = json |
340 | 360 | this.modSelf = ModuleRef.fromMetaJSON(json, this.actor.id) |
341 | 361 | } |
342 | 362 | |
363 | + onShutdown () { | |
364 | + | |
365 | + } | |
366 | + | |
343 | 367 | getMemory (offset, length) { |
344 | 368 | return new Uint8Array(this.instance.exports.memory.buffer, offset, length) |
345 | 369 | } |
346 | 370 |
Built with git-ssb-web