git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 2fc16a021c1f62dcf54b542adfdc06d6ace480e9

cleanup!

wanderer committed on 7/5/2017, 3:48:27 AM
Parent: 4f7f80fb523a0e4362db909d00f48fe553b0880f

Files changed

kernel.jschanged
portManager.jschanged
tests/index.jschanged
kernel.jsView
@@ -1,5 +1,6 @@
11 const Message = require('primea-message')
2+const BN = require('bn.js')
23 const PortManager = require('./portManager.js')
34 const DeleteMessage = require('./deleteMessage')
45
56 module.exports = class Kernel {
@@ -141,16 +142,39 @@
141142 return message
142143 }
143144
144145 /**
146+ * creates a new container. Returning a port to it.
147+ * @param {String} type
148+ * @param {*} data - the data to populate the initail state with
149+ * @returns {Object}
150+ */
151+ createInstance (type, message) {
152+ let nonce = this.state.nonce
153+
154+ const id = {
155+ nonce: nonce,
156+ parent: this.id
157+ }
158+
159+ // incerment the nonce
160+ nonce = new BN(nonce)
161+ nonce.iaddn(1)
162+ this.state.nonce = nonce.toArray()
163+ this.ports.removeSentPorts(message)
164+
165+ this.hypervisor.createInstance(type, message, id)
166+ }
167+
168+ /**
145169 * sends a message to a given port
146170 * @param {Object} portRef - the port
147171 * @param {Message} message - the message
148172 */
149173 async send (port, message) {
150174 // set the port that the message came from
151175 message._fromTicks = this.ticks
152- message.ports.forEach(port => this.ports._unboundPorts.delete(port))
176+ this.ports.removeSentPorts(message)
153177
154178 // if (this.currentMessage !== message && !message.responsePort) {
155179 // this.currentMessage._addSubMessage(message)
156180 // }
portManager.jsView
@@ -1,5 +1,4 @@
1-const BN = require('bn.js')
21 const DeleteMessage = require('./deleteMessage')
32
43 // decides which message to go first
54 function messageArbiter (nameA, nameB) {
@@ -165,31 +164,8 @@
165164 return this.ports[name]
166165 }
167166
168167 /**
169- * creates a new container. Returning a port to it.
170- * @param {String} type
171- * @param {*} data - the data to populate the initail state with
172- * @returns {Object}
173- */
174- create (type, message) {
175- let nonce = this.state.nonce
176-
177- const id = {
178- nonce: nonce,
179- parent: this.id
180- }
181-
182- // incerment the nonce
183- nonce = new BN(nonce)
184- nonce.iaddn(1)
185- this.state.nonce = nonce.toArray()
186- message.ports.forEach(port => this._unboundPorts.delete(port))
187-
188- this.hypervisor.createInstance(type, message, id)
189- }
190-
191- /**
192168 * creates a channel returns the created ports in an Array
193169 * @returns {array}
194170 */
195171 createChannel () {
@@ -269,5 +245,9 @@
269245 _olderMessage (message) {
270246 this._messageTickThreshold = message ? message._fromTicks : 0
271247 return this._oldestMessagePromise
272248 }
249+
250+ removeSentPorts (message) {
251+ message.ports.forEach(port => this._unboundPorts.delete(port))
252+ }
273253 }
tests/index.jsView
@@ -44,9 +44,9 @@
4444 data: Buffer.from('test code'),
4545 ports: [portRef2]
4646 })
4747
48- rootContainer.ports.create('test', initMessage)
48+ rootContainer.createInstance('test', initMessage)
4949
5050 rootContainer.ports.bind('first', portRef1)
5151 message = rootContainer.createMessage()
5252 rootContainer.send(portRef1, message)
@@ -71,9 +71,9 @@
7171 const root = await hypervisor.createInstance('test')
7272 const [portRef1, portRef2] = root.ports.createChannel()
7373
7474 root.ports.bind('one', portRef1)
75- root.ports.create('test', root.createMessage({
75+ root.createInstance('test', root.createMessage({
7676 ports: [portRef2]
7777 }))
7878
7979 const stateRoot = await hypervisor.createStateRoot(Infinity)
@@ -95,9 +95,9 @@
9595
9696 class testVMContainer extends BaseContainer {
9797 run (m) {
9898 const [portRef1, portRef2] = this.exInterface.ports.createChannel()
99- this.exInterface.ports.create('test2', this.exInterface.createMessage({
99+ this.exInterface.createInstance('test2', this.exInterface.createMessage({
100100 ports: [portRef2]
101101 }))
102102 this.exInterface.ports.bind('child', portRef1)
103103 this.exInterface.incrementTicks(2)
@@ -110,9 +110,9 @@
110110 hypervisor.registerContainer('test2', testVMContainer2)
111111
112112 const root = await hypervisor.createInstance('test')
113113 const [portRef1, portRef2] = root.ports.createChannel()
114- root.ports.create('test', root.createMessage({
114+ root.createInstance('test', root.createMessage({
115115 ports: [portRef2]
116116 }))
117117
118118 root.ports.bind('first', portRef1)
@@ -148,9 +148,9 @@
148148
149149 class testVMContainer extends BaseContainer {
150150 run (m) {
151151 const [portRef1, portRef2] = this.exInterface.ports.createChannel()
152- this.exInterface.ports.create('test2', this.exInterface.createMessage({
152+ this.exInterface.createInstance('test2', this.exInterface.createMessage({
153153 ports: [portRef2]
154154 }))
155155 this.exInterface.ports.bind('child', portRef1)
156156 this.exInterface.send(portRef1, m)
@@ -164,9 +164,9 @@
164164
165165 let root = await hypervisor.createInstance('test')
166166 const rootId = root.id
167167 const [portRef1, portRef2] = root.ports.createChannel()
168- root.ports.create('test', root.createMessage({
168+ root.createInstance('test', root.createMessage({
169169 ports: [portRef2]
170170 }))
171171
172172 root.ports.bind('first', portRef1)
@@ -213,11 +213,11 @@
213213 const message3 = this.exInterface.createMessage({
214214 ports: [portRef6]
215215 })
216216
217- this.exInterface.ports.create('root', message1)
218- this.exInterface.ports.create('root', message2)
219- this.exInterface.ports.create('root', message3)
217+ this.exInterface.createInstance('root', message1)
218+ this.exInterface.createInstance('root', message2)
219+ this.exInterface.createInstance('root', message3)
220220
221221 throw new Error('it is a trap!!!')
222222 }
223223 }
@@ -255,10 +255,10 @@
255255 const message2 = this.exInterface.createMessage({
256256 ports: [portRef4]
257257 })
258258
259- this.exInterface.ports.create('first', message1)
260- this.exInterface.ports.create('second', message2)
259+ this.exInterface.createInstance('first', message1)
260+ this.exInterface.createInstance('second', message2)
261261
262262 this.exInterface.send(portRef1, this.exInterface.createMessage())
263263 this.exInterface.send(portRef3, this.exInterface.createMessage())
264264 } else if (runs === 1) {
@@ -296,9 +296,9 @@
296296
297297 const root = await hypervisor.createInstance('root')
298298
299299 const [portRef1, portRef2] = root.ports.createChannel()
300- root.ports.create('root', root.createMessage({
300+ root.createInstance('root', root.createMessage({
301301 ports: [portRef2]
302302 }))
303303
304304 root.ports.bind('first', portRef1)
@@ -327,10 +327,10 @@
327327 const message2 = this.exInterface.createMessage({
328328 ports: [portRef4]
329329 })
330330
331- this.exInterface.ports.create('first', message1)
332- this.exInterface.ports.create('second', message2)
331+ this.exInterface.createInstance('first', message1)
332+ this.exInterface.createInstance('second', message2)
333333
334334 this.exInterface.send(portRef1, this.exInterface.createMessage())
335335 this.exInterface.send(portRef3, this.exInterface.createMessage())
336336 } else if (runs === 1) {
@@ -368,9 +368,9 @@
368368
369369 const root = await hypervisor.createInstance('root')
370370
371371 const [portRef1, portRef2] = root.ports.createChannel()
372- root.ports.create('root', root.createMessage({
372+ root.createInstance('root', root.createMessage({
373373 ports: [portRef2]
374374 }))
375375
376376 root.ports.bind('first', portRef1)
@@ -398,10 +398,10 @@
398398 const message2 = this.exInterface.createMessage({
399399 ports: [portRef4]
400400 })
401401
402- this.exInterface.ports.create('first', message1)
403- this.exInterface.ports.create('second', message2)
402+ this.exInterface.createInstance('first', message1)
403+ this.exInterface.createInstance('second', message2)
404404
405405 this.exInterface.send(portRef1, this.exInterface.createMessage())
406406 this.exInterface.send(portRef3, this.exInterface.createMessage())
407407
@@ -440,9 +440,9 @@
440440 hypervisor.registerContainer('second', Second)
441441
442442 const root = await hypervisor.createInstance('root')
443443 const [portRef1, portRef2] = root.ports.createChannel()
444- root.ports.create('root', root.createMessage({
444+ root.createInstance('root', root.createMessage({
445445 ports: [portRef2]
446446 }))
447447
448448 root.ports.bind('first', portRef1)
@@ -470,10 +470,10 @@
470470 const message2 = this.exInterface.createMessage({
471471 ports: [portRef4]
472472 })
473473
474- this.exInterface.ports.create('first', message1)
475- this.exInterface.ports.create('second', message2)
474+ this.exInterface.createInstance('first', message1)
475+ this.exInterface.createInstance('second', message2)
476476
477477 this.exInterface.send(portRef1, this.exInterface.createMessage())
478478 this.exInterface.send(portRef3, this.exInterface.createMessage())
479479
@@ -528,15 +528,15 @@
528528
529529 const message = root.createMessage()
530530 root.send(portRef1, message)
531531 root.ports.bind('first', portRef1)
532- root.ports.create('root', root.createMessage({
532+ root.createInstance('root', root.createMessage({
533533 ports: [portRef2]
534534 }))
535535
536536 const [portRef3, portRef4] = root.ports.createChannel()
537537 root.ports.bind('sencond', portRef3)
538- root.ports.create('waiter', root.createMessage({
538+ root.createInstance('waiter', root.createMessage({
539539 ports: [portRef4]
540540 }))
541541
542542 root.incrementTicks(100)
@@ -565,10 +565,10 @@
565565 const message2 = this.exInterface.createMessage({
566566 ports: [portRef4]
567567 })
568568
569- this.exInterface.ports.create('first', message1)
570- this.exInterface.ports.create('second', message2)
569+ this.exInterface.createInstance('first', message1)
570+ this.exInterface.createInstance('second', message2)
571571
572572 this.exInterface.send(portRef1, this.exInterface.createMessage())
573573 this.exInterface.send(portRef3, this.exInterface.createMessage())
574574
@@ -611,9 +611,9 @@
611611 const message = root.createMessage()
612612
613613 root.send(portRef1, message)
614614 root.ports.bind('first', portRef1)
615- root.ports.create('root', root.createMessage({
615+ root.createInstance('root', root.createMessage({
616616 ports: [portRef2]
617617 }))
618618 })
619619
@@ -638,10 +638,10 @@
638638 const message2 = this.exInterface.createMessage({
639639 ports: [portRef4]
640640 })
641641
642- this.exInterface.ports.create('first', message1)
643- this.exInterface.ports.create('second', message2)
642+ this.exInterface.createInstance('first', message1)
643+ this.exInterface.createInstance('second', message2)
644644
645645 this.exInterface.send(portRef1, this.exInterface.createMessage())
646646 this.exInterface.send(portRef3, this.exInterface.createMessage())
647647
@@ -684,9 +684,9 @@
684684 const message = root.createMessage()
685685
686686 root.send(portRef1, message)
687687 root.ports.bind('first', portRef1)
688- root.ports.create('root', root.createMessage({
688+ root.createInstance('root', root.createMessage({
689689 ports: [portRef2]
690690 }))
691691 })
692692
@@ -704,9 +704,9 @@
704704 this.exInterface.ports.bind('one', portRef1)
705705 const message1 = this.exInterface.createMessage({
706706 ports: [portRef2]
707707 })
708- this.exInterface.ports.create('first', message1)
708+ this.exInterface.createInstance('first', message1)
709709 } else {
710710 this.exInterface.send(one, this.exInterface.createMessage())
711711 this.exInterface.send(one, this.exInterface.createMessage())
712712 }
@@ -731,9 +731,9 @@
731731
732732 const root = await hypervisor.createInstance('root')
733733 const [portRef1, portRef2] = root.ports.createChannel()
734734 root.ports.bind('first', portRef1)
735- root.ports.create('root', root.createMessage({
735+ root.createInstance('root', root.createMessage({
736736 ports: [portRef2]
737737 }))
738738
739739 const message = root.createMessage()
@@ -751,9 +751,9 @@
751751
752752 const root = await hypervisor.createInstance('base')
753753
754754 const [portRef1, portRef2] = root.ports.createChannel()
755- root.ports.create('base', root.createMessage({
755+ root.createInstance('base', root.createMessage({
756756 ports: [portRef2]
757757 }))
758758 root.ports.bind('test', portRef1)
759759
@@ -796,9 +796,9 @@
796796 const message1 = this.exInterface.createMessage({
797797 ports: [portRef2]
798798 })
799799
800- this.exInterface.ports.create('first', message1)
800+ this.exInterface.createInstance('first', message1)
801801 this.exInterface.send(portRef1, this.exInterface.createMessage())
802802 this.exInterface.incrementTicks(6)
803803 }
804804 }
@@ -817,9 +817,9 @@
817817
818818 const root = await hypervisor.createInstance('root')
819819 const [portRef1, portRef2] = root.ports.createChannel()
820820 root.ports.bind('first', portRef1)
821- root.ports.create('root', root.createMessage({
821+ root.createInstance('root', root.createMessage({
822822 ports: [portRef2]
823823 }))
824824
825825 const message = root.createMessage()
@@ -836,9 +836,9 @@
836836 '/': 'zdpuB2QXxn1KQtLFfBqaritTRoe5BuKP5sNFSrPtRT6sxkY7Z'
837837 }
838838 class Root extends BaseContainer {
839839 run (m) {
840- this.exInterface.ports.create('root')
840+ this.exInterface.createInstance('root')
841841 }
842842 }
843843
844844 const hypervisor = new Hypervisor(node.dag)
@@ -847,9 +847,9 @@
847847
848848 const root = await hypervisor.createInstance('root')
849849 const [portRef1, portRef2] = root.ports.createChannel()
850850 root.ports.bind('first', portRef1)
851- root.ports.create('root', root.createMessage({
851+ root.createInstance('root', root.createMessage({
852852 ports: [portRef2]
853853 }))
854854
855855 const message = root.createMessage()
@@ -866,9 +866,9 @@
866866 }
867867 class Root extends BaseContainer {
868868 run (m) {
869869 const [, portRef2] = this.exInterface.ports.createChannel()
870- this.exInterface.ports.create('sub', this.exInterface.createMessage({
870+ this.exInterface.createInstance('sub', this.exInterface.createMessage({
871871 ports: [portRef2]
872872 }))
873873 }
874874 }
@@ -877,9 +877,9 @@
877877 initailize (message) {
878878 this.exInterface.ports.bind('root', message.ports[0])
879879 const [portRef1, portRef2] = root.ports.createChannel()
880880 root.ports.bind('child', portRef1)
881- root.ports.create('root', root.createMessage({
881+ root.createInstance('root', root.createMessage({
882882 ports: [portRef2]
883883 }))
884884 }
885885 }
@@ -891,9 +891,9 @@
891891
892892 const root = await hypervisor.createInstance('root')
893893 const [portRef1, portRef2] = root.ports.createChannel()
894894 root.ports.bind('first', portRef1)
895- root.ports.create('root', root.createMessage({
895+ root.createInstance('root', root.createMessage({
896896 ports: [portRef2]
897897 }))
898898
899899 root.send(portRef1, root.createMessage())
@@ -914,15 +914,15 @@
914914 this.exInterface.send(port, m)
915915 this.exInterface.ports.unbind('test1')
916916 } else {
917917 const [portRef1, portRef2] = this.exInterface.ports.createChannel()
918- this.exInterface.ports.create('sub', this.exInterface.createMessage({
918+ this.exInterface.createInstance('sub', this.exInterface.createMessage({
919919 ports: [portRef2]
920920 }))
921921 this.exInterface.ports.bind('test1', portRef1)
922922
923923 const [portRef3, portRef4] = this.exInterface.ports.createChannel()
924- this.exInterface.ports.create('sub', this.exInterface.createMessage({
924+ this.exInterface.createInstance('sub', this.exInterface.createMessage({
925925 ports: [portRef4]
926926 }))
927927 this.exInterface.ports.bind('test2', portRef3)
928928 this.exInterface.send(portRef3, this.exInterface.createMessage({
@@ -954,9 +954,9 @@
954954
955955 const root = await hypervisor.createInstance('root')
956956 const [portRef1, portRef2] = root.ports.createChannel()
957957 root.ports.bind('first', portRef1)
958- root.ports.create('root', root.createMessage({
958+ root.createInstance('root', root.createMessage({
959959 ports: [portRef2]
960960 }))
961961
962962 root.send(portRef1, root.createMessage())
@@ -979,15 +979,15 @@
979979 this.exInterface.ports.unbind('test1')
980980 this.exInterface.ports.unbind('test2')
981981 } else {
982982 const [portRef1, portRef2] = this.exInterface.ports.createChannel()
983- this.exInterface.ports.create('sub', this.exInterface.createMessage({
983+ this.exInterface.createInstance('sub', this.exInterface.createMessage({
984984 ports: [portRef2]
985985 }))
986986 this.exInterface.ports.bind('test1', portRef1)
987987
988988 const [portRef3, portRef4] = this.exInterface.ports.createChannel()
989- this.exInterface.ports.create('sub', this.exInterface.createMessage({
989+ this.exInterface.createInstance('sub', this.exInterface.createMessage({
990990 ports: [portRef4]
991991 }))
992992 this.exInterface.ports.bind('test2', portRef3)
993993 this.exInterface.send(portRef3, this.exInterface.createMessage({
@@ -1020,9 +1020,9 @@
10201020 const root = await hypervisor.createInstance('root')
10211021
10221022 const [portRef1, portRef2] = root.ports.createChannel()
10231023 root.ports.bind('first', portRef1)
1024- root.ports.create('root', root.createMessage({
1024+ root.createInstance('root', root.createMessage({
10251025 ports: [portRef2]
10261026 }))
10271027
10281028 root.send(portRef1, root.createMessage())
@@ -1059,9 +1059,9 @@
10591059 const initMessage = rootContainer.createMessage({
10601060 ports: [portRef2]
10611061 })
10621062
1063- rootContainer.ports.create('test', initMessage)
1063+ rootContainer.createInstance('test', initMessage)
10641064
10651065 rootContainer.ports.bind('first', portRef1)
10661066 const message = rootContainer.createMessage()
10671067 const rPort = rootContainer.getResponsePort(message)

Built with git-ssb-web