git ssb

0+

wanderer🌟 / js-primea-wasm-container



Commit cdbeb3cb31baff40feb98e22a70159cee381d657

updated abstract container

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 7/28/2017, 3:38:31 PM
Parent: f5b59c97800ffbfc5bc0a2e98c9ed035dea81ba7

Files changed

index.jschanged
package.jsonchanged
tests/index.jschanged
referanceMap.jsdeleted
index.jsView
@@ -1,20 +1,22 @@
1-const ReferanceMap = require('./referanceMap.js')
1+const ReferanceMap = require('reference-map')
2+const AbstractContainer = require('primea-abstract-container')
23
3-module.exports = class WasmContainer {
4+module.exports = class WasmContainer extends AbstractContainer {
45 /**
56 * The wasm container runs wasm code and provides a basic API for wasm
67 * interfaces for interacting with the kernel
78 * @param {object} kernel - the kernel instance
89 * @param {object} interfaces - a map of interfaces to expose to the wasm binary
910 */
1011 constructor (kernel, interfaces) {
12+ super()
1113 this.kernel = kernel
1214 this.imports = interfaces
1315 this.referanceMap = new ReferanceMap()
1416 }
1517
16- async initialize (message) {
18+ async onCreation (message) {
1719 let code = message.data
1820 if (!WebAssembly.validate(code)) {
1921 throw new Error('invalid wasm binary')
2022 } else {
@@ -33,9 +35,9 @@
3335 * Runs the wasm VM given a message
3436 * @param {object} message
3537 * @returns {Promise} a promise that resolves once the compuation is finished
3638 */
37- run (message) {
39+ onMessage (message) {
3840 return this._run(message, 'main')
3941 }
4042
4143 async _run (message, method) {
package.jsonView
@@ -23,10 +23,14 @@
2323 ],
2424 "devDependencies": {
2525 "ipfs": "^0.25.0",
2626 "istanbul": "^1.1.0-alpha.1",
27- "primea-hypervisor": "0.0.2",
27+ "primea-hypervisor": "0.0.3",
2828 "standard": "^10.0.0",
2929 "tape": "^4.6.3",
3030 "wast2wasm": "0.0.1"
31+ },
32+ "dependencies": {
33+ "primea-abstract-container": "0.0.4",
34+ "reference-map": "0.0.0"
3135 }
3236 }
tests/index.jsView
@@ -4,9 +4,8 @@
44 const Message = require('primea-message')
55 const WasmContainer = require('../index.js')
66 const testInterface = require('./testInterface.js')
77 const IPFS = require('ipfs')
8-const ReferanceMap = require('../referanceMap.js')
98
109 const node = new IPFS({
1110 start: false
1211 })
@@ -29,39 +28,8 @@
2928 }
3029 }
3130
3231 node.on('ready', () => {
33- tape('referance mapping', t => {
34- t.plan(6)
35- const referanceMap = new ReferanceMap()
36- const obj1 = {}
37- const obj2 = {}
38- const ref1 = referanceMap.add(obj1)
39- const ref2 = referanceMap.add(obj2)
40- t.equals(ref1, 0, 'should produce correct refs')
41- t.equals(ref2, 1, 'should produce correct refs')
42-
43- const foundObj1 = referanceMap.get(ref1)
44- const foundObj2 = referanceMap.get(ref2)
45-
46- t.equals(foundObj1, obj1, 'should get the correct object')
47- t.equals(foundObj2, obj2, 'should get the correct object')
48-
49- referanceMap.delete(ref1)
50- try {
51- referanceMap.get(ref1)
52- } catch (e) {
53- t.true(true, 'should delete refances')
54- }
55-
56- referanceMap.clear()
57- try {
58- referanceMap.get(ref2)
59- } catch (e) {
60- t.true(true, 'should clear refances')
61- }
62- })
63-
6432 tape('wasm container - main', async t => {
6533 t.plan(1)
6634 const hypervisor = new Hypervisor(node.dag)
6735 const main = fs.readFileSync(`${__dirname}/wasm/main.wasm`)
@@ -70,9 +38,9 @@
7038 })
7139 const instance = await hypervisor.createInstance('wasm', new Message({
7240 data: main
7341 }))
74- instance.run(instance.createMessage())
42+ instance.message(instance.createMessage())
7543 })
7644
7745 tape('wasm container - mem', async t => {
7846 t.plan(1)
referanceMap.jsView
@@ -1,51 +1,0 @@
1-module.exports = class ReferanceMap {
2- /**
3- * Handle mapping arbitary JS object to ints
4- */
5- constructor () {
6- this._map = new Map()
7- this._nonce = 0
8- }
9-
10- /**
11- * Adds an object to the referance map returning an int to be used as a
12- * referance
13- * @param {*} obj
14- * @return {integer}
15- */
16- add (obj) {
17- const nonce = this._nonce
18- this._map.set(this._nonce, obj)
19- this._nonce++
20- return nonce
21- }
22-
23- /**
24- * gets a POJO given a refernce as an int
25- * @param {integer} ref
26- * @return {*}
27- */
28- get (ref) {
29- const obj = this._map.get(ref)
30- if (!obj) {
31- throw new Error('invalid referance')
32- }
33- return obj
34- }
35-
36- /**
37- * deletes an object given a referance as an int
38- * @param {integer}
39- * @return {boolean} whether or not the object was deleted
40- */
41- delete (ref) {
42- return this._map.delete(ref)
43- }
44-
45- /**
46- * clears the referance map of a objects
47- */
48- clear () {
49- this._map.clear()
50- }
51-}

Built with git-ssb-web