git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: de87b9b68f0638e5deda64385e6c49c506325b8e

Files: de87b9b68f0638e5deda64385e6c49c506325b8e / index.js

1636 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 {
8 constructor (opts = {}) {
9 opts.state = opts.state || new Vertex(opts.code)
10 opts.code = opts.state.value || opts.code
11
12 // if code is bound to this kernel then create the interfaceAPI and the imports
13 this._vm = new VM(opts.code)
14 this.imports = buildImports(this._vm, opts.interfaces)
15
16 /**
17 * Builds a import map with an array of given interfaces
18 */
19 function buildImports (api, imports = [Imports]) {
20 return imports.reduce((obj, InterfaceConstuctor) => {
21 obj[InterfaceConstuctor.name] = new InterfaceConstuctor(api).exports
22 return obj
23 }, {})
24 }
25 }
26
27 /**
28 * run the kernels code with a given enviroment
29 * The Kernel Stores all of its state in the Environment. The Interface is used
30 * to by the VM to retrive infromation from the Environment.
31 */
32 async run (environment = new Environment({state: this}), imports = this.imports) {
33 await this._vm.run(environment, imports)
34 }
35
36 async messageReceiver (message) {
37 // let the code handle the message if there is code
38 const environment = new Environment(message)
39 let result = await this.run(environment)
40 if (!result.execption) {
41 this.state = result.state
42 }
43 }
44
45 copy () {
46 return new Kernel({
47 state: this.state.copy(),
48 code: this.code,
49 interfaces: this.interfaces,
50 parent: this.parent
51 })
52 }
53}
54

Built with git-ssb-web