Files: 9bb00a66c6590ce780568e10d649be74c66ce1e6 / index.js
3003 bytesRaw
1 | const EventEmitter = require('events') |
2 | const Vertex = require('merkle-trie') |
3 | const PortManager = require('./portManager.js') |
4 | const StateInterface = require('./stateInterface.js') |
5 | const imports = require('./EVMinterface.js') |
6 | const codeHandler = require('./codeHandler.js') |
7 | const common = require('./common.js') |
8 | |
9 | module.exports = class Kernel extends EventEmitter { |
10 | constructor (opts = {}) { |
11 | super() |
12 | const state = this.state = opts.state || new Vertex() |
13 | this.stateInterface = new StateInterface(state) |
14 | this.code = opts.code || state.value |
15 | this.path = state.path |
16 | this.imports = opts.imports || [imports] |
17 | this.ports = new PortManager(state, opts.parent, Kernel) |
18 | this._sentAtomicMessages = [] |
19 | // rename sandbox? |
20 | this._vm = (opts.codeHandler || codeHandler).init(this.code) |
21 | this._state = 'idle' |
22 | this.ports.on('message', index => { |
23 | this.runNextMessage(index) |
24 | }) |
25 | } |
26 | |
27 | runNextMessage (index = 0) { |
28 | this.ports.peek(index).then(message => { |
29 | if (message && (message.isCyclic(this) || this._state === 'idle')) { |
30 | this.ports.remove(index) |
31 | this.run(message) |
32 | } else { |
33 | this._state = 'idle' |
34 | this.emit('idle') |
35 | } |
36 | }) |
37 | } |
38 | |
39 | /** |
40 | * run the kernels code with a given enviroment |
41 | * The Kernel Stores all of its state in the Environment. The Interface is used |
42 | * to by the VM to retrive infromation from the Environment. |
43 | */ |
44 | async run (message, imports = this.imports) { |
45 | function revert () { |
46 | // revert the state |
47 | this.state.set([], oldState) |
48 | // revert all the sent messages |
49 | for (let msg in this._sentAtomicMessages) { |
50 | msg.revert() |
51 | } |
52 | this.runNextMessage(0) |
53 | } |
54 | |
55 | const oldState = this.state.copy() |
56 | let result |
57 | this._state = 'running' |
58 | try { |
59 | result = await this._vm.run(message, this, imports) || {} |
60 | } catch (e) { |
61 | console.log(e) |
62 | result = { |
63 | exception: true |
64 | } |
65 | } |
66 | if (result.execption) { |
67 | // failed messages |
68 | revert() |
69 | } else if (message.atomic) { |
70 | // messages |
71 | message._finish(result) |
72 | message.result().then(result => { |
73 | if (result.execption) { |
74 | revert() |
75 | } else { |
76 | this.runNextMessage(0) |
77 | } |
78 | }) |
79 | } else { |
80 | // non-atomic messages |
81 | this.runNextMessage(0) |
82 | } |
83 | return result |
84 | } |
85 | |
86 | async send (message) { |
87 | // replace root with parent path to root |
88 | let portName = message.nextPort() |
89 | if (portName === common.ROOT) { |
90 | message.to.shift() |
91 | message.to = new Array(this.path.length).fill(common.PARENT).concat(message.to) |
92 | portName = common.PARENT |
93 | } |
94 | message.addVistedKernel(message) |
95 | this.lastMessage = message |
96 | // console.log(portName, message) |
97 | const port = await this.ports.get(portName) |
98 | // save the atomic messages for possible reverts |
99 | if (message.atomic) { |
100 | this._sentAtomicMessages.push(message) |
101 | } |
102 | return port.send(message) |
103 | } |
104 | |
105 | shutdown () {} |
106 | } |
107 |
Built with git-ssb-web