git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 277b81fa0326007d061917f6ea1efa15f8ad5392

Files: 277b81fa0326007d061917f6ea1efa15f8ad5392 / index.js

1890 bytesRaw
1const Vertex = require('./deps/kernelVertex')
2// The Kernel Exposes this Interface to VM instances it makes
3const Interface = require('./interface.js')
4const InterfaceAPI = require('./interfaceAPI.js')
5const Environment = require('./environment.js')
6
7module.exports = class Kernel {
8 constructor (opts = {}) {
9 this.state = opts.state || new Vertex()
10 this.parent = opts.parent
11
12 if (opts.code) {
13 this.interfaceAPI = new InterfaceAPI(opts.code)
14 }
15 this.imports = this.buildImports(opts.interfaces)
16 }
17
18 /**
19 * Builds a import map with an array of given interfaces
20 */
21 buildImports (interfaces = [Interface]) {
22 return interfaces.reduce((obj, Interface) => {
23 obj[Interface.name] = new Interface(this.interfaceAPI).exports
24 return obj
25 }, {})
26 }
27
28 /**
29 * run the kernels code with a given enviroment
30 * The Kernel Stores all of its state in the Environment. The Interface is used
31 * to by the VM to retrive infromation from the Environment.
32 */
33 async run (environment = new Environment({state: this.state}), imports = this.imports) {
34 await this.interfaceAPI.run(environment, imports)
35 return environment
36 }
37
38 async messageReceiver (message) {
39 // let the code handle the message if there is code
40 if (this.code) {
41 const environment = new Environment(message)
42 let result = await this.run(environment)
43 if (!result.execption) {
44 this.state = result.state
45 }
46 } else if (message.to.length) {
47 // else forward the message on to the destination contract
48 let [vertex, done] = await this.state.update(message.to)
49 message.to = []
50 await vertex.kernel.messageReceiver(message)
51 done(vertex)
52 }
53 }
54
55 copy () {
56 return new Kernel({
57 state: this.state.copy(),
58 code: this.code,
59 interfaces: this.interfaces,
60 parent: this.parent
61 })
62 }
63}
64

Built with git-ssb-web