git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit b3e32df171921ad8cbc02f8ac9c39e05f2d8f4ab

switch borc to cbor and serialize/deserialize refs

Norton Wang committed on 3/7/2018, 5:00:36 AM
Parent: 7023adc16f2c47b0399a242111adf25544a62185

Files changed

package.jsonchanged
systemObjects.jschanged
package.jsonView
@@ -30,9 +30,9 @@
3030 "contributors": "Alex Beregszaszi <alex@rtfs.hu>",
3131 "license": "MPL-2.0",
3232 "dependencies": {
3333 "binary-search-insert": "^1.0.3",
34- "borc": "^2.0.2",
34+ "cbor": "^4.0.0",
3535 "events": "^2.0.0",
3636 "reference-map": "^1.2.3",
3737 "safe-buffer": "^5.1.1",
3838 "wasm-json-toolkit": "^0.2.2",
systemObjects.jsView
@@ -1,9 +1,9 @@
1-const cbor = require('borc')
1+const cbor = require('cbor')
22
33 const TAGS = {
4+ id: 41,
45 link: 42,
5- id: 43,
66 func: 43,
77 mod: 44
88 }
99
@@ -15,10 +15,32 @@
1515 link: {'/': null},
1616 func: new cbor.Tagged(TAGS.func, 0)
1717 }
1818
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 {
2041 constructor (privateFunc, identifier, params, id, gas=0) {
42+ super()
2143 this.private = privateFunc
2244 this.identifier = identifier
2345 this.destId = id
2446 this.params = params
@@ -28,20 +50,22 @@
2850 encodeCBOR (gen) {
2951 return gen.write(new cbor.Tagged(TAGS.func, [
3052 this.private,
3153 this.identifier,
54+ this.params,
3255 this.destId,
33- this.params
56+ this.gas
3457 ]))
3558 }
3659
3760 set container (container) {
3861 this._container = container
3962 }
4063 }
4164
42-class ModuleRef {
65+class ModuleRef extends Serializable {
4366 constructor (ex, id) {
67+ super()
4468 this.exports = ex
4569 this.id = id
4670 }
4771
@@ -60,19 +84,18 @@
6084 exports[ex] = type
6185 }
6286 return new ModuleRef(exports, id)
6387 }
64-
65- static deserialize (serialized) {}
6688 }
6789
68-class ID {
90+class ID extends Serializable {
6991 constructor (id) {
92+ super()
7093 this.id = id
7194 }
7295
7396 encodeCBOR (gen) {
74- return gen.write(cbor.Tagged(TAGS.id, this.id))
97+ return gen.write(new cbor.Tagged(TAGS.id, this.id))
7598 }
7699 }
77100
78101 module.exports = {

Built with git-ssb-web