godprotocol 1.2.25 → 1.2.26

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,249 +1,263 @@
1
- import Ram from "godprotocol/repos/ram.js";
2
1
  import Callable from "./Callable.js";
3
2
 
4
3
  class Virtual_machine extends Callable {
5
4
  constructor(account) {
6
5
  super();
7
6
 
8
- this.envs = new Object()
7
+ this.envs = new Object();
9
8
  this.account = account;
10
9
  }
11
10
 
12
- nodelist = async(config, options={}) => {
13
- let {call, list} = options
14
- list = list || new Array()
11
+ nodelist = async (config, options = {}) => {
12
+ let { call, list } = options;
13
+ list = list || new Array();
15
14
 
16
- const flatten = async (config, no_call) =>{
17
- if (typeof config === 'string'){
18
- config = await this.read_config(config)
15
+ const flatten = async (config, no_call) => {
16
+ if (typeof config === "string") {
17
+ config = await this.read_config(config);
19
18
  }
20
- switch(config.type){
21
- case 'module':
22
- for (let b=0; b< config.body.length; b++){
23
- let bdy = config.body[b]
24
- config.body[b] = await flatten(bdy)
19
+ switch (config.type) {
20
+ case "module":
21
+ for (let b = 0; b < config.body.length; b++) {
22
+ let bdy = config.body[b];
23
+ config.body[b] = await flatten(bdy);
25
24
  }
26
- list.push(config)
27
- return list
28
- break
29
- case 'function':
30
- if (call && !no_call){
31
- for (let p=0; p < config.parameters.length; p++){
32
- let param = config.parameters[p]
33
- param.location = `${config.address}/${param.identifier}`
34
- config.parameters[p] = await flatten(param)
25
+ list.push(config);
26
+ return list;
27
+ break;
28
+ case "function":
29
+ if (call && !no_call) {
30
+ for (let p = 0; p < config.parameters.length; p++) {
31
+ let param = config.parameters[p];
32
+ param.location = `${config.address}/${param.identifier}`;
33
+ config.parameters[p] = await flatten(param);
35
34
  }
36
- for (let b=0; b < config.body.length; b++){
37
- let bdy = config.body[b]
38
- await flatten(bdy, true)
35
+ for (let b = 0; b < config.body.length; b++) {
36
+ let bdy = config.body[b];
37
+ await flatten(bdy, true);
39
38
  }
40
- }else {
41
- list.push({type: 'function', _id: config._id, address: config.address, module: config.module})
39
+ } else {
40
+ list.push({
41
+ type: "function",
42
+ _id: config._id,
43
+ address: config.address,
44
+ module: config.module,
45
+ });
42
46
  }
43
-
44
- return list.length-1
45
- case 'class':
46
- if(call){
47
47
 
48
- }else {
49
- list.push({type: 'class', _id: config._id, address: config.address, module: config.module})
48
+ return list.length - 1;
49
+ case "class":
50
+ if (call) {
51
+ } else {
52
+ list.push({
53
+ type: "class",
54
+ _id: config._id,
55
+ address: config.address,
56
+ module: config.module,
57
+ });
50
58
  }
51
- return list.length-1
52
- case 'return':
53
- config.output = await flatten(config.output)
54
- list.push(config)
55
- return list.length - 1
56
- case 'raise':
57
- return await flatten(config.object)
58
- case 'assignment':
59
- let val = await flatten(config.value)
60
-
61
- config.value = val
62
- list.push(config)
63
- return list.length - 1
59
+ return list.length - 1;
60
+ case "return":
61
+ config.output = await flatten(config.output);
62
+ list.push(config);
63
+ return list.length - 1;
64
+ case "raise":
65
+ return await flatten(config.object);
66
+ case "assignment":
67
+ let val = await flatten(config.value);
68
+
69
+ config.value = val;
70
+ list.push(config);
71
+ return list.length - 1;
64
72
  break;
65
- case 'nest':
66
- for (let n=0; n< config.nests.length; n++){
67
- let nst = config.nests[n]
68
- let val = await flatten(nst)
69
- config.nests[n] = val
73
+ case "nest":
74
+ for (let n = 0; n < config.nests.length; n++) {
75
+ let nst = config.nests[n];
76
+ let val = await flatten(nst);
77
+ config.nests[n] = val;
70
78
  }
71
- if(config.assignment){
72
- config.assignment = await flatten(config.assignment)
79
+ if (config.assignment) {
80
+ config.assignment = await flatten(config.assignment);
73
81
  }
74
- list.push(config)
75
- return list.length-1
76
- case 'call':
77
- let id = await this.read_config(config.identifier)
78
- if (id && ['call', 'nest'].includes(id.type)){
79
- config.identifier = await flatten(id)
82
+ list.push(config);
83
+ return list.length - 1;
84
+ case "call":
85
+ let id = await this.read_config(config.identifier);
86
+ if (id && ["call", "nest"].includes(id.type)) {
87
+ config.identifier = await flatten(id);
80
88
  }
81
- for (let a=0; a< config.arguments.length; a++){
82
- let arg = config.arguments[a]
83
- config.arguments[a] = await flatten(arg)
89
+ for (let a = 0; a < config.arguments.length; a++) {
90
+ let arg = config.arguments[a];
91
+ config.arguments[a] = await flatten(arg);
84
92
  }
85
- list.push(config)
86
- return list.length-1
87
- case 'trycatch':
88
- let try_index = list.length
89
- let try_conf = {type:'try', alias: config.alias, location: config.location}
90
- list.push(try_conf)
91
- for(let i=0; i < config.try_body.length; i++){
92
- let bdy = config.try_body[i]
93
- let b = await flatten(bdy)
94
- config.try_body[i] = b
93
+ list.push(config);
94
+ return list.length - 1;
95
+ case "trycatch":
96
+ let try_index = list.length;
97
+ let try_conf = {
98
+ type: "try",
99
+ alias: config.alias,
100
+ location: config.location,
101
+ };
102
+ list.push(try_conf);
103
+ for (let i = 0; i < config.try_body.length; i++) {
104
+ let bdy = config.try_body[i];
105
+ let b = await flatten(bdy);
106
+ config.try_body[i] = b;
95
107
  }
96
- let catch_index = list.length
97
- try_conf.catch_index = catch_index
98
- list.push({type:'catch', objects: config.objects, try_index})
99
- for (let i=0; i< config.catch_body.length; i++){
100
- let bdy = config.catch_body[i]
101
- config.catch_body[i] = await flatten(bdy)
108
+ let catch_index = list.length;
109
+ try_conf.catch_index = catch_index;
110
+ list.push({ type: "catch", objects: config.objects, try_index });
111
+ for (let i = 0; i < config.catch_body.length; i++) {
112
+ let bdy = config.catch_body[i];
113
+ config.catch_body[i] = await flatten(bdy);
102
114
  }
103
115
  try_conf.catch_offset = -1 + list.length - catch_index;
104
- return try_index
105
- case 'variable':
106
- list.push(config)
107
- return list.length-1
108
- case 'reference':
109
- list.push(config)
110
- return list.length -1
111
- case 'address':
112
- list.push(config)
113
- return list.length -1
114
- case 'loop':
115
- let loop_config = {type: 'loop', kind: config.loop_type || 'while'}
116
- let loop_index = list.length
117
- list.push(loop_config)
118
- if (config.loop_type === 'for')
119
- await flatten(config.iterator)
120
- loop_config.condition_index = list.length
121
- let cond_index = await flatten(config.condition)
122
-
123
- for (let b=0; b< config.body.length; b++){
124
- let bdy = config.body[b]
125
- await flatten(bdy)
116
+ return try_index;
117
+ case "variable":
118
+ list.push(config);
119
+ return list.length - 1;
120
+ case "reference":
121
+ list.push(config);
122
+ return list.length - 1;
123
+ case "address":
124
+ list.push(config);
125
+ return list.length - 1;
126
+ case "loop":
127
+ let loop_config = { type: "loop", kind: config.loop_type || "while" };
128
+ let loop_index = list.length;
129
+ list.push(loop_config);
130
+ if (config.loop_type === "for") await flatten(config.iterator);
131
+ loop_config.condition_index = list.length;
132
+ let cond_index = await flatten(config.condition);
133
+
134
+ for (let b = 0; b < config.body.length; b++) {
135
+ let bdy = config.body[b];
136
+ await flatten(bdy);
126
137
  }
127
- if (loop_config.kind === 'while'){
128
- loop_config.update_jmp = loop_config.condition_index
129
- }else loop_config.update_jmp = list.length
130
- if (config.loop_type === 'for'){
131
- await flatten(config.update)
138
+ if (loop_config.kind === "while") {
139
+ loop_config.update_jmp = loop_config.condition_index;
140
+ } else loop_config.update_jmp = list.length;
141
+ if (config.loop_type === "for") {
142
+ await flatten(config.update);
132
143
  }
133
- list.push({jmp: loop_config.condition_index})
134
- list[cond_index].conditional_jmp = list.length
135
- list[cond_index].invert = true
144
+ list.push({ jmp: loop_config.condition_index });
145
+ list[cond_index].conditional_jmp = list.length;
146
+ list[cond_index].invert = true;
136
147
  loop_config.conditional_jmp = list.length;
137
- list.push({type: 'endloop', kind: 'unloop'})
138
- return loop_index
139
- case 'condition':
140
- let prior_index, bdys = new Array(), indexx, prior_bdy;
141
- for (let b=0; b< config.blocks.length; b++){
148
+ list.push({ type: "endloop", kind: "unloop" });
149
+ return loop_index;
150
+ case "condition":
151
+ let prior_index,
152
+ bdys = new Array(),
153
+ indexx,
154
+ prior_bdy;
155
+ for (let b = 0; b < config.blocks.length; b++) {
142
156
  let blk = config.blocks[b];
143
157
 
144
- indexx = list.length
145
- let index = await flatten(blk.predicate)
146
- if (prior_index){
147
- list[prior_index].conditional_jmp = indexx
158
+ indexx = list.length;
159
+ let index = await flatten(blk.predicate);
160
+ if (prior_index) {
161
+ list[prior_index].conditional_jmp = indexx;
148
162
  }
149
- prior_index = index
163
+ prior_index = index;
150
164
 
151
165
  prior_bdy;
152
- for (let d = 0; d< blk.body.length; d++){
153
- let bdy = blk.body[d]
154
- prior_bdy = await flatten(bdy)
166
+ for (let d = 0; d < blk.body.length; d++) {
167
+ let bdy = blk.body[d];
168
+ prior_bdy = await flatten(bdy);
155
169
  }
156
170
 
157
- bdys.push(blk.body.length? prior_bdy: index)
158
- list.push({})
171
+ bdys.push(blk.body.length ? prior_bdy : index);
172
+ list.push({});
159
173
  }
160
- if (config.blocks.length === 1){
161
- list[prior_index].conditional_jmp = prior_bdy + 1
174
+ if (config.blocks.length === 1) {
175
+ list[prior_index].conditional_jmp = prior_bdy + 1;
162
176
  }
163
- bdys.map(dy=>{
164
- list[dy+1].jmp = list.length
165
- })
166
- return list.length - 1
167
- case 'twain':
168
- for (let key in config.value){
169
- let [prop, val] = config.value[key]
170
- prop = await flatten(prop)
171
- val = await flatten(val)
172
- config.value[key][0] = prop
173
- config.value[key][1] = val
177
+ bdys.map((dy) => {
178
+ list[dy + 1].jmp = list.length;
179
+ });
180
+ return list.length - 1;
181
+ case "twain":
182
+ for (let key in config.value) {
183
+ let [prop, val] = config.value[key];
184
+ prop = await flatten(prop);
185
+ val = await flatten(val);
186
+ config.value[key][0] = prop;
187
+ config.value[key][1] = val;
174
188
  }
175
- list.push(config)
176
- return list.length - 1
177
- case 'array':
178
- for (let i=0; i< config.value.length; i++){
179
- config.value[i] = await flatten(config.value[i])
189
+ list.push(config);
190
+ return list.length - 1;
191
+ case "array":
192
+ for (let i = 0; i < config.value.length; i++) {
193
+ config.value[i] = await flatten(config.value[i]);
180
194
  }
181
- list.push(config)
182
- return list.length - 1
195
+ list.push(config);
196
+ return list.length - 1;
183
197
  default:
184
- if (['number', 'string', 'void', 'boolean'].includes(config.type)){
185
- list.push(config)
186
- return list.length-1
187
- }else if (['continue', 'break'].includes(config.type)){
188
- list.push(config)
189
- return list.length-1
198
+ if (["number", "string", "void", "boolean"].includes(config.type)) {
199
+ list.push(config);
200
+ return list.length - 1;
201
+ } else if (["continue", "break"].includes(config.type)) {
202
+ list.push(config);
203
+ return list.length - 1;
190
204
  }
191
205
  }
192
- }
193
- let result = await flatten(config)
206
+ };
207
+ let result = await flatten(config);
194
208
 
195
- return list
196
- }
209
+ return list;
210
+ };
197
211
 
198
- read_config = async (addr, options={}) => {
212
+ read_config = async (addr, options = {}) => {
199
213
  if (typeof addr !== "string") {
200
214
  return addr;
201
215
  }
202
- addr = addr.split('/')
203
- let folder = await this.account.ds.folder(addr.slice(0,-1).join('/'))
204
- let result = await folder.readone({_id: addr.slice(-1)[0]})
205
-
216
+ addr = addr.split("/");
217
+ let folder = await this.account.ds.folder(addr.slice(0, -1).join("/"));
218
+ let result = await folder.readone({ _id: addr.slice(-1)[0] });
219
+
206
220
  return result;
207
221
  };
208
222
 
209
223
  sync = async () => {};
210
224
 
211
- throw_error = async(payload)=>{
225
+ throw_error = async (payload) => {
212
226
  return {
213
- __interrupt__: true,
227
+ __interrupt__: true,
214
228
  payload,
215
- }
216
- }
229
+ };
230
+ };
217
231
 
218
232
  execute_body = async (body, options) => {
219
233
  let res;
220
- for (let b=0; b < body.length; b++){
221
- let line = body[b]
234
+ for (let b = 0; b < body.length; b++) {
235
+ let line = body[b];
222
236
 
223
- res = await this.execute(line, options)
224
- if(res){
225
- if (res.done || res.__interrupt__) break
237
+ res = await this.execute(line, options);
238
+ if (res) {
239
+ if (res.done || res.__interrupt__) break;
226
240
  }
227
241
  }
228
242
  return res;
229
243
  };
230
244
 
231
- modules = new Array()
245
+ modules = new Array();
232
246
 
233
- times = new Array()
247
+ times = new Array();
234
248
 
235
- curr_field = ()=>{
236
- let curr = this.modules.slice(-1)[0]
237
- return curr && curr.address
238
- }
249
+ curr_field = () => {
250
+ let curr = this.modules.slice(-1)[0];
251
+ return curr && curr.address;
252
+ };
239
253
 
240
254
  spawns = new Array();
241
255
 
242
256
  execute = async (config, options = {}) => {
243
- let {chain, cloth, thread} = options
257
+ let { chain, cloth, thread } = options;
244
258
 
245
- config = await this.read_config(config, chain)
246
- this.running_thread = thread
259
+ config = await this.read_config(config, chain);
260
+ this.running_thread = thread;
247
261
  // console.log(config, 'EXE')
248
262
 
249
263
  let result;
@@ -251,126 +265,152 @@ class Virtual_machine extends Callable {
251
265
  if (!config) return config;
252
266
 
253
267
  switch (config.type) {
254
- case 'module':
255
- let mod_chain = await chain.chain(config.location)
268
+ case "module":
269
+ let mod_chain = await chain.chain(config.location);
256
270
  // result = await this.execute_body(config.body, {chain: mod_chain, config})
257
- result = config.body
271
+ result = config.body;
258
272
  break;
259
- case 'class':
260
- let cls_chn = await chain.chain(config.address)
261
- result = await (await cls_chn.folder()).write(config, {replace: true})
273
+ case "class":
274
+ let cls_chn = await chain.chain(config.address);
275
+ result = await (
276
+ await cls_chn.folder()
277
+ ).write(config, { replace: true });
262
278
  result = {
263
- type: 'function',
279
+ type: "function",
264
280
  address: config.address,
265
281
  value: config.address,
266
- _id: result._id
267
- }
268
- break
269
- case 'function':
270
- let fn_chn = await chain.chain(config.address)
271
- result = await (await fn_chn.folder()).write(config, {replace: true})
282
+ _id: result._id,
283
+ };
284
+ break;
285
+ case "function":
286
+ let fn_chn = await chain.chain(config.address);
287
+ result = await (await fn_chn.folder()).write(config, { replace: true });
272
288
  result = {
273
- type: 'function',
289
+ type: "function",
274
290
  address: config.address,
275
291
  value: config.address,
276
- _id: result._id
277
- }
278
- break
279
- case 'return':
292
+ _id: result._id,
293
+ };
294
+ break;
295
+ case "return":
280
296
  result = {
281
297
  __interrupt__: true,
282
- type: 'return',
283
- output: config.output
284
- }
298
+ type: "return",
299
+ output: config.output,
300
+ };
301
+ break;
302
+ case "loop":
303
+ break;
304
+ case "condition":
285
305
  break;
286
- case 'loop':
287
-
306
+ case "assignment":
307
+ let ass_chain = await chain.chain(config.location);
308
+
309
+ result = await ass_chain.write(config.value, { addr: true });
310
+
311
+ result = { ...config.value, identifier: config.identifier };
312
+
288
313
  break;
289
- case 'condition':
290
-
291
- break
292
- case 'assignment':
293
- let ass_chain = await chain.chain(config.location)
294
-
295
- result = await ass_chain.write(config.value, {addr: true})
296
-
297
- result = {...config.value, identifier: config.identifier}
298
-
299
- break
300
- case 'nest':
301
- let prev = await this.cloth(config.nests[0])
302
- result = config.nests[0]
303
- for (let i=1; i < config.nests.length - (config.assignment?1:0); i++){
304
- let nst = await this.read_config(config.nests[i]), obj;
305
-
306
- if(nst.type === 'call'){
307
- obj = await this.execute({
308
- type: "string",
309
- value: nst.identifier,
310
- location: `${nst.location}_`,
311
- }, { chain, cloth: true })
312
- obj = await prev.get(nst.identifier, {chain, obj, config: nst})
313
- if(obj.__interrupt__){
314
- result = obj
315
- break
316
- }else{
317
- result = await this.execute({
318
- ...nst, identifier: obj
319
- }, {chain})
320
- prev = await this.cloth(result)
314
+ case "nest":
315
+ let prev = await this.cloth(config.nests[0]);
316
+ result = config.nests[0];
317
+ for (
318
+ let i = 1;
319
+ i < config.nests.length - (config.assignment ? 1 : 0);
320
+ i++
321
+ ) {
322
+ let nst = await this.read_config(config.nests[i]),
323
+ obj;
324
+
325
+ if (nst.type === "call") {
326
+ obj = await this.execute(
327
+ {
328
+ type: "string",
329
+ value: nst.identifier,
330
+ location: `${nst.location}_`,
331
+ },
332
+ { chain, cloth: true }
333
+ );
334
+ obj = await prev.get(nst.identifier, { chain, obj, config: nst });
335
+ if (obj.__interrupt__) {
336
+ result = obj;
337
+ break;
338
+ } else {
339
+ result = await this.execute(
340
+ {
341
+ ...nst,
342
+ identifier: obj,
343
+ },
344
+ { chain }
345
+ );
346
+ prev = await this.cloth(result);
321
347
  }
322
- }else{
323
- obj = await this.cloth(nst)
324
- prev = await prev.get(await obj.literal(), {chain, obj, config: nst})
325
-
326
- if (prev.__interrupt__){
327
- result = prev
328
- break
329
- }else {
348
+ } else {
349
+ obj = await this.cloth(nst);
350
+ prev = await prev.get(await obj.literal(), {
351
+ chain,
352
+ obj,
353
+ config: nst,
354
+ });
355
+
356
+ if (prev.__interrupt__) {
330
357
  result = prev;
331
- prev = await this.cloth(prev)
358
+ break;
359
+ } else {
360
+ result = prev;
361
+ prev = await this.cloth(prev);
332
362
  }
333
363
  }
334
364
  }
335
365
 
336
- if (config.assignment){
337
- let bull = thread.bullets[thread.pointer]
338
- if (result && result.__interrupt__){
339
- }else {
340
- let last = await this.cloth(config.nests.slice(-1)[0], {just_content: true})
366
+ if (config.assignment) {
367
+ let bull = thread.bullets[thread.pointer];
368
+ if (result && result.__interrupt__) {
369
+ } else {
370
+ let last = await this.cloth(config.nests.slice(-1)[0], {
371
+ just_content: true,
372
+ });
341
373
  if (this.datatypes.includes(last.type)) {
342
- let r = await prev.set(config.nests.slice(-1)[0], config.assignment, {chain})
343
- bull.assignment_location = r
344
- result = config.assignment
374
+ let r = await prev.set(
375
+ config.nests.slice(-1)[0],
376
+ config.assignment,
377
+ { chain }
378
+ );
379
+ bull.assignment_location = r;
380
+ result = config.assignment;
345
381
  }
346
382
  }
347
383
  }
348
- break
349
- case 'call':
350
- result = await this.execute_call(config, {chain, thread})
384
+ break;
385
+ case "call":
386
+ result = await this.execute_call(config, { chain, thread });
351
387
  break;
352
388
  default:
353
- if (this.datatypes.includes(config.type)){
354
- result = await this.instantiate(config, {chain})
355
- } else if (config.type === 'variable' || config.type === 'reference'){
356
- if (!!await this.callable(config.value) || this.datatypes.includes(config.value)){
389
+ if (this.datatypes.includes(config.type)) {
390
+ result = await this.instantiate(config, { chain });
391
+ } else if (config.type === "variable" || config.type === "reference") {
392
+ if (
393
+ !!(await this.callable(config.value)) ||
394
+ this.datatypes.includes(config.value)
395
+ ) {
357
396
  result = {
358
- type: 'address',
397
+ type: "address",
359
398
  value: config.value,
360
399
  _id: config.value,
361
400
  config: true,
362
- }
363
- } else result = await this.variable(config.location, {chain, config})
364
- } else if(["continue", "break"].includes(config.type)) {
401
+ };
402
+ } else
403
+ result = await this.variable(config.location, { chain, config });
404
+ } else if (["continue", "break"].includes(config.type)) {
365
405
  config.__interrupt__ = config.type;
366
406
  result = config;
367
407
  }
368
408
  }
369
409
 
370
- if(result && cloth && !result.__interrupt__){
371
- result = await this.cloth(result)
372
- }else if (result && config.conditional_jmp){
373
- result = await this.cloth(result)
410
+ if (result && cloth && !result.__interrupt__) {
411
+ result = await this.cloth(result);
412
+ } else if (result && config.conditional_jmp) {
413
+ result = await this.cloth(result);
374
414
  }
375
415
 
376
416
  return result;