Commit 3a2129ef21fb40c0212b10d052749c0437d93ffc
remove opts
wanderer committed on 4/29/2017, 4:31:32 PMParent: 4a1f9a53b0854ec031013d473ac2f51e826d4dc5
Files changed
index.js | changed |
kernel.js | changed |
portManager.js | changed |
index.js | ||
---|---|---|
@@ -9,17 +9,12 @@ | ||
9 | 9 | } |
10 | 10 | |
11 | 11 | module.exports = class Hypervisor { |
12 | 12 | constructor (opts) { |
13 | - this._opts = { | |
14 | - VMs: {} | |
15 | - } | |
16 | - | |
17 | 13 | this.graph = new Graph(opts.dag) |
18 | - delete opts.dag | |
19 | 14 | this.root = new Root() |
20 | 15 | this._vmInstances = new Map([[null, new Root()]]) |
21 | - Object.assign(this._opts, opts) | |
16 | + this._VMs = {} | |
22 | 17 | } |
23 | 18 | |
24 | 19 | async getInstance (port) { |
25 | 20 | let id = await this.generateID(port) |
@@ -28,12 +23,13 @@ | ||
28 | 23 | // load the container from the state |
29 | 24 | await this.graph.tree(port, 2) |
30 | 25 | |
31 | 26 | // create a new kernel instance |
32 | - const VM = this._opts.VMs[port.type] | |
27 | + const VM = this._VMs[port.type] | |
33 | 28 | |
34 | 29 | kernel = new Kernel({ |
35 | 30 | parentPort: port, |
31 | + state: port.link['/'], | |
36 | 32 | hypervisor: this, |
37 | 33 | VM: VM |
38 | 34 | }) |
39 | 35 | |
@@ -60,9 +56,9 @@ | ||
60 | 56 | return kernel.wait(threshold) |
61 | 57 | } |
62 | 58 | |
63 | 59 | createPort (type, id = {nonce: [0], parent: null}) { |
64 | - const VM = this._opts.VMs[type] | |
60 | + const VM = this._VMs[type] | |
65 | 61 | return { |
66 | 62 | 'messages': [], |
67 | 63 | 'id': { |
68 | 64 | '/': id |
@@ -93,7 +89,7 @@ | ||
93 | 89 | return id |
94 | 90 | } |
95 | 91 | |
96 | 92 | addVM (type, vm) { |
97 | - this._opts.VMs[type] = vm | |
93 | + this._VMs[type] = vm | |
98 | 94 | } |
99 | 95 | } |
kernel.js | ||
---|---|---|
@@ -5,13 +5,21 @@ | ||
5 | 5 | |
6 | 6 | module.exports = class Kernel extends EventEmitter { |
7 | 7 | constructor (opts) { |
8 | 8 | super() |
9 | - this._opts = opts | |
10 | - this.state = opts.parentPort.link['/'] | |
9 | + this.state = opts.state | |
10 | + this.parentPort = opts.parentPort | |
11 | + this.hypervisor = opts.hypervisor | |
12 | + | |
11 | 13 | this.vmState = 'idle' |
12 | 14 | this.ticks = 0 |
13 | - this.ports = new PortManager(this) | |
15 | + // create the port manager | |
16 | + this.ports = new PortManager({ | |
17 | + kernel: this, | |
18 | + hypervisor: opts.hypervisor, | |
19 | + ports: opts.state.ports, | |
20 | + parentPort: opts.parentPort | |
21 | + }) | |
14 | 22 | this.vm = new opts.VM(this) |
15 | 23 | this._waitingQueue = new PriorityQueue((a, b) => { |
16 | 24 | return a.threshold > b.threshold |
17 | 25 | }) |
@@ -56,19 +64,19 @@ | ||
56 | 64 | * to by the VM to retrive infromation from the Environment. |
57 | 65 | */ |
58 | 66 | async _run (message) { |
59 | 67 | // shallow copy |
60 | - const oldState = Object.assign({}, this._opts.state) | |
68 | + const oldState = Object.assign({}, this.state) | |
61 | 69 | let result |
62 | 70 | try { |
63 | 71 | result = await this.vm.run(message) || {} |
64 | 72 | } catch (e) { |
65 | 73 | result = { |
66 | 74 | exception: true, |
67 | 75 | exceptionError: e |
68 | 76 | } |
69 | - clearObject(this._opts.state) | |
70 | - Object.assign(this._opts.state, oldState) | |
77 | + clearObject(this.state) | |
78 | + Object.assign(this.state, oldState) | |
71 | 79 | } |
72 | 80 | |
73 | 81 | this.emit('result', result) |
74 | 82 | return result |
@@ -106,11 +114,11 @@ | ||
106 | 114 | const nonce = new BN(this.state.nonce) |
107 | 115 | nonce.iaddn(1) |
108 | 116 | this.state.nonce = nonce.toArray() |
109 | 117 | |
110 | - let portRef = this._opts.hypervisor.createPort(type, { | |
118 | + let portRef = this.hypervisor.createPort(type, { | |
111 | 119 | nonce: this.state.nonce, |
112 | - parent: this._opts.parentPort.id | |
120 | + parent: this.parentPort.id | |
113 | 121 | }) |
114 | 122 | await manager.set(name, portRef) |
115 | 123 | return portRef |
116 | 124 | } |
@@ -122,9 +130,9 @@ | ||
122 | 130 | async send (portRef, message) { |
123 | 131 | message._ticks = this.ticks |
124 | 132 | const portInstance = await this.ports.get(portRef) |
125 | 133 | portInstance.hasSent = true |
126 | - return this._opts.hypervisor.send(portRef, message) | |
134 | + return this.hypervisor.send(portRef, message) | |
127 | 135 | } |
128 | 136 | } |
129 | 137 | |
130 | 138 | function clearObject (myObject) { |
portManager.js | ||
---|---|---|
@@ -23,13 +23,10 @@ | ||
23 | 23 | } |
24 | 24 | } |
25 | 25 | |
26 | 26 | module.exports = class PortManager { |
27 | - constructor (kernel) { | |
28 | - this.kernel = kernel | |
29 | - this.hypervisor = kernel._opts.hypervisor | |
30 | - this.ports = kernel.state.ports | |
31 | - this.parentPort = kernel._opts.parentPort | |
27 | + constructor (opts) { | |
28 | + Object.assign(this, opts) | |
32 | 29 | this.parentId = { |
33 | 30 | id: this.parentPort.id['/'].parent |
34 | 31 | } |
35 | 32 | this._portMap = new Map() |
Built with git-ssb-web