git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 4764e106f64da9641c4d72e1e6974d46024fbab3

Files: 4764e106f64da9641c4d72e1e6974d46024fbab3 / systemObjects.js

2058 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 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.finalize()
32 }
33
34 static deserialize (serialized) {
35 return decoder.decodeFirst(serialized)
36 }
37}
38
39class FunctionRef extends Serializable {
40 constructor (privateFunc, identifier, params, id, gas=0) {
41 super()
42 this.private = privateFunc
43 this.identifier = identifier
44 if (!(id instanceof ID))
45 id = new ID(id)
46 this.destId = id
47 this.params = params
48 this.gas = gas
49 }
50
51 encodeCBOR (gen) {
52 return gen.write(new cbor.Tagged(TAGS.func, [
53 this.private,
54 this.identifier,
55 this.params,
56 this.destId
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