godprotocol 1.1.32 → 1.2.1

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.
Files changed (71) hide show
  1. package/Datatypes/arr.js +559 -254
  2. package/Datatypes/bool.js +8 -5
  3. package/Datatypes/callable.js +1 -1
  4. package/Datatypes/cls.js +193 -24
  5. package/Datatypes/conf.js +2 -2
  6. package/Datatypes/err.js +25 -0
  7. package/Datatypes/func.js +89 -13
  8. package/Datatypes/functions/storage.js +572 -12
  9. package/Datatypes/functions/wallet.js +49 -0
  10. package/Datatypes/instance.js +87 -122
  11. package/Datatypes/num.js +192 -153
  12. package/Datatypes/str.js +188 -122
  13. package/Datatypes/twa.js +346 -71
  14. package/Datatypes/voi.js +9 -4
  15. package/Index.js +1 -1
  16. package/Instances/account.js +0 -0
  17. package/Instances/event.js +0 -0
  18. package/Instances/fold.js +103 -0
  19. package/Instances/folder_queries.js +46 -0
  20. package/Instances/response.js +31 -0
  21. package/Objects/Account.js +160 -110
  22. package/Objects/Blockweb.js +6 -11
  23. package/Objects/Callable.js +223 -188
  24. package/Objects/Chain.js +127 -142
  25. package/Objects/Datatypes.js +114 -588
  26. package/Objects/Manager.js +349 -39
  27. package/Objects/Oracle.js +287 -210
  28. package/Objects/Repository.js +12 -33
  29. package/Objects/Trinity.js +9 -7
  30. package/Objects/Utils.js +8 -8
  31. package/Objects/Virtual_machine.js +320 -320
  32. package/README.md +0 -0
  33. package/callables/functions/assert.js +10 -0
  34. package/callables/functions/display.js +2 -3
  35. package/callables/functions/env.js +5 -0
  36. package/callables/functions/event.js +14 -0
  37. package/callables/functions/exit.js +3 -2
  38. package/callables/functions/folder.js +15 -0
  39. package/callables/functions/get_uid.js +6 -0
  40. package/callables/functions/hash.js +7 -0
  41. package/callables/functions/import_.js +62 -0
  42. package/callables/functions/local.js +9 -0
  43. package/callables/functions/net.js +33 -18
  44. package/callables/functions/parse.js +35 -0
  45. package/callables/functions/query.js +12 -0
  46. package/callables/functions/render.js +9 -13
  47. package/callables/functions/servers.js +10 -0
  48. package/callables/functions/spawn.js +7 -0
  49. package/callables/functions/store.js +75 -0
  50. package/callables/functions/super_.js +24 -0
  51. package/callables/functions/typeof.js +32 -0
  52. package/callables/functions/wait.js +9 -0
  53. package/callables/register.js +193 -8
  54. package/html/create.js +110 -0
  55. package/html/index.js +114 -0
  56. package/html/load.js +165 -0
  57. package/html/parse.js +171 -0
  58. package/html/run.js +151 -0
  59. package/html/util.js +5 -0
  60. package/package.json +5 -3
  61. package/repos/fs.js +114 -0
  62. package/repos/github.js +73 -0
  63. package/repos/ram.js +47 -0
  64. package/utils/create_server.js +66 -26
  65. package/utils/hash.js +3 -2
  66. package/utils/ops.js +2 -0
  67. package/utils/oracle_handlers.js +7 -0
  68. package/utils/services.js +18 -51
  69. package/utils/socket_server.js +36 -0
  70. package/utils/vm_utils.js +241 -137
  71. package/callables/functions/assign.js +0 -6
