git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 158d5b428af69e19901680edef92ece0a876a97c

Files: 158d5b428af69e19901680edef92ece0a876a97c / systemObjects.js

2129 bytesRaw
1const cbor = require('borc')
2
3const TAGS = {
4 id: 41,
5 link: 42,
6 func: 43,
7 mod: 44
8}
9
10const DEFAULTS = {
11 elem: [],
12 data: 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.finalize()
32 }
33
34 static deserialize (serialized) {
35 return decoder.decodeFirst(serialized)
36 }
37}
38
39class FunctionRef extends Serializable {
40 constructor (opts) {
41 super()
42 this.private = opts.private
43 this.identifier = opts.identifier
44 if (!(opts.id instanceof ID)) {
45 opts.id = new ID(opts.id)
46 }
47 this.destId = opts.id
48 this.params = opts.params
49 this.gas = opts.gas
50 }
51
52 encodeCBOR (gen) {
53 return gen.write(new cbor.Tagged(TAGS.func, [
54 this.private,
55 this.identifier,
56 this.params,
57 this.destId
58 ]))
59 }
60
61 set container (container) {
62 this._container = container
63 }
64}
65
66class ModuleRef extends Serializable {
67 constructor (ex, id) {
68 super()
69 this.exports = ex
70 this.id = id
71 }
72
73 getFuncRef (name) {
74 return new FunctionRef({
75 private: false,
76 identifier: name,
77 params: this.exports[name],
78 id: this.id
79 })
80 }
81
82 encodeCBOR (gen) {
83 return gen.write(new cbor.Tagged(TAGS.mod, [this.exports, this.id]))
84 }
85
86 static fromMetaJSON (json, id) {
87 const exports = {}
88 for (const ex in json.exports) {
89 const type = json.types[json.indexes[json.exports[ex].toString()]].params
90 exports[ex] = type
91 }
92 return new ModuleRef(exports, id)
93 }
94}
95
96class ID extends Serializable {
97 constructor (id) {
98 super()
99 this.id = id
100 }
101
102 encodeCBOR (gen) {
103 return gen.write(new cbor.Tagged(TAGS.id, this.id))
104 }
105}
106
107module.exports = {
108 ID,
109 FunctionRef,
110 ModuleRef,
111 DEFAULTS
112}
113

Built with git-ssb-web