systemObjects.jsView |
---|
1 | | -const cbor = require('borc') |
| 1 | +const cbor = require('cbor') |
2 | 2 | |
3 | 3 | const TAGS = { |
| 4 | + id: 41, |
4 | 5 | link: 42, |
5 | | - id: 43, |
6 | 6 | func: 43, |
7 | 7 | mod: 44 |
8 | 8 | } |
9 | 9 | |
15 | 15 | link: {'/': null}, |
16 | 16 | func: new cbor.Tagged(TAGS.func, 0) |
17 | 17 | } |
18 | 18 | |
19 | | -class FunctionRef { |
| 19 | +const 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 | + |
| 27 | +class 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 | + |
| 40 | +class FunctionRef extends Serializable { |
20 | 41 | constructor (privateFunc, identifier, params, id, gas=0) { |
| 42 | + super() |
21 | 43 | this.private = privateFunc |
22 | 44 | this.identifier = identifier |
23 | 45 | this.destId = id |
24 | 46 | this.params = params |
28 | 50 | encodeCBOR (gen) { |
29 | 51 | return gen.write(new cbor.Tagged(TAGS.func, [ |
30 | 52 | this.private, |
31 | 53 | this.identifier, |
| 54 | + this.params, |
32 | 55 | this.destId, |
33 | | - this.params |
| 56 | + this.gas |
34 | 57 | ])) |
35 | 58 | } |
36 | 59 | |
37 | 60 | set container (container) { |
38 | 61 | this._container = container |
39 | 62 | } |
40 | 63 | } |
41 | 64 | |
42 | | -class ModuleRef { |
| 65 | +class ModuleRef extends Serializable { |
43 | 66 | constructor (ex, id) { |
| 67 | + super() |
44 | 68 | this.exports = ex |
45 | 69 | this.id = id |
46 | 70 | } |
47 | 71 | |
60 | 84 | exports[ex] = type |
61 | 85 | } |
62 | 86 | return new ModuleRef(exports, id) |
63 | 87 | } |
64 | | - |
65 | | - static deserialize (serialized) {} |
66 | 88 | } |
67 | 89 | |
68 | | -class ID { |
| 90 | +class ID extends Serializable { |
69 | 91 | constructor (id) { |
| 92 | + super() |
70 | 93 | this.id = id |
71 | 94 | } |
72 | 95 | |
73 | 96 | encodeCBOR (gen) { |
74 | | - return gen.write(cbor.Tagged(TAGS.id, this.id)) |
| 97 | + return gen.write(new cbor.Tagged(TAGS.id, this.id)) |
75 | 98 | } |
76 | 99 | } |
77 | 100 | |
78 | 101 | module.exports = { |