package/Datatypes/arr.js CHANGED
@@ -1,284 +1,589 @@
1
- import Storage from "./functions/storage";
1
+ import Storage from "./functions/storage.js";
2
+
3
+ /*
4
+ An Array is constructed by a parent config which holds
5
+ properties such as length, and a value which is basically the
6
+ last item in the array.
7
+
8
+ The array stores its entries by using a parent address
9
+ with a subfolder of item index
10
+ so for a array address at /places/things
11
+ First entry would be at
12
+ > places/things/0
13
+
14
+ and second at
15
+ >places/things/1
16
+
17
+ The native routines of an array are index, insert and pop.
18
+ Which those 3 are all its super methods constructed.
19
+
20
+ Now lets explain the `pop` native method and interface.
21
+ It takes in the arr config, and the index.
22
+ The index should always be less than the arr.length
23
+ Unless its an empty array, because the index also would not be less than zero.
24
+
25
+ It would access the index value by subfoldering.
26
+ Then it would read the next
27
+ */
2
28
 
3
29
  class Arr extends Storage {
4
- constructor(payload, manager) {
5
- super(payload.config, manager);
30
+ constructor(config, account) {
31
+ super(account);
6
32
 
7
- this.instance_object = payload;
8
- this.value = payload.value
9
- this.type = 'array'
33
+ this.config = config;
34
+ this.type = "array";
35
+
36
+ this.methods = {
37
+ ...this.methods,
38
+ 'replace': {args: [['index', 'number'], ['item', 'object']], ret: ['empty', 'void']},
39
+ 'pop': {args: [['name', 'number']], ret: ['item', 'object']},
40
+ 'insert': {args: [['item', 'object'], ['index', 'number']], ret: ['arr.length', 'number']},
41
+ 'push': {args: [['item', 'object']], ret: ['arr.length', 'number']},
42
+
43
+ call: {args: [['handler', 'function'], ['callback', 'function']], ret: ['results', 'array']},
44
+ // 'map': {args: [['handler', 'function']], ret: ['items', 'array']},
45
+ // 'filter': {args: [['handler', 'function']], ret: ['pass_items', 'array']},
46
+ // 'find': {args: [['handler', 'function']], ret: ['item', 'object']},
47
+
48
+ 'clear': {args: [], ret: ['empty', 'array']},
49
+ 'len': {args: [], ret: ['length', 'number']},
50
+ 'extend': {args: [['other', 'array']], ret: ['concatenated', 'array']},
51
+ 'padstart': {args: [['filler', 'object'], ['length', 'number']], ret: ['items', 'array']},
52
+ 'padend': {args: [['filler', 'object'], ['length', 'number']], ret: ['items', 'array']},
53
+ 'index': {args: [['index', 'number']], ret: ['item', 'object']},
54
+ 'to_string': {args: [['glue|ascii', 'object|string']], ret: ['chars', 'string']},
55
+ 'is_empty': {args: [[]], ret: ['isit', 'boolean']},
56
+ 'reverse': {args: [[]], ret: ['items', 'array']},
57
+ 'join': {args: [['other', 'object']], ret: ['concatenated_items', 'string']},
58
+ 'slice': {args: [['start', 'number'], ['stop', 'number']], ret: ['sub_arr', 'array']},
59
+ 'splice': {args: [['start', 'number'], ['stop', 'number']], ret: ['deleted_items', 'array']},
60
+ 'includes': {args: [['item', 'object']], ret: ['yes', 'boolean']},
61
+ 'repeat': {args: [['count', 'number']], ret: ['items', 'array']}
62
+ }
10
63
  }
11
64
 
12
- call = async (name, args, options={})=>{
13
- let {config, chain} = options;
14
- let result, other, me = await this.statics_()
65
+ swap_ = async (item, position) => {
66
+ let chain = await this.chain();
67
+ await chain.pop({ arr: this.config, index: position });
68
+ await chain.insert({ arr: this.config, item, index: position });
69
+ };
70
+
71
+ replace = async (index, args) => {
72
+ index = await index.literal();
73
+ if (index >= this.config.length) return;
15
74
 
16
- other = args[0] && await args[0].statics_()
75
+ let item = args[1];
76
+ item = await item.to_address();
77
+ await this.swap_(item, index);
78
+
79
+ return index >=0 && index < this.config.length
80
+ };
81
+
82
+ retrieve =async (items)=>{
83
+ let ds = this.account.manager.ds
84
+
85
+ delete this.config.items
86
+ await (await ds.folder(this.config.location)).write(this.config, {replace: true})
17
87
 
18
- let spli = this.value.value.split('/');
19
- let config_hash = spli.slice(-1)[0], store;
88
+ for (let i=0; i< this.config.length; i++){
89
+ let addr = `${this.config.location}/${i}`;
90
+ let item_ref = this.account.vm._split_datapath(items[i])
20
91
 
21
- switch(name){
22
- case 'push':
23
- result = me.push( await this.account.vm.instance_address(other))
24
- store = true;
92
+ let item_folder = await ds.folder(item_ref[0], {account: this.account.physical_address})
93
+ let item = await item_folder.readone({_id: item_ref[1]})
25
94
 
26
- break;
27
- case 'length':
28
- result = me.length;
29
- break;
30
- case 'pop':
31
- if (other){
32
- result = me.splice(other, 1)[0]
33
- }else result = me.pop()
34
-
35
- store= true
36
-
37
- if(result){
38
- result = {type:'address',value: result}
39
- }
40
- break
41
- case 'extend':
42
- result = me.push(...other)
43
- store = true
44
-
45
- break;
46
- case 'slice':
47
- let start = args[0] && await args[0].literal()
48
- let end = args[1] && await args[1].literal()
95
+ if(!item && item_ref[0].endsWith('/instances')){
96
+ let adr = item_ref[0].split('/').slice(0,-1).join('/')
97
+ item = await (await ds.folder(adr, {account: this.account.physical_address})).readone({_id: item_ref[1]})
98
+ }
99
+
100
+ await (await ds.folder(addr)).write({
101
+ type: 'address',
102
+ value: item.location,
103
+ _id: item._id
104
+ }, {replace: true})
49
105
 
50
- let new_arr = me.slice(start, end)
51
- result = new_arr
52
- break;
53
- case 'splice':
54
- let spl_replacement = args[2] && await args[2].statics_()
55
- if (spl_replacement){
56
- result = me.splice(args[0] && await args[0].literal(), args[1] && await args[1].literal(), await await this.account.vm.instance_address(spl_replacement) )
57
- }else result = me.splice(args[0] && await args[0].literal(), args[1] && await args[1].literal())
58
-
59
- store = true;
60
- break;
61
- case 'replace':
62
- me[await args[0].literal()] = await this.account.vm.instance_address(await args[1].statics_())
63
- store = true;
64
- break
65
- case 'index':
66
- let i = args[0] && await args[0].literal()
67
- if (i< 0){
68
- result = me.slice(i)[0]
69
- }else result = me[i]
70
- if (result)result = {type:'address', value: result}
71
- else result = null
72
- break;
73
- case 'copy':
74
- result = me;
75
- break;
76
- case 'deepcopy':
77
- break;
78
- case 'reverse':
79
- me = [...me].reverse()
80
- result = {type:'address', value: await this.account.vm.instance_address(this.instance_object)}
81
-
82
- if (other){
83
- store = true;
84
- }else {
85
- result = me
86
- }
87
- break;
88
- case "is_empty":
89
- result = !me.length
90
- break;
91
- case 'clear':
92
- me = []
93
- store = true;
94
- break;
95
- case 'join':
96
- let str_val = ''
97
- let separator = (other && await args[0].literal()) || ','
98
- for (let i=0; i< me.length; i++){
99
- let item = me[i]
100
- let val = await this.account.vm.cloth_object(item)
101
-
102
- if (['number', 'string', 'void', 'boolean'].includes(val.type))str_val = `${str_val}${i ? separator: ''}${await val.literal()}`
103
- }
104
- result = str_val
105
- break;
106
- case 'repeat':
107
- me = Array.from({ length: other? await args[0].literal(): 0 }, () => me).flat()
108
-
109
- store = true;
110
- result = me.length;
111
- break;
112
- case 'padstart':
113
- let p_length = args[0] && await args[0].literal()
114
- let value = args[1] && args[1]
115
- if (me.length >= p_length){ me = me;
116
- }else {
117
- let padding = Array(p_length - me.length).fill(['array', 'twain'].includes(value && value.type) ? value.value.address : value.instance_object.address);
118
- me = [...padding, ...me];
119
-
120
- store = true;
121
- }
122
- result = {type:'address', value: await this.account.vm.instance_address(this.instance_object)}
106
+ if (['array', 'twain'].includes(item.type)){
107
+ await (await this.account.vm.cloth_content(item)).retrieve(item.items || item.entries)
108
+ } else if (item.type === 'instance'){
109
+ await this.account.vm.retrieve(item)
110
+ } else {
111
+ await (await ds.folder(item.location)).write(item, {replace: true})
112
+ }
113
+ }
114
+ }
123
115
 
124
- break;
125
- case 'padend':
126
- let ps_length = args[0] && await args[0].literal()
127
- let svalue = args[1] && args[1]
128
- if (me.length >= ps_length) {me = me;
129
- }else {
130
- let spadding = Array(ps_length - me.length).fill(['array', 'twain'].includes(svalue && svalue.type) ? svalue.value.address : svalue.instance_object.address);
131
- me = [...me, ...spadding];
132
-
133
- store = true;
134
- }
135
- result = {type:'address', value: await this.account.vm.instance_address(this.instance_object)}
136
-
137
- break;
138
- case 'to_string':
139
- let str = ''
140
- if(other){
141
- other = await args[0].literal()
142
- }
143
- for (let a=0; a< me.length; a++){
144
- let item = me[a]
145
- let val = await this.account.vm.cloth_object(item)
146
- if (other === 'ascii'){
147
- if (val.type!=='number'){
148
- str = `${str}void`
149
- }else {
150
- val = await val.literal()
151
- str = `${str}${String.fromCharCode(val)}`
152
- }
153
- }
154
- }
155
- result = str
156
- break;
157
- case 'map':
158
- result = new Array()
159
- for (let a=0; a< me.length; a++){
160
- let item = me[a]
161
- let res = await this.account.vm.execute({
162
- type: 'call',
163
- arguments: [
164
- {type:'address', value: item},
165
- {type: 'number', value: a, location: `${config.location}/${await this.account.manager.oracle.hash(a)}`}
166
- ],
167
- identifier: args[0].value.address,
168
- location: config.location,
169
- }, {chain})
116
+ store = async ({store, addresses, thread})=>{
117
+ let chain = await this.chain();
170
118
 
171
- result[a] = res.type === 'instance' ? res.address : res.value
172
- }
173
- break;
174
- case 'findindex':
175
- for (let a=0; a< me.length; a++){
176
- let item = me[a]
177
- let res = await this.account.vm.execute({
178
- type: 'call',
179
- arguments: [
180
- {type:'address', value: item},
181
- {type: 'number', value: a, location: `${config.location}/${await this.account.manager.oracle.hash(a)}`}
182
- ],
183
- identifier: args[0].value.address,
184
- location: config.location,
185
- }, {chain, cloth: true})
186
-
187
- if(await res.literal()){
188
- result = a
189
- break
190
- }
191
- }
192
- if (result == null)result = -1
193
- break;
194
- case 'find':
195
- for (let a=0; a< me.length; a++){
196
- let item = me[a]
197
- let res = await this.account.vm.execute({
198
- type: 'call',
199
- arguments: [
200
- {type:'address', value: item},
201
- {type: 'number', value: a, location: `${config.location}/${await this.account.manager.oracle.hash(a)}`}
202
- ],
203
- identifier: args[0].value.address,
204
- location: config.location,
205
- }, {chain, cloth: true})
206
-
207
- if(await res.literal()){
208
- result = {type:'address', value: res. type === 'instance'? res.address: res.value}
209
- break
210
- }
211
- }
119
+ let data = {...this.config, items:[]}
120
+
121
+ for (let i=0; i < this.config.length; i++){
122
+ let item = await chain.index({arr: this.config, i})
123
+
124
+ await store({object: await this.account.vm.cloth(item)}, {vm: this.account.vm, addresses, thread})
125
+
126
+ data.items.push(`${item.value}/${item._id}`)
127
+ }
128
+
129
+ return data;
130
+ }
131
+
132
+ find = async (func) => {
133
+ let chain = await this.chain(),
134
+ result = new Array();
135
+ for (let i = 0; i < this.config.length; i++) {
136
+ let item = await chain.index({ arr: this.config, i });
137
+ let res = await func.invoke(
138
+ [
139
+ item,
140
+ await this.account.vm.instantiate(
141
+ {
142
+ type: "number",
143
+ value: i.toString(),
144
+ location: this.spawn_address(),
145
+ },
146
+ { chain }
147
+ ),
148
+ ],
149
+ { chain, cloth: true }
150
+ );
151
+
152
+ let res_lit = await res.literal();
153
+
154
+ if (!!res_lit) {
155
+ result = i;
212
156
  break;
213
- case 'filter':
214
- result = []
215
- for (let a=0; a< me.length; a++){
216
- let item = me[a]
217
- let res = await this.account.vm.execute({
218
- type: 'call',
219
- arguments: [
220
- {type:'address', value: item},
221
- {type: 'number', value: a, location: `${config.location}/${await this.account.manager.oracle.hash(a)}`}
222
- ],
223
- identifier: args[0].value.address,
224
- location: config.location,
225
- }, {chain, cloth: true})
226
-
227
- if(await res.literal()){
228
- result.push(item)
229
- }
157
+ }
158
+ }
159
+
160
+ return result;
161
+ };
162
+
163
+ filter = async (func, pure) => {
164
+ let chain = await this.chain(),
165
+ result = new Array();
166
+ for (let i = 0; i < this.config.length; i++) {
167
+ let item = await chain.index({ arr: this.config, i });
168
+ let res = await func.invoke(
169
+ [
170
+ item,
171
+ await this.account.vm.instantiate(
172
+ {
173
+ type: "number",
174
+ value: i.toString(),
175
+ location: this.spawn_address(),
176
+ },
177
+ { chain }
178
+ ),
179
+ ],
180
+ { chain, cloth: true }
181
+ );
182
+
183
+ let res_lit = await res.literal();
184
+
185
+ if (res_lit) {
186
+ if (pure) {
187
+ result.push(i);
188
+ } else result.push(item);
189
+ }
190
+ }
191
+ if (pure) return result;
192
+
193
+ result = await this.account.vm.instantiate(
194
+ {
195
+ type: "array",
196
+ value: result,
197
+ location: this.spawn_address(),
198
+ },
199
+ { chain }
200
+ );
201
+
202
+ return result;
203
+ };
204
+
205
+ __or__ = async (other) => {
206
+ if (!this.config.length) {
207
+ return await other.to_address();
208
+ }
209
+ return await this.to_address();
210
+ };
211
+
212
+ __and__ = async (other) => {
213
+ if (!this.config.length) {
214
+ return await this.to_address();
215
+ }
216
+ return await other.to_address();
217
+ };
218
+
219
+ call_ = async (func, args) => {
220
+ let chain = await this.chain(),
221
+ result = new Array();
222
+
223
+ for (let i = 0; i < this.config.length; i++) {
224
+ let item = await chain.index({arr: this.config, i})
225
+ let index = await this.account.vm.instantiate({
226
+ type:'number',
227
+ value: i.toString(),
228
+ location: this.spawn_address()
229
+ }, {chain})
230
+
231
+ await func.invoke([item, index], {no_wait: true, callback: async (res)=>{
232
+ if (res.__interrupt__){
233
+ res = await this.account.vm.instantiate({
234
+ type:'error',
235
+ payload: res.payload,
236
+ location: this.spawn_address()
237
+ }, {chain})
230
238
  }
231
- break
232
- case 'includes':
233
- if(!['number', 'string', 'void', 'boolean'].includes(other.type)) result = false;
234
- else {
235
- let search = other? await args[0].literal(): null
236
- if (!other)result = false;
237
- else {
238
- for (let a=0; a< me.length; a++){
239
- let item = me[a]
240
- let obj = await this.account.vm.cloth_object(item)
241
-
242
- if(['array', 'twain'].includes(obj.type)){
243
- result = false
244
- }
245
- else if(await obj.literal() === search){
246
- result = true;
247
- break
248
- }else result = false
249
- }
250
- }
239
+ result[i] = res
240
+ if (result.filter(r=>!!r).length === this.config.length){
241
+ await args[1].invoke([
242
+ await this.account.vm.instantiate({
243
+ type: 'array',
244
+ value: result,
245
+ location: this.spawn_address()
246
+ }, {chain})
247
+ ], {no_wait: true})
251
248
  }
252
-
249
+ }})
253
250
  }
254
- if(store){
255
- await this.account.manager.oracle.write(spli.slice(0,-1).join('/'), {type: this.type, values: me}, {config_hash, config: true})
251
+
252
+ return result;
253
+ };
254
+
255
+ extend = async (other) => {
256
+ let other_chain = await other.chain();
257
+ let chain = await this.chain();
258
+ for (let i = 0; i < other.config.length; i++) {
259
+ let obj = await other_chain.index({ arr: other.config, i });
260
+ await chain.insert({
261
+ arr: this.config,
262
+ item: obj,
263
+ index: this.config.length,
264
+ });
256
265
  }
266
+ return this.config.length;
267
+ };
257
268
 
258
- return result
259
- }
269
+ len = () => {
270
+ return this.config.length;
271
+ };
260
272
 
261
- configs = (name)=>{
262
- let conf = {push: {
263
- parameters: [{name: 'item', position: 0}]
264
- }}
273
+ padstart = async (filler, args) => {
274
+ let index = args[1],
275
+ chain = await this.chain();
265
276
 
266
- return conf[name]
267
- }
277
+ while (this.config.length < await index.literal()) {
278
+ await chain.insert({
279
+ arr: this.config,
280
+ index: 0,
281
+ item: await filler.to_address(),
282
+ });
283
+ }
284
+
285
+ return this.config.length;
286
+ };
287
+
288
+ padend = async (filler, args) => {
289
+ let index = await args[1].literal()
290
+ let chain = await this.chain();
291
+
292
+ while (this.config.length < index) {
293
+ await chain.insert({
294
+ arr: this.config,
295
+ index: this.config.length,
296
+ item: await filler.to_address(),
297
+ });
298
+ }
299
+
300
+ return this.config.length;
301
+ };
302
+
303
+ to_string = async (other) => {
304
+ other = other && (await other.literal());
305
+ let length = this.config.length,
306
+ result = "",
307
+ chain = await this.chain();
308
+
309
+ for (let k = 0; k < length; k++) {
310
+ let item = await chain.index({ arr: this.config, i: k });
311
+ item = await this.account.vm.cloth(item);
312
+ item = await item.literal();
313
+ if (other === "ascii") {
314
+ result = `${result}${String.fromCharCode(item)}`;
315
+ } else {
316
+ result = k ? `${result},${item.toString()}` : item.toString();
317
+ }
318
+ }
268
319
 
269
- push = async({item})=>{
270
- if (!item)return null;
320
+ return result;
321
+ };
271
322
 
272
- let path = this.config.path;
323
+ copy = async () => {
324
+ let result = new Array(),
325
+ chain = await this.chain(),
326
+ length = this.config.length;
327
+
328
+ for (let i = 0; i < length; i++)
329
+ result.push(await chain.index({ arr: this.config, i }));
330
+
331
+ return await this.account.vm.instantiate(
332
+ {
333
+ type: "array",
334
+ value: result,
335
+ location: this.spawn_address(),
336
+ },
337
+ { chain }
338
+ );
339
+ };
340
+
341
+ reverse = async () => {
342
+ let chain = await this.chain(),
343
+ result = new Array(),
344
+ length = this.config.length,
345
+ i = 0;
346
+
347
+ while (i < this.config.length) {
348
+ let pop = await chain.pop({
349
+ arr: this.config,
350
+ index: i,
351
+ });
352
+ result.unshift(pop);
353
+ }
273
354
 
274
- this.value.push(item.to_static())
355
+ for (let i = 0; i < length; i++) {
356
+ await chain.insert({
357
+ arr: this.config,
358
+ item: result[i],
359
+ index: this.config.length,
360
+ });
361
+ }
362
+
363
+ return await this.to_address()
364
+ };
365
+
366
+ join = async (other) => {
367
+ other = other ? await other.literal() : "";
368
+ let chain = await this.chain(),
369
+ result;
370
+
371
+ for (let i = 0; i < this.config.length; i++) {
372
+ let item = await chain.index({ arr: this.config, i });
373
+ let obj = await this.account.vm.cloth(item);
374
+
375
+ result = result
376
+ ? `${result}${other}${await obj.literal()}`
377
+ : `${await obj.literal()}`;
378
+ }
379
+
380
+ return result;
381
+ };
382
+
383
+ slice = async (other, args) => {
384
+ let i = await other.literal();
385
+ let stop = args[1] ? await args[1].literal() : this.config.length;
386
+ let skip = args[2] ? await args[2].literal() : 1;
387
+
388
+ let res = new Array(),
389
+ chain = await this.chain();
390
+ while (i < stop) {
391
+ let obj = await chain.index({ arr: this.config, i });
392
+ if (!obj) break;
393
+
394
+ res.push(obj);
395
+ i += skip;
396
+ }
397
+
398
+ return await this.account.vm.instantiate(
399
+ {
400
+ value: res,
401
+ location: this.spawn_address(),
402
+ type: "array",
403
+ },
404
+ { chain }
405
+ );
406
+ };
407
+
408
+ splice = async (other, args) => {
409
+ let i = await other.literal();
410
+ let count = args[1] ? await args[1].literal() : this.config.length - i;
411
+
412
+ let replacement = args[2];
413
+
414
+ let res = new Array(),
415
+ chain = await this.chain();
416
+
417
+ if (i < 0) {
418
+ i = this.config.length + i;
419
+ }
420
+
421
+ while (this.config.length > i && count) {
422
+ let obj = await chain.pop({ arr: this.config, index: i });
423
+ if (!obj) break;
424
+ res.push(obj);
425
+ count--;
426
+ }
427
+
428
+ if (replacement) {
429
+ await chain.insert({
430
+ arr: this.config,
431
+ index: i,
432
+ item: await replacement.to_address(),
433
+ });
434
+ }
435
+
436
+ return await this.account.vm.instantiate(
437
+ {
438
+ value: res,
439
+ location: this.spawn_address(),
440
+ type: "array",
441
+ },
442
+ { chain }
443
+ );
444
+ };
275
445
 
276
- this.config.content = this.to_static()
446
+ is_empty = () => {
447
+ return !this.config.length;
448
+ };
277
449
 
278
- await this.manager.oracle.fs.write(path, JSON.stringify(this.config))
450
+ includes = async (other) => {
451
+ other = await other.literal();
452
+ let result = false,
453
+ chain = await this.chain();
454
+ for (let i = 0; i < this.config.length; i++) {
455
+ let item = await chain.index({ arr: this.config, i });
456
+ let obj = await this.account.vm.cloth(item);
457
+ if ((await obj.literal()) === other) {
458
+ result = true;
459
+ break;
460
+ }
461
+ }
462
+
463
+ return result;
464
+ };
465
+
466
+ repeat = async (other) => {
467
+ other = await other.literal();
468
+ let chain = await this.chain();
469
+ let length = this.config.length;
470
+ for (let i = 1; i < other; i++) {
471
+ for (let k = 0; k < length; k++) {
472
+ let item = await chain.index({ arr: this.config, i: k });
473
+ await chain.insert({
474
+ arr: this.config,
475
+ item,
476
+ index: this.config.length,
477
+ });
478
+ }
479
+ }
480
+
481
+ return this.config.length;
482
+ };
483
+
484
+ clear = async () => {
485
+ let chain = await this.chain();
486
+ while (this.config.length) {
487
+ await chain.pop({ arr: this.config, index: this.config.length - 1 });
488
+ }
489
+ };
490
+
491
+ index = async (other) => {
492
+ if (other == null) return;
493
+
494
+ let chain = await this.chain();
495
+ let i = other.literal ? await other.literal() : other;
279
496
 
280
- return this.value.length;
497
+ i = await this.normalise_index_(i)
498
+ if (i.__interrupt__) return i
499
+
500
+ return await chain.index({ arr: this.config, i });
501
+ };
502
+
503
+ normalise_index_ = async(i)=>{
504
+ if (i < 0) {
505
+ i = this.config.length + i;
506
+ }
507
+ if (i >= this.config.length || i < 0){
508
+ return await this.account.vm.throw_error({type:"IndexError", message: `Index value out of range 0-${this.config.length-1}`})
509
+ }
510
+ return i
281
511
  }
512
+ pop = async (other) => {
513
+ let chain = await this.chain();
514
+
515
+ if (other.type !== 'number'){
516
+ return await this.account.vm.throw_error({type:"TypeError", message: 'Array.pop method expects a numeric index'})
517
+ }
518
+ let index = await other.literal()
519
+
520
+ index = await this.normalise_index_(index)
521
+ if (index.__interrupt__) return index
522
+
523
+ let res = await chain.pop({ arr: this.config, index });
524
+
525
+ return res;
526
+ };
527
+
528
+ insert = async (other, args) => {
529
+ let item = await other.to_address();
530
+ if (other.object) item.object = other.object;
531
+
532
+ let chain = await this.chain();
533
+ let index = await args[1].literal();
534
+ if (index === -1) {
535
+ index = this.config.length - 1;
536
+ } else {
537
+ if (index < 0) {
538
+ index = this.config.length + index;
539
+ }
540
+ }
541
+ if (index >= this.config.length){
542
+ index = this.config.length
543
+ }
544
+ await chain.insert({ arr: this.config, item, index });
545
+
546
+ return this.config.length;
547
+ };
548
+
549
+ push = async (other) => {
550
+ let chain = await this.chain();
551
+ let item = await other.to_address();
552
+ if (other.object) item.object = other.object;
553
+
554
+ await chain.insert({ arr: this.config, item, index: this.config.length });
555
+
556
+ return this.config.length;
557
+ };
558
+
559
+ literal = async () => {
560
+ if (this.value) return this.value;
561
+
562
+ let value = new Array();
563
+
564
+ let folder = await this._folder();
565
+ for (let i = 0; i < this.config.length; i++) {
566
+ let item_folder = await folder.folder(i.toString());
567
+ let item = (await item_folder.readone());
568
+
569
+ let obj = await this.account.vm.cloth(item);
570
+
571
+ value.push(await obj.literal());
572
+ }
573
+
574
+ this.value = value;
575
+ return value;
576
+ };
577
+
578
+ configs = (name) => {
579
+ let conf = {
580
+ push: {
581
+ parameters: [{ name: "item", position: 0 }],
582
+ },
583
+ };
584
+
585
+ return conf[name];
586
+ };
282
587
  }
283
588
 
284
589
  export default Arr;