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