godprotocol 1.1.2 → 1.1.31

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,29 +1,235 @@
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";
12
+
13
+ let res = await this.account.compiler.to_sequence(
14
+ JSON.stringify(result),
15
+ config.location.split("/").slice(2).join("/")
16
+ )[0].body[0];
17
+
18
+ await chain.add_config(res);
19
+ result = await this.execute(res, { chain });
20
+
21
+ return result;
22
+ };
23
+
24
+ resolve_address = async (path, chain) => {
25
+ let oracle = this.account.manager.oracle,
26
+ fs = oracle.fs;
27
+
28
+ if (typeof path !== "string") {
29
+ return path;
30
+ }
31
+
32
+ if (path == "g") throw new Error();
33
+ let conf = await fs.read(`${path}/.config`),
34
+ value;
35
+ if (!conf.current) {
36
+ value = await fs.read(`${path}/${conf.index - 1}`);
37
+ value = JSON.parse(value.content);
38
+ } else value = await fs.read(`${path}/${conf.current}`);
39
+ value.path = path;
40
+
41
+ return value;
42
+ };
43
+
44
+ instantiate = async (config, { chain, hash, pure }) => {
45
+ let instance_chain = await chain.chain(
46
+ config.type === "void" ? config.value : config.location
47
+ );
48
+ let uid = this.gen_uid();
49
+
50
+ if (config.type === "twain") {
51
+ if (!pure) {
52
+ for (let p in config.value) {
53
+ let pair = config.value[p];
54
+
55
+ pair[0] = (await this.execute(pair[0], { chain })).value;
56
+ pair[1] = (await this.execute(pair[1], { chain })).value;
57
+ }
58
+ }
59
+
60
+ config.value = JSON.stringify(config.value);
61
+ } else if (config.type === "array") {
62
+ if (!pure) {
63
+ for (let i = 0; i < config.value.length; i++) {
64
+ let item = config.value[i];
65
+ config.value[i] = (await this.execute(item, { chain })).value;
66
+ }
67
+ }
68
+ config.value = JSON.stringify(config.value);
69
+ }
70
+
71
+ let res = await instance_chain.write(
72
+ { content: config.value, __classifier__: config.type, uid },
73
+ { hash, is_instance: true }
74
+ );
75
+ return res;
76
+ };
77
+
78
+ operate_result = async (value, call_config, chain) => {
79
+ let result;
80
+ if (value && value.error) {
81
+ result = value;
82
+ } else if (value && value.type === "address") {
83
+ result = value;
84
+ } else
85
+ result = await this.parse_aircode(value, { config: call_config, chain });
9
86
 
10
- for (let a = 0; a < args.length; a++) {
11
- let arg = args[a];
87
+ return result;
88
+ };
89
+
90
+ object_identifier = async (call_config, chain) => {
91
+ let { identifier } = call_config,
92
+ result,
93
+ oracle = this.account.manager.oracle;
94
+ let conf = await oracle.fs.read(`${identifier.object}/.config`);
95
+ let obj = await oracle.fs.read(`${identifier.object}/${conf.current}`);
96
+
97
+ let res = await this.cloth_content({ ...obj, path: identifier.object });
98
+ let classifier = await res.get("__classifier__");
99
+
100
+ if (this.datatypes.includes(classifier)) {
101
+ for (let a = 0; a < call_config.arguments.length; a++)
102
+ call_config.arguments[a] = await this.execute(
103
+ call_config.arguments[a],
104
+ { chain, cloth: true }
105
+ );
106
+
107
+ let value = await res.operate(identifier.value, call_config.arguments, {
108
+ classifier,
109
+ config: call_config,
110
+ chain,
111
+ });
112
+ result = await this.operate_result(value, call_config, chain);
113
+ } else {
114
+ let conf = await oracle.fs.read(`${identifier.value}/.config`);
115
+ conf = await oracle.fs.read(`${identifier.value}/${conf.index - 1}`);
116
+ let content = JSON.parse(conf.content);
117
+
118
+ let val = await this.execute(content, {
119
+ chain,
120
+ call_config: {
121
+ ...call_config,
122
+ object: { type: "address", value: identifier.object },
123
+ },
124
+ });
125
+
126
+ result = val;
127
+ }
128
+
129
+ return result;
130
+ };
12
131
 
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);
132
+ execute_call = async (call_config, { chain, obj }) => {
133
+ let { identifier, arguments: args } = call_config,
134
+ result;
17
135
 
18
- if (!param) return await this.stderr({ name: "unidentified_argument" });
136
+ if (typeof identifier !== "string") {
137
+ if (identifier.object) {
138
+ result = await this.object_identifier(call_config, chain);
139
+ } else {
140
+ let conf = await this.read_config(identifier.value);
141
+ if (!["function", "class"].includes(conf.type)) {
142
+ conf = await obj.get(identifier.prev_name, {
143
+ obj: identifier.prev_name,
144
+ });
145
+ conf = await this.resolve_address(conf.value);
146
+ }
147
+ result = await this.execute(conf, { call_config, chain });
148
+ }
149
+ } else if (identifier.includes("/")) {
150
+ let oracle = this.account.manager.oracle;
151
+ let i_chain = identifier.startsWith(this.account.manager.path)
152
+ ? { path: identifier }
153
+ : await chain.chain(identifier);
154
+ let conf = await oracle.fs.read(`${i_chain.path}/.config`);
19
155
 
20
- content = await this.read_chain(content.value);
21
- } else param = config.parameters[a];
156
+ if (conf.sibling) {
157
+ let config = await oracle.fs.read(conf.sibling);
22
158
 
23
- aggs[param.name] = content;
159
+ if (config.content.type === "address") {
160
+ if (config.content.object) {
161
+ result = await this.object_identifier(
162
+ { ...call_config, identifier: config.content },
163
+ chain
164
+ );
165
+ } else {
166
+ let iconf = await oracle.fs.read(`${config.content.value}/.config`);
167
+ let configg = await oracle.fs.read(
168
+ `${config.content.value}/${iconf.index - 1}`
169
+ );
170
+
171
+ result = await this.execute(JSON.parse(configg.content), {
172
+ call_config,
173
+ chain,
174
+ });
175
+ }
176
+ }
177
+ } else if (conf.current) {
178
+ let iconf = await oracle.fs.read(`${conf.current}/.config`);
179
+ let config = await oracle.fs.read(`${conf.current}/${iconf.index - 1}`);
180
+
181
+ result = await this.execute(JSON.parse(config.content), {
182
+ call_config,
183
+ chain,
184
+ });
185
+ } else {
186
+ let config = await oracle.fs.read(
187
+ `${call_config.identifier}/${conf.index - 1}`
188
+ );
189
+ result = await this.execute(JSON.parse(config.content), {
190
+ call_config,
191
+ chain,
192
+ });
193
+ }
194
+ } else {
195
+ if (this.datatypes.includes(identifier)) {
196
+ } else {
197
+ let { callable, config } = await this.callable(identifier);
198
+ let data = {};
199
+
200
+ for (let a = 0; a < args.length; a++) {
201
+ let param;
202
+ let arg = args[a];
203
+ if (arg.identifier) {
204
+ } else {
205
+ param = config.parameters[arg.position];
206
+ if (!param) {
207
+ let lst_param = config.parameters.slice(-1)[0];
208
+ if (lst_param.spread) param = lst_param;
209
+ }
210
+ }
211
+ let res = await this.execute(arg, { chain, cloth: true });
212
+ if (param.spread) {
213
+ let arr = data[param.name];
214
+ if (!arr) {
215
+ arr = new Array();
216
+ data[param.name] = arr;
217
+ }
218
+ arr.push(res);
219
+ } else data[param.name] = res;
220
+ }
221
+
222
+ result = await callable(data, { vm: this, config, chain });
223
+ if (["Twain", "exit", "render"].includes(identifier)) {
224
+ } else
225
+ result = await this.parse_aircode(result, {
226
+ config: call_config,
227
+ chain,
228
+ });
229
+ }
24
230
  }
