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.js | changed |
package.json | changed |
tests/index.js | changed |
referanceMap.js | deleted |
index.js | ||
---|---|---|
@@ -1,20 +1,22 @@ | ||
1 | -const ReferanceMap = require('./referanceMap.js') | |
1 | +const ReferanceMap = require('reference-map') | |
2 | +const AbstractContainer = require('primea-abstract-container') | |
2 | 3 | |
3 | -module.exports = class WasmContainer { | |
4 | +module.exports = class WasmContainer extends AbstractContainer { | |
4 | 5 | /** |
5 | 6 | * The wasm container runs wasm code and provides a basic API for wasm |
6 | 7 | * interfaces for interacting with the kernel |
7 | 8 | * @param {object} kernel - the kernel instance |
8 | 9 | * @param {object} interfaces - a map of interfaces to expose to the wasm binary |
9 | 10 | */ |
10 | 11 | constructor (kernel, interfaces) { |
12 | + super() | |
11 | 13 | this.kernel = kernel |
12 | 14 | this.imports = interfaces |
13 | 15 | this.referanceMap = new ReferanceMap() |
14 | 16 | } |
15 | 17 | |
16 | - async initialize (message) { | |
18 | + async onCreation (message) { | |
17 | 19 | let code = message.data |
18 | 20 | if (!WebAssembly.validate(code)) { |
19 | 21 | throw new Error('invalid wasm binary') |
20 | 22 | } else { |
@@ -33,9 +35,9 @@ | ||
33 | 35 | * Runs the wasm VM given a message |
34 | 36 | * @param {object} message |
35 | 37 | * @returns {Promise} a promise that resolves once the compuation is finished |
36 | 38 | */ |
37 | - run (message) { | |
39 | + onMessage (message) { | |
38 | 40 | return this._run(message, 'main') |
39 | 41 | } |
40 | 42 | |
41 | 43 | async _run (message, method) { |
package.json | ||
---|---|---|
@@ -23,10 +23,14 @@ | ||
23 | 23 | ], |
24 | 24 | "devDependencies": { |
25 | 25 | "ipfs": "^0.25.0", |
26 | 26 | "istanbul": "^1.1.0-alpha.1", |
27 | - "primea-hypervisor": "0.0.2", | |
27 | + "primea-hypervisor": "0.0.3", | |
28 | 28 | "standard": "^10.0.0", |
29 | 29 | "tape": "^4.6.3", |
30 | 30 | "wast2wasm": "0.0.1" |
31 | + }, | |
32 | + "dependencies": { | |
33 | + "primea-abstract-container": "0.0.4", | |
34 | + "reference-map": "0.0.0" | |
31 | 35 | } |
32 | 36 | } |
tests/index.js | ||
---|---|---|
@@ -4,9 +4,8 @@ | ||
4 | 4 | const Message = require('primea-message') |
5 | 5 | const WasmContainer = require('../index.js') |
6 | 6 | const testInterface = require('./testInterface.js') |
7 | 7 | const IPFS = require('ipfs') |
8 | -const ReferanceMap = require('../referanceMap.js') | |
9 | 8 | |
10 | 9 | const node = new IPFS({ |
11 | 10 | start: false |
12 | 11 | }) |
@@ -29,39 +28,8 @@ | ||
29 | 28 | } |
30 | 29 | } |
31 | 30 | |
32 | 31 | 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 | - | |
64 | 32 | tape('wasm container - main', async t => { |
65 | 33 | t.plan(1) |
66 | 34 | const hypervisor = new Hypervisor(node.dag) |
67 | 35 | const main = fs.readFileSync(`${__dirname}/wasm/main.wasm`) |
@@ -70,9 +38,9 @@ | ||
70 | 38 | }) |
71 | 39 | const instance = await hypervisor.createInstance('wasm', new Message({ |
72 | 40 | data: main |
73 | 41 | })) |
74 | - instance.run(instance.createMessage()) | |
42 | + instance.message(instance.createMessage()) | |
75 | 43 | }) |
76 | 44 | |
77 | 45 | tape('wasm container - mem', async t => { |
78 | 46 | t.plan(1) |
referanceMap.js | ||
---|---|---|
@@ -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