git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 32d6817f60330c28d8efbe94308e107d379bb465

Files: 32d6817f60330c28d8efbe94308e107d379bb465 / index.js

1932 bytesRaw
1const Vertex = require('merkle-trie')
2// The Kernel Exposes this Interface to VM instances it makes
3const Imports = require('./EVMimports.js')
4const VM = require('./vm.js')
5const Environment = require('./environment.js')
6
7module.exports = class Kernel extends Vertex {
8 constructor (opts = {}) {
9 opts.code = opts.value || opts.code
10 super(opts)
11
12 // if code is bound to this kernel then create the interfaceAPI and the imports
13 if (opts.code) {
14 this._vm = new VM(opts.code)
15 this.imports = buildImports(this._vm, opts.interfaces)
16 }
17
18 /**
19 * Builds a import map with an array of given interfaces
20 */
21 function buildImports (api, imports = [Imports]) {
22 return imports.reduce((obj, InterfaceConstuctor) => {
23 obj[InterfaceConstuctor.name] = new InterfaceConstuctor(api).exports
24 return obj
25 }, {})
26 }
27 }
28
29 /**
30 * run the kernels code with a given enviroment
31 * The Kernel Stores all of its state in the Environment. The Interface is used
32 * to by the VM to retrive infromation from the Environment.
33 */
34 async run (environment = new Environment({state: this}), imports = this.imports) {
35 await this._vm.run(environment, imports)
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