Files: f51dc0dfc7b37b0d369c1f9a552d6ab0e1129055 / index.js
1860 bytesRaw
1 | const Vertex = require('merkle-trie') |
2 | const Port = require('./port.js') |
3 | const imports = require('./EVMinterface.js') |
4 | const codeHandler = require('./codeHandler.js') |
5 | const MessageQueue = require('./messageQueue') |
6 | const common = require('./common.js') |
7 | |
8 | module.exports = class Kernel { |
9 | constructor (opts = {}) { |
10 | const state = this.state = opts.state || new Vertex() |
11 | this.code = opts.code || state.value |
12 | this.path = state.path |
13 | this.imports = opts.imports || [imports] |
14 | // RENAME agent |
15 | this._vm = (opts.codeHandler || codeHandler).init(this.code) |
16 | this._messageQueue = new MessageQueue(this) |
17 | this.ports = new Port(state, Kernel) |
18 | } |
19 | |
20 | /** |
21 | * run the kernels code with a given enviroment |
22 | * The Kernel Stores all of its state in the Environment. The Interface is used |
23 | * to by the VM to retrive infromation from the Environment. |
24 | */ |
25 | async run (message, imports = this.imports) { |
26 | // const state = this.state.copy() |
27 | const result = await this._vm.run(message, this, imports) |
28 | // if (!result.execption) { |
29 | // // update the state |
30 | // this.state.set([], state) |
31 | // } |
32 | message.finished() |
33 | return result |
34 | } |
35 | |
36 | async recieve (message) { |
37 | if (message.isCyclic(this)) { |
38 | const result = await this.run(message) |
39 | return result |
40 | } else { |
41 | return this._messageQueue.add(message) |
42 | } |
43 | } |
44 | |
45 | async send (port, message) { |
46 | message.addVistedKernel(this) |
47 | // replace root with parent path to root |
48 | if (port === common.ROOT) { |
49 | port = common.PARENT |
50 | message.to = new Array(this.state.path.length).fill(common.PARENT).concat(message.to) |
51 | } |
52 | return this.ports.send(port, message) |
53 | } |
54 | |
55 | setValue (name, value) { |
56 | this.state.set(name, value) |
57 | } |
58 | |
59 | getValue (name) { |
60 | return this.state.get(name) |
61 | } |
62 | |
63 | deleteValue (name) { |
64 | return this.state.del(name) |
65 | } |
66 | } |
67 |
Built with git-ssb-web