25
231
 
26
- return aggs;
232
+ return result;
27
233
  };
28
234
 
29
235
  callable = async (name) => {
@@ -35,6 +241,25 @@ class Callable {
35
241
  set = (name, config) => {
36
242
  this.objects[name] = config;
37
243
  };
244
+
245
+ native_components = async (config, chain) => {
246
+ let props = await this.execute(config.props, { chain, cloth: true });
247
+ let children = new Array();
248
+ for (let c = 0; c < config.children.length; c++) {
249
+ let child = config.children[c];
250
+
251
+ let res = await this.execute(child, { chain, cloth: true });
252
+ children.push(await res.literal());
253
+ }
254
+
255
+ let obj = {
256
+ name: config.name,
257
+ props: await props.literal(),
258
+ children,
259
+ };
260
+
261
+ return obj;
262
+ };
38
263
  }
39
264
 
40
265
  export default Callable;
package/Objects/Chain.js CHANGED
@@ -1,39 +1,208 @@
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;
27
+ }
28
+
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};
40
+
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
+ }
52
+
53
+ read =async (options={})=>{
54
+ let res = await this.retrieve(options)
55
+
56
+ return res;
57
+ }
58
+
59
+ write_config = async (config, options={})=>{
60
+ return await this.persist(config, {...options, config: true, })
9
61
  }
