git ssb

0+

wanderer🌟 / js-primea-objects



Commit bbffc1cd4f29e05f96fa2d1d15e71595b17bfc7f

use class methods for json stuff

Norton Wang committed on 4/27/2018, 4:33:23 PM
Parent: 5418f16a7749446c341174100524c5ddcec4a6a9

Files changed

index.jschanged
tests/index.jschanged
utils.jschanged
index.jsView
@@ -46,8 +46,17 @@
4646 toString () {
4747 return this.id.toString('hex')
4848 }
4949
50 + toJSON () {
51 + return `0x${this.toString()}`
52 + }
53 +
54 + static fromJSON (arg) {
55 + const { fromHex } = require('./utils')
56 + return new ID(fromHex(arg))
57 + }
58 +
5059 encodeCBOR (gen) {
5160 return gen.write(new cbor.Tagged(TAGS.id, this.id))
5261 }
5362 }
@@ -77,8 +86,33 @@
7786 this.gas
7887 ]))
7988 }
8089
90 + toJSON (includeParams = false) {
91 + const json = {
92 + '@FunctionRef': {
93 + actorID: this.actorID.toJSON(),
94 + private: this.identifier[0],
95 + name: this.identifier[1],
96 + gas: this.gas
97 + }
98 + }
99 + if (includeParams) {
100 + json['@FunctionRef'].params = this.params
101 + }
102 + return json
103 + }
104 +
105 + static fromJSON (arg) {
106 + const data = arg['@FunctionRef']
107 + return new FunctionRef({
108 + identifier: [data.private, data.name],
109 + actorID: ID.fromJSON(data.actorID),
110 + params: data.params,
111 + gas: data.gas
112 + })
113 + }
114 +
81115 /**
82116 * Creates a copy of the funcRef
83117 * @returns {FunctionRef}
84118 */
@@ -119,8 +153,25 @@
119153 actorID: this.id
120154 })
121155 }
122156
157 + toJSON (includeExports = false) {
158 + const json = {
159 + '@ModuleRef': {
160 + id: this.id.toJSON()
161 + }
162 + }
163 + if (includeExports) {
164 + json['@ModuleRef'].exports = this.exports
165 + }
166 + return json
167 + }
168 +
169 + static fromJSON (arg) {
170 + const data = arg['@ModuleRef']
171 + return new ModuleRef(data.exports, ID.fromJSON(data.id))
172 + }
173 +
123174 encodeCBOR (gen) {
124175 return gen.write(new cbor.Tagged(TAGS.mod, [this.exports, this.id]))
125176 }
126177 }
tests/index.jsView
@@ -78,10 +78,13 @@
7878 }),
7979 Buffer.from([1, 2, 3, 4])
8080 ]
8181 const json = utils.toJSON(obj)
82- t.deepEquals(JSON.stringify(json), '[{"@ModuleRef":{"id":"0x01"}},{"@FunctionRef":{"id":"0x01","private":false,"name":"main","gas":100}},"0x01020304"]')
82 + t.deepEquals(JSON.stringify(json), '[{"@ModuleRef":{"id":"0x01"}},{"@FunctionRef":{"actorID":"0x01","private":false,"name":"main","gas":100}},"0x01020304"]')
8383
84 + const jsonFull = utils.toJSON(obj, true)
85 + t.deepEquals(JSON.stringify(jsonFull), '[{"@ModuleRef":{"id":"0x01","exports":{"name":["i32"]}}},{"@FunctionRef":{"actorID":"0x01","private":false,"name":"main","gas":100,"params":["i32"]}},"0x01020304"]')
86 +
8487 const newObj = [
8588 new objects.ModuleRef(undefined, id),
8689 new objects.FunctionRef({
8790 identifier: [false, 'main'],
@@ -91,8 +94,10 @@
9194 Buffer.from([1, 2, 3, 4])
9295 ]
9396 t.deepEquals(utils.fromJSON(json), newObj)
9497
98 + t.deepEquals(utils.fromJSON(jsonFull), obj)
99 +
95100 const hashedId01 = utils.actorRefToId([0, 1])
96101 t.deepEquals(hashedId01.id, Buffer.from('0ca311b75efd27e7daf6eec8b51b5c1fe33ff233', 'hex'))
97102
98103 t.end()
utils.jsView
@@ -1,37 +1,24 @@
1-const { ID, FunctionRef, ModuleRef, generateActorId, getType } = require('./')
1 +const { FunctionRef, ModuleRef, generateActorId, getType } = require('./')
22
33 const actorRefToId = ref => ref.reduce((parent, curr) => generateActorId({ parent, nonce: curr }), null)
44
55 const toHex = arg => Buffer.isBuffer(arg) ? `0x${arg.toString('hex')}` : arg
66
77 const fromHex = arg => typeof arg !== 'string' ? arg : Buffer.from(arg.slice(0, 2) === '0x' ? arg.slice(2) : arg, 'hex')
88
9-const toJSON = arg => {
9 +const toJSON = (arg, includeOptional = false) => {
1010 switch (getType(arg)) {
1111 case 'elem':
12- return arg.map(toJSON)
12 + return arg.map(a => toJSON(a, includeOptional))
1313 case 'id':
14- return toJSON(arg.id)
1514 case 'func':
16- return {
17- '@FunctionRef': {
18- id: toJSON(arg.actorID),
19- private: arg.identifier[0],
20- name: arg.identifier[1],
21- gas: arg.gas
22- }
23- }
2415 case 'mod':
25- return {
26- '@ModuleRef': {
27- id: toJSON(arg.id)
28- }
29- }
16 + return arg.toJSON(includeOptional)
3017 case 'link':
3118 return {
3219 '@Link': {
33- '/': toJSON(arg['/'])
20 + '/': toJSON(arg['/'], includeOptional)
3421 }
3522 }
3623 case 'data':
3724 default:
@@ -45,23 +32,11 @@
4532 return arg.map(fromJSON)
4633 }
4734
4835 if (arg['@FunctionRef']) {
49- const data = arg['@FunctionRef']
50- return new FunctionRef({
51- identifier: [data.private, data.name],
52- actorID: new ID(fromJSON(data.id)),
53- params: data.params,
54- gas: data.gas
55- })
36 + return FunctionRef.fromJSON(arg)
5637 } else if (arg['@ModuleRef']) {
57- const data = arg['@ModuleRef']
58- return new ModuleRef(data.exports, new ID(fromJSON(data.id)))
59- } else if (arg['@Link']) {
60- const data = arg['@Link']
61- return {
62- '/': fromJSON(data['/'])
63- }
38 + return ModuleRef.fromJSON(arg)
6439 }
6540 }
6641
6742 return fromHex(arg)

Built with git-ssb-web