Commit 762d1ed4087ea8ec034bd4a95090557cc577197e
Merge pull request #4 from dfinity/cbor-refs
switch borc to cbor and serialize/deserialize refswanderer authored on 3/11/2018, 11:33:40 PM
GitHub committed on 3/11/2018, 11:33:40 PM
Parent: 3293a87f5f8d4e902258609eb8f0c213b7c38192
Parent: eac3880a8c43e92cadb18f267ddef5ca6f8d8d31
Files changed
systemObjects.js | changed |
systemObjects.js | ||
---|---|---|
@@ -1,9 +1,9 @@ | ||
1 | 1 | const cbor = require('borc') |
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,12 +15,35 @@ | ||
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.finalize() | |
32 | + } | |
33 | + | |
34 | + static deserialize (serialized) { | |
35 | + return decoder.decodeFirst(serialized) | |
36 | + } | |
37 | +} | |
38 | + | |
39 | +class FunctionRef extends Serializable { | |
20 | 40 | constructor (privateFunc, identifier, params, id, gas=0) { |
41 | + super() | |
21 | 42 | this.private = privateFunc |
22 | 43 | this.identifier = identifier |
44 | + if (!(id instanceof ID)) | |
45 | + id = new ID(id) | |
23 | 46 | this.destId = id |
24 | 47 | this.params = params |
25 | 48 | this.gas = gas |
26 | 49 | } |
@@ -28,20 +51,21 @@ | ||
28 | 51 | encodeCBOR (gen) { |
29 | 52 | return gen.write(new cbor.Tagged(TAGS.func, [ |
30 | 53 | this.private, |
31 | 54 | this.identifier, |
32 | - this.destId, | |
33 | - this.params | |
55 | + this.params, | |
56 | + this.destId | |
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,19 +84,18 @@ | ||
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 = { |
Built with git-ssb-web