git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: b3e32df171921ad8cbc02f8ac9c39e05f2d8f4ab

Files: b3e32df171921ad8cbc02f8ac9c39e05f2d8f4ab / systemObjects.js

2067 bytesRaw
1const cbor = require('cbor')
2
3const TAGS = {
4 id: 41,
5 link: 42,
6 func: 43,
7 mod: 44
8}
9
10const DEFAULTS = {
11 elem: [],
12 buf: Buffer.from([]),
13 id: new cbor.Tagged(TAGS.id, 0),
14 mod: new cbor.Tagged(TAGS.mod, [{}, new cbor.Tagged(TAGS.id, 0)]),
15 link: {'/': null},
16 func: new cbor.Tagged(TAGS.func, 0)
17}
18
19const decoder = new cbor.Decoder({
20 tags: {
21 [TAGS.id]: val => new ID(val),
22 [TAGS.func]: val => new FunctionRef(...val),
23 [TAGS.mod]: val => new ModuleRef(...val),
24 }
25})
26
27class Serializable {
28 serialize () {
29 const encoder = new cbor.Encoder()
30 this.encodeCBOR(encoder)
31 return encoder.read().toString('hex')
32 }
33
34 static deserialize (serialized) {
35 decoder.push(Buffer.from(serialized, 'hex'))
36 return decoder.read()
37 }
38}
39
40class FunctionRef extends Serializable {
41 constructor (privateFunc, identifier, params, id, gas=0) {
42 super()
43 this.private = privateFunc
44 this.identifier = identifier
45 this.destId = id
46 this.params = params
47 this.gas = gas
48 }
49
50 encodeCBOR (gen) {
51 return gen.write(new cbor.Tagged(TAGS.func, [
52 this.private,
53 this.identifier,
54 this.params,
55 this.destId,
56 this.gas
57 ]))
58 }
59
60 set container (container) {
61 this._container = container
62 }
63}
64
65class ModuleRef extends Serializable {
66 constructor (ex, id) {
67 super()
68 this.exports = ex
69 this.id = id
70 }
71
72 getFuncRef (name) {
73 return new FunctionRef(false, name, this.exports[name], this.id)
74 }
75
76 encodeCBOR (gen) {
77 return gen.write(new cbor.Tagged(TAGS.mod, [this.exports, this.id]))
78 }
79
80 static fromMetaJSON (json, id) {
81 const exports = {}
82 for (const ex in json.exports) {
83 const type = json.types[json.indexes[json.exports[ex].toString()]].params
84 exports[ex] = type
85 }
86 return new ModuleRef(exports, id)
87 }
88}
89
90class ID extends Serializable {
91 constructor (id) {
92 super()
93 this.id = id
94 }
95
96 encodeCBOR (gen) {
97 return gen.write(new cbor.Tagged(TAGS.id, this.id))
98 }
99}
100
101module.exports = {
102 ID,
103 FunctionRef,
104 ModuleRef,
105 DEFAULTS
106}
107

Built with git-ssb-web