godprotocol 1.1.2 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,30 +1,190 @@
1
- class Callable {
1
+ import Vm_utils from "../utils/vm_utils";
2
+
3
+ class Callable extends Vm_utils {
2
4
  constructor() {
5
+ super();
6
+
3
7
  this.objects = new Object();
4
8
  }
5
9
 
6
- parse_arguments = async (payload, config) => {
7
- let { args } = payload;
8
- let aggs = new Object();
10
+ parse_aircode = async(result, {config, chain})=>{
11
+ if (result == null)result = 'void';
9
12
 
10
- for (let a = 0; a < args.length; a++) {
11
- let arg = args[a];
13
+ let res = await this.account.aircode.to_sequence(JSON.stringify(result), config.location.split('/').slice(2).join('/'))[0].body[0]
12
14
 
13
- let content = await this.read_chain(arg),
14
- param;
15
- if (content.type === "variable") {
16
- param = config.parameters.find((p) => p.name === content.identifier);
15
+ await chain.add_config(res)
16
+ result = await this.execute(res, {chain})
17
17
 
18
- if (!param) return await this.stderr({ name: "unidentified_argument" });
18
+ return result
19
+ }
19
20
 
20
- content = await this.read_chain(content.value);
21
- } else param = config.parameters[a];
21
+ resolve_address = async (path, chain)=>{
22
+ let oracle = this.account.manager.oracle,
23
+ fs = oracle.fs;
22
24
 
23
- aggs[param.name] = content;
25
+ if (typeof path !== 'string'){
26
+ return path
24
27
  }
25
28
 
26
- return aggs;
27
- };
29
+ if(path == 'g')throw new Error()
30
+ let conf = await fs.read(`${path}/.config`), value;
31
+ if (!conf.current){
32
+ value = await fs.read(`${path}/${conf.index-1}`)
33
+ value = JSON.parse(value.content)
34
+ }else value = await fs.read(`${path}/${conf.current}`)
35
+ value.path = path
36
+
37
+ return value;
38
+ }
39
+
40
+ instantiate = async(config, {chain, hash, pure})=>{
41
+ let instance_chain = await chain.chain(config.type === 'void'? config.value : config.location);
42
+ let uid = this.gen_uid()
43
+
44
+ if (config.type === 'twain'){
45
+ if (!pure){
46
+ for (let p in config.value){
47
+ let pair = config.value[p]
48
+
49
+ pair[0] = (await this.execute(pair[0], {chain})).value
50
+ pair[1] = (await this.execute(pair[1], {chain})).value
51
+ }
52
+ }
53
+
54
+ config.value = JSON.stringify(config.value)
55
+ }else if(config.type === 'array'){
56
+ if (!pure){
57
+ for (let i=0; i< config.value.length; i++){
58
+ let item = config.value[i]
59
+ config.value[i] = (await this.execute(item, {chain})).value
60
+ }
61
+ }
62
+ config.value = JSON.stringify(config.value)
63
+ }
64
+
65
+ let res = await instance_chain.write({content: config.value, __classifier__: config.type, uid}, {hash, is_instance: true})
66
+ return res;
67
+ }
68
+
69
+ operate_result = async (value, call_config, chain)=>{
70
+ let result;
71
+ if (value && value.error){
72
+ result = value
73
+ }else if(value && value.type === 'address'){
74
+ result = value
75
+ } else result = await this.parse_aircode(value, {config: call_config, chain})
76
+
77
+ return result
78
+ }
79
+
80
+ object_identifier = async (call_config, chain)=>{
81
+ let {identifier, }= call_config, result, oracle = this.account.manager.oracle;
82
+ let conf = await oracle.fs.read(`${identifier.object}/.config`)
83
+ let obj = await oracle.fs.read(`${identifier.object}/${conf.current}`);
84
+
85
+ let res = await this.cloth_content({...obj, path: identifier.object})
86
+ let classifier = await res.get('__classifier__')
87
+
88
+ if (this.datatypes.includes(classifier)){
89
+ for (let a=0; a < call_config.arguments.length; a++)
90
+ call_config.arguments[a] = await this.execute(call_config.arguments[a], {chain, cloth: true})
91
+
92
+ let value = await res.operate(identifier.value, call_config.arguments, {classifier, config: call_config, chain})
93
+ result = await this.operate_result(value, call_config, chain)
94
+ }else {
95
+ let conf = await oracle.fs.read(`${identifier.value}/.config`)
96
+ conf = await oracle.fs.read(`${identifier.value}/${conf.index-1}`)
97
+ let content = JSON.parse(conf.content)
98
+
99
+ let val = await this.execute(content, {chain, call_config: {...call_config, object: {type:'address', value: identifier.object}}})
100
+
101
+ result = val
102
+ }
103
+
104
+ return result
105
+ }
106
+
107
+ execute_call = async(call_config, {chain, obj})=>{
108
+ let {identifier, arguments: args} = call_config, result;
109
+
110
+ if(typeof identifier !== 'string'){
111
+ if (identifier.object){
112
+ result = await this.object_identifier(call_config, chain)
113
+ }else {
114
+ let conf = await this.read_config(identifier.value)
115
+ if(!['function', 'class'].includes(conf.type)){
116
+ conf = await obj.get(identifier.prev_name, {obj:identifier.prev_name})
117
+ conf = await this.resolve_address(conf.value)
118
+ }
119
+ result = await this.execute(conf, {call_config, chain})
120
+ }
121
+ } else if (identifier.includes('/')){
122
+ let oracle = this.account.manager.oracle;
123
+ let i_chain = identifier.startsWith(this.account.manager.path)? {path: identifier} : await chain.chain(identifier)
124
+ let conf = await oracle.fs.read(`${i_chain.path}/.config`)
125
+
126
+ if(conf.sibling){
127
+ let config = await oracle.fs.read(conf.sibling)
128
+
129
+ if (config.content.type === 'address'){
130
+ if (config.content.object){
131
+ result = await this.object_identifier({...call_config, identifier: config.content}, chain)
132
+ }else {
133
+ let iconf = await oracle.fs.read(`${config.content.value}/.config`);
134
+ let configg = await oracle.fs.read(`${config.content.value}/${iconf.index-1}`)
135
+
136
+ result = await this.execute(JSON.parse(configg.content), {call_config, chain})
137
+ }
138
+ }
139
+ }else if(conf.current) {
140
+ let iconf = await oracle.fs.read(`${conf.current}/.config`);
141
+ let config = await oracle.fs.read(`${conf.current}/${iconf.index-1}`)
142
+
143
+ result = await this.execute(JSON.parse(config.content), {call_config, chain})
144
+ }else {
145
+ let config = await oracle.fs.read(`${call_config.identifier}/${conf.index-1}`)
146
+ result = await this.execute(JSON.parse(config.content), {call_config, chain})
147
+ }
148
+ }
149
+ else {
150
+ if (this.datatypes.includes(identifier)){}
151
+ else {
152
+ let {callable, config} = await this.callable(identifier)
153
+ let data = {}
154
+
155
+ for (let a=0; a< args.length; a++){
156
+ let param;
157
+ let arg = args[a]
158
+ if(arg.identifier){
159
+
160
+ }else {
161
+ param = config.parameters[arg.position]
162
+ if (!param){
163
+ let lst_param = config.parameters.slice(-1)[0]
164
+ if (lst_param.spread) param = lst_param;
165
+ }
166
+ }
167
+ let res = await this.execute(arg, {chain, cloth: true})
168
+ if (param.spread){
169
+ let arr = data[param.name]
170
+ if (!arr){
171
+ arr = new Array();
172
+ data[param.name] = arr;
173
+ }
174
+ arr.push(res)
175
+ }else data[param.name] = res;
176
+ }
177
+
178
+ result = await callable(data)
179
+ if (identifier === 'exit' || identifier === 'Twain'){
180
+
181
+ }else result = await this.parse_aircode(result, {config: call_config, chain})
182
+ }
183
+ }
184
+
185
+ return result
186
+ }
187
+
28
188
 
29
189
  callable = async (name) => {
30
190
  let object = this.objects[name];
package/Objects/Chain.js CHANGED
@@ -1,39 +1,200 @@
1
- import Block from "./Block";
2
-
3
- class Chain {
4
- constructor(address, account) {
5
- this.physical_address = address;
6
- this.account = account;
7
- this.blocks = new Array();
8
- this.servers = new Array();
1
+ import Storage from "godprotocol/Datatypes/functions/storage";
2
+
3
+ class Folder extends Storage{
4
+ constructor(address, parent) {
5
+ super(parent && parent.account.manager)
6
+
7
+ this.account = parent.account;
8
+ this.physical_address = address
9
+ this.path = `${this.account.manager.path}/${this.physical_address}`
10
+ this.parent = parent;
11
+ }
12
+
13
+ sync = async()=>{}
14
+
15
+ birth = async physical_address =>{
16
+ let folder = new Folder(physical_address, this);
17
+ await folder.sync()
18
+
19
+ return folder
20
+ }
21
+
22
+ chain = async(address)=>{
23
+ if (!address.includes('/'))address = `${this.physical_address}/${address}`
24
+
25
+ let chain = await this.account.manager.web.set(address, this)
26
+ return chain;
9
27
  }
10
28
 
11
- sync = async () => {
12
- this.folder = await this.account.manager.oracle.folder(
13
- this.physical_address
14
- );
15
- };
29
+ write = async (content, options)=>{
30
+ let res;
31
+ if (content && content.type === 'address' && options.hash) {
32
+ let oracle = this.account.manager.oracle;
33
+ let conf = await oracle.fs.read(`${this.path}/.config`) || {}
34
+
35
+ let pth = `${this.path}/${options.hash}`
36
+ let p_conf = await oracle.fs.read(`${pth}/.config`) || {}
37
+ let obj = {content, type: 'reference', previous: p_conf.current, sibling: conf.sibling}
38
+ let hsh = await oracle.hash(obj)
39
+ res = `${pth}/${hsh}`;
16
40
 
17
- write = async (payload) => {
18
- let address = await this.account.manager.oracle.write(payload);
41
+ await oracle.fs.write(res, JSON.stringify(obj))
42
+ p_conf.current = res
43
+ await oracle.fs.write(`${pth}/.config`, JSON.stringify(p_conf))
44
+
45
+ conf.sibling = res;
46
+ await oracle.fs.write(`${this.path}/.config`, JSON.stringify(conf))
47
+ }else {
48
+ res = await this.persist(content, {...options })
49
+ }
50
+ return res
51
+ }
19
52
 
20
- let block = new Block({ content: address }, this);
21
- await block.sync();
53
+ read =async (options={})=>{
54
+ let res = await this.retrieve(options)
55
+
56
+ return res;
57
+ }
22
58
 
23
- this.blocks.push(block);
24
- };
59
+ write_config = async (config, options={})=>{
60
+ return await this.persist(config, {...options, config: true, })
61
+ }
62
+
63
+ add_config = async (config, options={})=>{
64
+ let result;
25
65
 
26
- read = async (query) => {
27
- let { index } = query;
66
+ switch(config.type){
67
+ case 'module':
68
+ for (let b=0; b < config.body.length; b++){
69
+ let line = config.body[b]
28
70
 
29
- if (index != null) return await this.get_block(index);
71
+ config.body[b] = await this.add_config(line)
72
+ }
73
+ result = await this.write_config(config)
74
+ break;
75
+ case 'class':
76
+ let class_chain = await this.chain(config.name)
77
+ for (let name in config.methods){
78
+ config.methods[name] = await class_chain.add_config(config.methods[name])
79
+ }
30
80
 
31
- return await this.get_block();
32
- };
81
+ result = await class_chain.write_config(config)
82
+ break;
83
+ case 'function':
84
+ let fn_chain = await this.chain(config.name)
85
+ for (let p=0; p< config.parameters.length; p++){
86
+ config.parameters[p] = await fn_chain.add_config(config.parameters[p])
87
+ }
88
+ for (let b=0; b< config.body.length; b++){
89
+
90
+ config.body[b] = await fn_chain.add_config(config.body[b])
91
+ }
92
+ result = await fn_chain.write_config(config)
93
+ break;
94
+ case 'return':
95
+ config.output = await this.add_config(config.output)
96
+ result = config
97
+ break;
98
+ case 'raise':
99
+ config.object = await this.add_config(config.object)
100
+ result = config
101
+ break;
102
+ case 'trycatch':
103
+ let try_chain = await this.chain(config.location);
104
+ for (let t=0;t< config.try_body.length; t++){
105
+ config.try_body[t] = await this.add_config(config.try_body[t])
106
+ }
107
+ for (let t=0;t< config.catch_body.length; t++){
108
+ config.catch_body[t] = await this.add_config(config.catch_body[t])
109
+ }
110
+ if (config.alias){
111
+ config.alias = await this.add_config({type:'assignment', identifier: config.alias.value, value: {type:'void', value:'void'}})
112
+ }
113
+ result = await try_chain.write_config(config)
114
+ break;
115
+ case 'condition':
116
+ let cond_chain = await this.chain(config.location)
117
+ for(let b= 0; b< config.blocks.length; b++){
118
+ let block= config.blocks[b]
119
+ block.predicate = await cond_chain.add_config(block.predicate)
120
+ for (let i=0; i< block.body.length; i++){
121
+ block.body[i] = await cond_chain.add_config(block.body[i])
122
+ }
123
+ }
124
+ result = await cond_chain.write_config(config)
125
+ break;
126
+ case 'assignment':
127
+ let ass_chain = await this.chain(config.identifier)
128
+
129
+ config.value = await ass_chain.add_config(config.value);
130
+
131
+ result = await ass_chain.write_config(config)
132
+ break;
133
+ case 'loop':
134
+ let loop_chain = await this.chain(config.location)
135
+ config.condition = await this.add_config(config.condition)
136
+ if(config.loop_type === 'for'){
137
+ config.iterator = await this.add_config(config.iterator)
138
+ config.update = await this.add_config(config.update)
139
+ }
140
+
141
+ for (let b=0; b< config.body.length; b++){
142
+ config.body[b] = await this.add_config(config.body[b])
143
+ }
144
+ result = await loop_chain.write_config(config)
145
+ break;
146
+ case 'call':
147
+ if(config.identifier.type === 'nest'){
148
+ let addr = await this.add_config(config.identifier)
149
+ config.identifier = {address: addr, type: 'nest'}
150
+ }
151
+ let call_chain = await this.chain(config.location)
152
+ for(let a=0; a< config.arguments.length; a++){
153
+ config.arguments[a] = await call_chain.add_config(config.arguments[a])
154
+ }
155
+ result = await call_chain.write_config(config)
156
+ break;
157
+ case 'nest':
158
+ let nest_chain = await this.chain(config.location)
159
+ for (let n=0; n< config.nests.length; n++){
160
+ config.nests[n] = await nest_chain.add_config(config.nests[n])
161
+ }
162
+ if(config.assignment)
163
+ config.assignment = await nest_chain.add_config(config.assignment)
164
+
165
+ result = await nest_chain.write_config(config)
166
+ break;
167
+ default:
168
+ if (['number', 'string', 'boolean'].includes(config.type)){
169
+ result = await (await this.chain(config.location)).write_config(config)
170
+ }else if(['variable', 'address', 'reference'].includes(config.type)){
171
+ result = config;
172
+ }else if (config.type === 'void'){
173
+ result = config
174
+ }else if(config.type === 'twain'){
175
+ let c_chain = await this.chain(config.location)
176
+ for (let p in config.value){
177
+ let pair = config.value[p]
178
+ pair[0] = await this.add_config(pair[0])
179
+ pair[1] = await this.add_config(pair[1])
180
+ }
181
+ result = await c_chain.write_config(config)
182
+ } else if(config.type === 'array'){
183
+ let c_chain = await this.chain(config.location)
184
+ for (let i=0; i< config.value.length; i++){
185
+ let item = config.value[i]
186
+ config.value[i] = await this.add_config(item)
187
+ }
188
+ result = await c_chain.write_config(config)
189
+ }else if (['break', 'continue'].includes(config.type)){
190
+ result = config;
191
+ }
192
+
193
+ }
194
+
195
+ return result;
196
+ }
33
197
 
34
- get_block = async (index) => {
35
- return this.blocks[index == null ? this.blocks.length - 1 : index];
36
- };
37
198
  }
38
199
 
39
- export default Chain;
200
+ export default Folder;