Files: 23e12cd33ecd2ef038fb71cc7b53d43a2dd19637 / systemObjects.js
1506 bytesRaw
1 | const cbor = require('borc') |
2 | |
3 | const TAGS = { |
4 | link: 42, |
5 | id: 43, |
6 | func: 43, |
7 | mod: 44 |
8 | } |
9 | |
10 | const 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 | |
19 | class FunctionRef { |
20 | constructor (privateFunc, identifier, params, id) { |
21 | this.private = privateFunc |
22 | this.identifier = identifier |
23 | this.destId = id |
24 | this.params = params |
25 | } |
26 | |
27 | encodeCBOR (gen) { |
28 | return gen.write(new cbor.Tagged(TAGS.func, [ |
29 | this.private, |
30 | this.identifier, |
31 | this.destId, |
32 | this.params |
33 | ])) |
34 | } |
35 | |
36 | set container (container) { |
37 | this._container = container |
38 | } |
39 | } |
40 | |
41 | class ModuleRef { |
42 | constructor (ex, id) { |
43 | this.exports = ex |
44 | this.id = id |
45 | } |
46 | |
47 | getFuncRef (name) { |
48 | return new FunctionRef(false, name, this.exports[name], this.id) |
49 | } |
50 | |
51 | encodeCBOR (gen) { |
52 | return gen.write(new cbor.Tagged(TAGS.mod, [this.exports, this.id])) |
53 | } |
54 | |
55 | static fromMetaJSON (json, id) { |
56 | const exports = {} |
57 | for (const ex in json.exports) { |
58 | const type = json.types[json.indexes[json.exports[ex].toString()]].params |
59 | exports[ex] = type |
60 | } |
61 | return new ModuleRef(exports, id) |
62 | } |
63 | |
64 | static deserialize (serialized) {} |
65 | } |
66 | |
67 | class ID { |
68 | constructor (id) { |
69 | this.id = id |
70 | } |
71 | |
72 | encodeCBOR (gen) { |
73 | return gen.write(cbor.Tagged(TAGS.id, this.id)) |
74 | } |
75 | } |
76 | |
77 | module.exports = { |
78 | ID, |
79 | FunctionRef, |
80 | ModuleRef, |
81 | DEFAULTS |
82 | } |
83 |
Built with git-ssb-web