git ssb

0+

wanderer🌟 / js-primea-objects



Commit 5418f16a7749446c341174100524c5ddcec4a6a9

add from/to json utils

Norton Wang committed on 4/27/2018, 2:08:46 AM
Parent: b851241a3490c147c24fd7ab85ee479612353ddb

Files changed

tests/index.jschanged
utils.jsadded
tests/index.jsView
@@ -1,8 +1,9 @@
11 const Buffer = require('safe-buffer').Buffer
22 const tape = require('tape')
33 const cbor = require('borc')
44 const objects = require('../')
5 +const utils = require('../utils')
56
67 tape('system objects', t => {
78 t.equals(objects.getType(), 'invalid')
89 t.equals(objects.getType(true), 'invalid')
@@ -63,4 +64,36 @@
6364 t.deepEquals(hashedId01.id, Buffer.from('0ca311b75efd27e7daf6eec8b51b5c1fe33ff233', 'hex'))
6465
6566 t.end()
6667 })
68 +
69 +tape('utils', t => {
70 + const id = new objects.ID(Buffer.from([0x1]))
71 + const obj = [
72 + new objects.ModuleRef({'name': ['i32']}, id),
73 + new objects.FunctionRef({
74 + identifier: [false, 'main'],
75 + actorID: id,
76 + params: ['i32'],
77 + gas: 100
78 + }),
79 + Buffer.from([1, 2, 3, 4])
80 + ]
81 + const json = utils.toJSON(obj)
82 + t.deepEquals(JSON.stringify(json), '[{"@ModuleRef":{"id":"0x01"}},{"@FunctionRef":{"id":"0x01","private":false,"name":"main","gas":100}},"0x01020304"]')
83 +
84 + const newObj = [
85 + new objects.ModuleRef(undefined, id),
86 + new objects.FunctionRef({
87 + identifier: [false, 'main'],
88 + actorID: id,
89 + gas: 100
90 + }),
91 + Buffer.from([1, 2, 3, 4])
92 + ]
93 + t.deepEquals(utils.fromJSON(json), newObj)
94 +
95 + const hashedId01 = utils.actorRefToId([0, 1])
96 + t.deepEquals(hashedId01.id, Buffer.from('0ca311b75efd27e7daf6eec8b51b5c1fe33ff233', 'hex'))
97 +
98 + t.end()
99 +})
utils.jsView
@@ -1,0 +1,76 @@
1 +const { ID, FunctionRef, ModuleRef, generateActorId, getType } = require('./')
2 +
3 +const actorRefToId = ref => ref.reduce((parent, curr) => generateActorId({ parent, nonce: curr }), null)
4 +
5 +const toHex = arg => Buffer.isBuffer(arg) ? `0x${arg.toString('hex')}` : arg
6 +
7 +const fromHex = arg => typeof arg !== 'string' ? arg : Buffer.from(arg.slice(0, 2) === '0x' ? arg.slice(2) : arg, 'hex')
8 +
9 +const toJSON = arg => {
10 + switch (getType(arg)) {
11 + case 'elem':
12 + return arg.map(toJSON)
13 + case 'id':
14 + return toJSON(arg.id)
15 + 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 + }
24 + case 'mod':
25 + return {
26 + '@ModuleRef': {
27 + id: toJSON(arg.id)
28 + }
29 + }
30 + case 'link':
31 + return {
32 + '@Link': {
33 + '/': toJSON(arg['/'])
34 + }
35 + }
36 + case 'data':
37 + default:
38 + return toHex(arg)
39 + }
40 +}
41 +
42 +const fromJSON = arg => {
43 + if (typeof arg === 'object') {
44 + if (Array.isArray(arg)) {
45 + return arg.map(fromJSON)
46 + }
47 +
48 + 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 + })
56 + } 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 + }
64 + }
65 + }
66 +
67 + return fromHex(arg)
68 +}
69 +
70 +module.exports = {
71 + actorRefToId,
72 + toJSON,
73 + fromJSON,
74 + toHex,
75 + fromHex
76 +}

Built with git-ssb-web