git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit ad560c8f8c3258d82e01e7dcf6778f3b3d1e8265

updated dataBuf eleBufs

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 2/7/2018, 11:20:19 PM
Parent: e1f7b0f1b9dc6898db308a95307211a4ea191e3c

Files changed

package-lock.jsonchanged
package.jsonchanged
tests/wasmContainer.jschanged
wasmContainer.jschanged
package-lock.jsonView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 343966 bytes
New file size: 344090 bytes
package.jsonView
@@ -28,15 +28,15 @@
2828 "author": "mjbecze <mjbecze@gmail.com>",
2929 "contributors": "Alex Beregszaszi <alex@rtfs.hu>",
3030 "license": "MPL-2.0",
3131 "dependencies": {
32- "binary-search": "^1.3.2",
32+ "binary-search": "^1.3.3",
3333 "binary-search-insert": "^1.0.3",
3434 "buffer-pipe": "0.0.2",
35- "events": "^1.1.1",
35+ "events": "^2.0.0",
3636 "leb128": "0.0.4",
3737 "levelup": "^2.0.1",
38- "reference-map": "^1.1.0",
38+ "reference-map": "^1.2.0",
3939 "safe-buffer": "^5.1.1",
4040 "wasm-json-toolkit": "^0.2.0",
4141 "wasm-metering": "^0.1.1"
4242 },
tests/wasmContainer.jsView
@@ -10,8 +10,12 @@
1010
1111 let tester
1212
1313 class TestWasmContainer extends WasmContainer {
14+ constructor (actor) {
15+ super(actor)
16+ this._storage = new Map()
17+ }
1418 getInteface (funcRef) {
1519 const orginal = super.getInteface(funcRef)
1620 return Object.assign(orginal, {
1721 test: {
@@ -20,8 +24,16 @@
2024 }
2125 }
2226 })
2327 }
28+ setState (key, ref) {
29+ const obj = this.refs.get(ref)
30+ this._storage.set(key, obj)
31+ }
32+ getState (key) {
33+ const obj = this._storage.get(key)
34+ return this.refs.add(obj)
35+ }
2436 }
2537
2638 tape('basic', async t => {
2739 t.plan(2)
wasmContainer.jsView
@@ -2,13 +2,15 @@
22 const wasmMetering = require('wasm-metering')
33 const customTypes = require('./customTypes.js')
44 const typeCheckWrapper = require('./typeCheckWrapper.js')
55 const ReferanceMap = require('reference-map')
6+const leb128 = require('leb128')
67
78 const nativeTypes = new Set(['i32', 'i64', 'f32', 'f64'])
89 const LANGUAGE_TYPES = {
910 'actor': 0x0,
1011 'buf': 0x1,
12+ 'elem': 0x2,
1113 'i32': 0x7f,
1214 'i64': 0x7e,
1315 'f32': 0x7d,
1416 'f64': 0x7c,
@@ -26,8 +28,38 @@
2628 0x60: 'func',
2729 0x40: 'block_type'
2830 }
2931
32+class ElementBuffer {
33+ constructor (size) {
34+ this._array = new Array(size)
35+ }
36+
37+ serialize () {
38+ const serialized = this._array.map(ref => ref.serailize())
39+ return Buffer.concat(Buffer.from([LANGUAGE_TYPES['elem']]), leb128.encode(serialized.length), serialized)
40+ }
41+
42+ static deserialize (serialized) {}
43+}
44+
45+class DataBuffer {
46+ constructor (memory, offset, length) {
47+ this._data = new Uint8Array(this.instance.exports.memory.buffer, offset, length)
48+ }
49+ serialize () {
50+ return Buffer.concat(Buffer.from([LANGUAGE_TYPES['elem']]), leb128.encode(this._data.length), this._data)
51+ }
52+ static deserialize (serialized) {}
53+}
54+
55+class LinkRef {
56+ serialize () {
57+ return Buffer.concat(Buffer.from([LANGUAGE_TYPES['link'], this]))
58+ }
59+ static deserialize (serialized) {}
60+}
61+
3062 class FunctionRef {
3163 constructor (name, json, id) {
3264 this.name = name
3365 this.destId = id
@@ -45,12 +77,9 @@
4577 while (args.length) {
4678 const type = LANGUAGE_TYPES[args.shift()]
4779 let arg = args.shift()
4880 if (!nativeTypes.has(type)) {
49- arg = self._container.refs.get(arg)
50- if (arg.type !== type) {
51- throw new Error('invalid type')
52- }
81+ arg = self._container.refs.get(arg, type)
5382 }
5483 self.args.push({
5584 arg,
5685 type
@@ -72,18 +101,24 @@
72101 this.actor = actor
73102 this.refs = new ReferanceMap()
74103 }
75104
76- static onCreation (wasm, id, cachedb) {
105+ static async onCreation (wasm, id, cachedb) {
77106 WebAssembly.validate(wasm)
78107 let moduleJSON = wasm2json(wasm)
79108 const json = mergeTypeSections(moduleJSON)
80109 moduleJSON = wasmMetering.meterJSON(moduleJSON, {
81110 meterType: 'i32'
82111 })
83112 wasm = json2wasm(moduleJSON)
84- cachedb.put(id.toString() + 'meta', json)
85- cachedb.put(id.toString() + 'code', wasm.toString('hex'))
113+ await Promise.all([
114+ new Promise((resolve, reject) => {
115+ cachedb.put(id.toString() + 'meta', json, resolve)
116+ }),
117+ new Promise((resolve, reject) => {
118+ cachedb.put(id.toString() + 'code', wasm.toString('hex'), resolve)
119+ })
120+ ])
86121 const refs = {}
87122 Object.keys(json.typeMap).forEach(key => {
88123 refs[key] = new FunctionRef(key, json, id)
89124 })
@@ -111,41 +146,62 @@
111146 const {funcRef} = self.refs.get(ref, FunctionRef)
112147 const {funcRef: catchFunc} = self.refs.get(ref, FunctionRef)
113148 funcRef.catch = catchFunc
114149 },
115- getGasAmount: () => {},
116- setGasAmount: () => {}
150+ getGasAmount: (funcRef) => {},
151+ setGasAmount: (funcRef) => {}
117152 },
118- storage: {
119- load: () => {},
120- store: () => {},
121- delete: () => {}
122- },
123153 link: {
124154 wrap: (ref) => {
125155 const obj = this.refs.get(ref)
126- obj.seriarlize()
156+ const link = new LinkRef(obj.serialize())
157+ return this.refs.add(link, 'link')
127158 },
128- unwrap: () => {}
159+ unwrap: async (ref, cb) => {
160+ const obj = this.refs.get(ref, 'link')
161+ const promise = this.actor.tree.dataStore.get(obj)
162+ await this._opsQueue.push(promise)
163+ // todo
164+ }
129165 },
130- databuf: {
131- create: () => {},
132- load8: () => {},
133- load16: () => {},
134- load32: () => {},
135- load64: () => {},
136- store8: () => {},
137- store16: () => {},
138- store32: () => {},
139- store64: () => {},
140- copy: () => {}
166+ module: {
167+ new: code => {},
168+ exports: (mod, name) => {}
141169 },
142- elembuf: {
143- create: () => {},
144- load: () => {},
145- store: () => {},
146- delete: () => {}
170+ memory: {
171+ externalize: (index, length) => {
172+ const buf = this.getMemory(index, length)
173+ return this.refs.add(buf, 'buf')
174+ },
175+ internalize: (dataRef, writeOffset, readOffset, length) => {
176+ let buf = this.refs.get(dataRef, 'buf')
177+ buf = buf.subarray(readOffset, length)
178+ const mem = this.getMemory(writeOffset, buf.length)
179+ mem.set(buf)
180+ }
147181 },
182+ table: {
183+ externalize: (index, length) => {
184+ const mem = this.getMemory(index, length * 4)
185+ const objects = []
186+ while (length--) {
187+ const ref = mem[index + length]
188+ if (this.refs.has(ref)) {
189+ objects.push(ref)
190+ } else {
191+ throw new Error('invalid ref')
192+ }
193+ }
194+ const eleBuf = new ElementBuffer(objects)
195+ return this.refs.add(eleBuf, 'ele')
196+ },
197+ internalize: (dataRef, writeOffset, readOffset, length) => {
198+ let buf = this.refs.get(dataRef, 'ele')
199+ buf = buf.subarray(readOffset, length)
200+ const mem = this.getMemory(writeOffset, buf.length)
201+ mem.set(buf)
202+ }
203+ },
148204 metering: {
149205 usegas: (amount) => {
150206 funcRef.gas -= amount
151207 if (funcRef.gas < 0) {
@@ -155,22 +211,49 @@
155211 }
156212 }
157213 }
158214
159- onMessage (message) {
215+ async onMessage (message) {
160216 const funcRef = message.funcRef
161217 const intef = this.getInteface(funcRef)
162218 this.instance = WebAssembly.Instance(this.mod, intef)
219+ if (this.instance.exports.table) {
220+ this._orginalTable = this.instance.exports.table.slice()
221+ }
163222 const args = message.funcArguments.map(arg => {
164223 if (typeof arg === 'number') {
165224 return arg
166225 } else {
167226 return this.refs.add(arg)
168227 }
169228 })
170229 this.instance.exports[funcRef.name](...args)
230+ await this.onDone()
231+ this.referanceMap.clear()
171232 }
172233
234+ /**
235+ * returns a promise that resolves when the wasm instance is done running
236+ * @returns {Promise}
237+ */
238+ async onDone () {
239+ let prevOps
240+ while (prevOps !== this._opsQueue) {
241+ prevOps = this._opsQueue
242+ await prevOps
243+ }
244+ }
245+
246+ /**
247+ * Pushed an async operation to the a promise queue that
248+ * @returns {Promise} the returned promise resolves in the order the intail
249+ * operation was pushed to the queue
250+ */
251+ pushOpsQueue (promise) {
252+ this._opsQueue = Promise.all([this._opsQueue, promise])
253+ return this._opsQueue
254+ }
255+
173256 getFuncRef (name, send) {
174257 const funcRef = new FunctionRef(this.json, name, send)
175258 return funcRef
176259 }
@@ -200,8 +283,12 @@
200283 this.mod = WebAssembly.Module(wasm)
201284 this.json = json
202285 }
203286
287+ getMemory (offset, length) {
288+ return new DataBuffer(this.instance.exports.memory.buffer, offset, length)
289+ }
290+
204291 static get typeId () {
205292 return 9
206293 }
207294 }

Built with git-ssb-web