git ssb

0+

wanderer🌟 / js-primea-wasm-container



Commit 5f942df4b072166f79f934a8cfe0c6888abcaf21

added referance mapping

wanderer committed on 6/3/2017, 10:02:15 PM
Parent: b9771c43b3fc8be378582ea55e07a202d67a081e

Files changed

index.jschanged
referanceMap.jsadded
index.jsView
@@ -1,69 +1,45 @@
1+const ReferanceMap = require('./referanceMap.js')
2+
13 module.exports = class WasmContainer {
24 /**
35 * The interface API is the api the exposed to interfaces. All queries about
46 * the enviroment and call to the kernel go through this API
57 */
6- constructor (code) {
7- this._module = WebAssembly.Module(code)
8+ constructor (exoInterface, imports) {
9+ this.exoInterface = exoInterface
10+ this.imports = imports
11+ this.referanceMap = new ReferanceMap()
812 }
913
10- static get name () {
11- return 'wasm'
12- }
1314 /**
1415 * Runs the core VM with a given environment and imports
1516 */
16- async run (message, kernel, imports = []) {
17- const responses = {}
17+ async run (message) {
1818 /**
1919 * Builds a import map with an array of given interfaces
2020 */
21- function buildImports (opts, imports) {
22- const importMap = {}
23- for (const Import of imports) {
24- const name = Import.name
25- opts.response = responses[name] = {}
26- const newInterface = new Import(opts)
27- const props = Object.getOwnPropertyNames(Import.prototype)
21+ const importMap = {}
22+ for (const name in this.imports) {
23+ importMap[name] = {}
24+ const Import = this.imports[name]
25+ const newInterface = new Import(this)
26+ const props = Object.getOwnPropertyNames(Import.prototype)
2827
29- // bind the methods to the correct 'this'
30- for (const prop of props) {
31- newInterface[prop] = newInterface[prop].bind(newInterface)
28+ // bind the methods to the correct 'this'
29+ for (const prop of props) {
30+ if (prop !== 'constructor') {
31+ importMap[name][prop] = newInterface[prop].bind(newInterface)
3232 }
33- importMap[name] = newInterface
3433 }
35- return importMap
3634 }
3735
38- let instance
39-
40- const opts = {
41- vm: {
42- /**
43- * adds an aync operation to the operations queue
44- */
45- pushOpsQueue: (promise, callbackIndex, intefaceCallback) => {
46- this._opsQueue = Promise.all([this._opsQueue, promise]).then(values => {
47- const result = intefaceCallback(values.pop())
48- instance.exports.callback.get(callbackIndex)(result)
49- })
50- },
51- memory: () => {
52- return instance.exports.memory.buffer
53- }
54- },
55- kernel: kernel,
56- message: message
36+ const result = await WebAssembly.instantiate(this.exoInterface.state['/'].code, importMap)
37+ this.instance = result.instance
38+ if (this.instance.exports.main) {
39+ this.instance.exports.main()
5740 }
58- const initializedImports = buildImports(opts, imports)
59- instance = WebAssembly.Instance(this._module, initializedImports)
60-
61- if (instance.exports.main) {
62- instance.exports.main()
63- }
64- await this.onDone()
65- return responses
41+ return this.onDone()
6642 }
6743
6844 /**
6945 * returns a promise that resolves when the wasm instance is done running
@@ -71,8 +47,30 @@
7147 async onDone () {
7248 let prevOps
7349 while (prevOps !== this._opsQueue) {
7450 prevOps = this._opsQueue
75- await this._opsQueue
51+ await prevOps
7652 }
53+ this.referanceMap.clear()
7754 }
55+
56+ pushOpsQueue (promise) {
57+ this._opsQueue = Promise.all([this._opsQueue, promise])
58+ return this._opsQueue
59+ }
60+
61+ get memory () {
62+ return this.instance.exports.memory.buffer
63+ }
64+
65+ getMemory (offset, length) {
66+ return new Uint8Array(this.memory, offset, length)
67+ }
68+
69+ static createState (wasm) {
70+ return {
71+ nonce: [0],
72+ ports: {},
73+ code: wasm
74+ }
75+ }
7876 }
referanceMap.jsView
@@ -1,0 +1,27 @@
1+module.exports = class ReferanceMap {
2+ constructor () {
3+ this._map = new Map()
4+ this._nonce = 0
5+ }
6+
7+ add (obj) {
8+ this._map.set(this._nonce, obj)
9+ this._nonce++
10+ }
11+
12+ get (ref) {
13+ const obj = this._map.get(ref)
14+ if (!obj) {
15+ throw new Error('invalid referance')
16+ }
17+ return obj
18+ }
19+
20+ delete (obj) {
21+ return this._map.delete(obj)
22+ }
23+
24+ clear () {
25+ this._map.clear()
26+ }
27+}

Built with git-ssb-web