10
62
 
11
- sync = async () => {
12
- this.folder = await this.account.manager.oracle.folder(
13
- this.physical_address
14
- );
15
- };
63
+ add_config = async (config, options={})=>{
64
+ let result;
65
+
66
+ switch(config.type){
67
+ case 'module':
68
+ for (let b=0; b < config.body.length; b++){
69
+ let line = config.body[b]
70
+
71
+ config.body[b] = await this.add_config(line)
72
+ }
73
+ result = await this.write_config(config)
74
+ break;
75
+ case 'adm':
76
+ let adm_chain = await this.chain(config.location)
77
+ config.props = await this.add_config(config.props)
78
+ for (let c=0; c< config.children.length; c++){
79
+ config.children[c] = await this.add_config(config.children[c])
80
+ }
81
+ result = await adm_chain.write_config(config)
82
+ break;
83
+ case 'class':
84
+ let class_chain = await this.chain(config.name)
85
+ for (let name in config.methods){
86
+ config.methods[name] = await class_chain.add_config(config.methods[name])
87
+ }
16
88
 
17
- write = async (payload) => {
18
- let address = await this.account.manager.oracle.write(payload);
89
+ result = await class_chain.write_config(config)
90
+ break;
91
+ case 'function':
92
+ let fn_chain = await this.chain(config.name)
93
+ for (let p=0; p< config.parameters.length; p++){
94
+ config.parameters[p] = await fn_chain.add_config(config.parameters[p])
95
+ }
96
+ for (let b=0; b< config.body.length; b++){
19
97
 
20
- let block = new Block({ content: address }, this);
21
- await block.sync();
98
+ config.body[b] = await fn_chain.add_config(config.body[b])
99
+ }
100
+ result = await fn_chain.write_config(config)
101
+ break;
102
+ case 'return':
103
+ config.output = await this.add_config(config.output)
104
+ result = config
105
+ break;
106
+ case 'raise':
107
+ config.object = await this.add_config(config.object)
108
+ result = config
109
+ break;
110
+ case 'trycatch':
111
+ let try_chain = await this.chain(config.location);
112
+ for (let t=0;t< config.try_body.length; t++){
113
+ config.try_body[t] = await this.add_config(config.try_body[t])
114
+ }
115
+ for (let t=0;t< config.catch_body.length; t++){
116
+ config.catch_body[t] = await this.add_config(config.catch_body[t])
117
+ }
118
+ if (config.alias){
119
+ config.alias = await this.add_config({type:'assignment', identifier: config.alias.value, value: {type:'void', value:'void'}})
120
+ }
121
+ result = await try_chain.write_config(config)
122
+ break;
123
+ case 'condition':
124
+ let cond_chain = await this.chain(config.location)
125
+ for(let b= 0; b< config.blocks.length; b++){
126
+ let block= config.blocks[b]
127
+ block.predicate = await cond_chain.add_config(block.predicate)
128
+ for (let i=0; i< block.body.length; i++){
129
+ block.body[i] = await cond_chain.add_config(block.body[i])
130
+ }
131
+ }
132
+ result = await cond_chain.write_config(config)
133
+ break;
134
+ case 'assignment':
135
+ let ass_chain = await this.chain(config.identifier)
22
136
 
23
- this.blocks.push(block);
24
- };
137
+ config.value = await ass_chain.add_config(config.value);
25
138
 
26
- read = async (query) => {
27
- let { index } = query;
139
+ result = await ass_chain.write_config(config)
140
+ break;
141
+ case 'loop':
142
+ let loop_chain = await this.chain(config.location)
143
+ config.condition = await this.add_config(config.condition)
144
+ if(config.loop_type === 'for'){
145
+ config.iterator = await this.add_config(config.iterator)
146
+ config.update = await this.add_config(config.update)
147
+ }
28
148
 
29
- if (index != null) return await this.get_block(index);
149
+ for (let b=0; b< config.body.length; b++){
150
+ config.body[b] = await this.add_config(config.body[b])
151
+ }
152
+ result = await loop_chain.write_config(config)
153
+ break;
154
+ case 'call':
155
+ if(config.identifier.type === 'nest'){
156
+ let addr = await this.add_config(config.identifier)
157
+ config.identifier = {address: addr, type: 'nest'}
158
+ }
159
+ let call_chain = await this.chain(config.location)
160
+ for(let a=0; a< config.arguments.length; a++){
161
+ config.arguments[a] = await call_chain.add_config(config.arguments[a])
162
+ }
163
+ result = await call_chain.write_config(config)
164
+ break;
165
+ case 'nest':
166
+ let nest_chain = await this.chain(config.location)
167
+ for (let n=0; n< config.nests.length; n++){
168
+ config.nests[n] = await nest_chain.add_config(config.nests[n])
169
+ }
170
+ if(config.assignment)
171
+ config.assignment = await nest_chain.add_config(config.assignment)
30
172
 
31
- return await this.get_block();
32
- };
173
+ result = await nest_chain.write_config(config)
174
+ break;
175
+ default:
176
+ if (['number', 'string', 'boolean'].includes(config.type)){
177
+ result = await (await this.chain(config.location)).write_config(config)
178
+ }else if(['variable', 'address', 'reference'].includes(config.type)){
179
+ result = config;
180
+ }else if (config.type === 'void'){
181
+ result = config
182
+ }else if(config.type === 'twain'){
183
+ let c_chain = await this.chain(config.location)
184
+ for (let p in config.value){
185
+ let pair = config.value[p]
186
+ pair[0] = await this.add_config(pair[0])
187
+ pair[1] = await this.add_config(pair[1])
188
+ }
189
+ result = await c_chain.write_config(config)
190
+ } else if(config.type === 'array'){
191
+ let c_chain = await this.chain(config.location)
192
+ for (let i=0; i< config.value.length; i++){
193
+ let item = config.value[i]
194
+ config.value[i] = await this.add_config(item)
195
+ }
196
+ result = await c_chain.write_config(config)
197
+ }else if (['break', 'continue'].includes(config.type)){
198
+ result = config;
199
+ }
200
+
201
+ }
202
+
203
+ return result;
204
+ }
33
205
 
34
- get_block = async (index) => {
35
- return this.blocks[index == null ? this.blocks.length - 1 : index];
36
- };
37
206
  }
38
207
 
39
- export default Chain;
208
+ export default Folder;