git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 90c78a592cb45542546f035167e39d219a6eaaf1

Files: 90c78a592cb45542546f035167e39d219a6eaaf1 / index.js

1798 bytesRaw
1const Vertex = require('merkle-trie')
2const Port = require('./port.js')
3const imports = require('./EVMinterface.js')
4const codeHandler = require('./codeHandler.js')
5const MessageQueue = require('./messageQueue')
6const common = require('./common.js')
7
8module.exports = class Kernel {
9 constructor (opts = {}) {
10 const state = this.state = opts.state || new Vertex()
11 state.value = 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(state.value)
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, state)
28 if (!result.execption) {
29 // update the state
30 this.state.set([], state)
31 }
32 return result
33 }
34
35 async recieve (message) {
36 if (message.isCyclic(this)) {
37 const result = await this.run(message)
38 message.finished()
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

Built with git-ssb-web