Files: 72a22f7318aa0ad186ba6c2b134f0d45a091a18d / systemObjects.js
1559 bytesRaw
1 | const cbor = require('borc') |
2 | |
3 | const TAGS = { |
4 | id: 41, |
5 | link: 42, |
6 | func: 43, |
7 | mod: 44 |
8 | } |
9 | |
10 | const 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: new cbor.Tagged(TAGS.link, null), |
16 | func: new cbor.Tagged(TAGS.func, 0) |
17 | } |
18 | |
19 | const decoder = new cbor.Decoder({ |
20 | tags: { |
21 | [TAGS.id]: val => new ID(val), |
22 | [TAGS.func]: val => new FunctionRef({ |
23 | identifier: val[0], |
24 | params: val[1], |
25 | id: val[2], |
26 | gas: val[3] |
27 | }), |
28 | [TAGS.mod]: val => new ModuleRef(...val), |
29 | [TAGS.link]: val => { |
30 | return { |
31 | '/': val |
32 | } |
33 | } |
34 | } |
35 | }) |
36 | |
37 | class FunctionRef { |
38 | constructor (opts) { |
39 | this.identifier = opts.identifier |
40 | this.destId = opts.id |
41 | this.params = opts.params |
42 | this.gas = opts.gas || 0 |
43 | } |
44 | |
45 | encodeCBOR (gen) { |
46 | return gen.write(new cbor.Tagged(TAGS.func, [ |
47 | this.identifier, |
48 | this.params, |
49 | this.destId, |
50 | this.gas |
51 | ])) |
52 | } |
53 | } |
54 | |
55 | class ModuleRef { |
56 | constructor (ex, id) { |
57 | this.exports = ex |
58 | this.id = id |
59 | } |
60 | |
61 | getFuncRef (name) { |
62 | return new FunctionRef({ |
63 | identifier: [false, name], |
64 | params: this.exports[name], |
65 | id: this.id |
66 | }) |
67 | } |
68 | |
69 | encodeCBOR (gen) { |
70 | return gen.write(new cbor.Tagged(TAGS.mod, [this.exports, this.id])) |
71 | } |
72 | } |
73 | |
74 | class ID { |
75 | constructor (id) { |
76 | this.id = id |
77 | } |
78 | |
79 | encodeCBOR (gen) { |
80 | return gen.write(new cbor.Tagged(TAGS.id, this.id)) |
81 | } |
82 | } |
83 | |
84 | module.exports = { |
85 | ID, |
86 | FunctionRef, |
87 | ModuleRef, |
88 | DEFAULTS, |
89 | decoder |
90 | } |
91 |
Built with git-ssb-web