Files: fe338fdb6c586d0d30ccf3d53e445c347b67e8aa / index.js
1867 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 | // |
33 | message.finished() |
34 | return result |
35 | } |
36 | |
37 | async recieve (message) { |
38 | if (message.isCyclic(this)) { |
39 | const result = await this.run(message) |
40 | return result |
41 | } else { |
42 | return this._messageQueue.add(message) |
43 | } |
44 | } |
45 | |
46 | async send (port, message) { |
47 | message.addVistedKernel(this) |
48 | // replace root with parent path to root |
49 | if (port === common.ROOT) { |
50 | port = common.PARENT |
51 | message.to = new Array(this.state.path.length).fill(common.PARENT).concat(message.to) |
52 | } |
53 | return this.ports.send(port, message) |
54 | } |
55 | |
56 | setValue (name, value) { |
57 | this.state.set(name, value) |
58 | } |
59 | |
60 | getValue (name) { |
61 | return this.state.get(name) |
62 | } |
63 | |
64 | deleteValue (name) { |
65 | return this.state.del(name) |
66 | } |
67 | } |
68 |
Built with git-ssb-web