UNPKG

godprotocol

Version:
285 lines (249 loc) 8.73 kB
import Storage from "./functions/storage"; class Arr extends Storage { constructor(payload, manager) { super(payload.config, manager); this.instance_object = payload; this.value = payload.value this.type = 'array' } call = async (name, args, options={})=>{ let {config, chain} = options; let result, other, me = await this.statics_() other = args[0] && await args[0].statics_() let spli = this.value.value.split('/'); let config_hash = spli.slice(-1)[0], store; switch(name){ case 'push': result = me.push( await this.account.vm.instance_address(other)) store = true; break; case 'length': result = me.length; break; case 'pop': if (other){ result = me.splice(other, 1)[0] }else result = me.pop() store= true if(result){ result = {type:'address',value: result} } break case 'extend': result = me.push(...other) store = true break; case 'slice': let start = args[0] && await args[0].literal() let end = args[1] && await args[1].literal() let new_arr = me.slice(start, end) result = new_arr break; case 'splice': let spl_replacement = args[2] && await args[2].statics_() if (spl_replacement){ result = me.splice(args[0] && await args[0].literal(), args[1] && await args[1].literal(), await await this.account.vm.instance_address(spl_replacement) ) }else result = me.splice(args[0] && await args[0].literal(), args[1] && await args[1].literal()) store = true; break; case 'replace': me[await args[0].literal()] = await this.account.vm.instance_address(await args[1].statics_()) store = true; break case 'index': let i = args[0] && await args[0].literal() if (i< 0){ result = me.slice(i)[0] }else result = me[i] if (result)result = {type:'address', value: result} else result = null break; case 'copy': result = me; break; case 'deepcopy': break; case 'reverse': me = [...me].reverse() result = {type:'address', value: await this.account.vm.instance_address(this.instance_object)} if (other){ store = true; }else { result = me } break; case "is_empty": result = !me.length break; case 'clear': me = [] store = true; break; case 'join': let str_val = '' let separator = (other && await args[0].literal()) || ',' for (let i=0; i< me.length; i++){ let item = me[i] let val = await this.account.vm.cloth_object(item) if (['number', 'string', 'void', 'boolean'].includes(val.type))str_val = `${str_val}${i ? separator: ''}${await val.literal()}` } result = str_val break; case 'repeat': me = Array.from({ length: other? await args[0].literal(): 0 }, () => me).flat() store = true; result = me.length; break; case 'padstart': let p_length = args[0] && await args[0].literal() let value = args[1] && args[1] if (me.length >= p_length){ me = me; }else { let padding = Array(p_length - me.length).fill(['array', 'twain'].includes(value && value.type) ? value.value.address : value.instance_object.address); me = [...padding, ...me]; store = true; } result = {type:'address', value: await this.account.vm.instance_address(this.instance_object)} break; case 'padend': let ps_length = args[0] && await args[0].literal() let svalue = args[1] && args[1] if (me.length >= ps_length) {me = me; }else { let spadding = Array(ps_length - me.length).fill(['array', 'twain'].includes(svalue && svalue.type) ? svalue.value.address : svalue.instance_object.address); me = [...me, ...spadding]; store = true; } result = {type:'address', value: await this.account.vm.instance_address(this.instance_object)} break; case 'to_string': let str = '' if(other){ other = await args[0].literal() } for (let a=0; a< me.length; a++){ let item = me[a] let val = await this.account.vm.cloth_object(item) if (other === 'ascii'){ if (val.type!=='number'){ str = `${str}void` }else { val = await val.literal() str = `${str}${String.fromCharCode(val)}` } } } result = str break; case 'map': result = new Array() for (let a=0; a< me.length; a++){ let item = me[a] let res = await this.account.vm.execute({ type: 'call', arguments: [ {type:'address', value: item}, {type: 'number', value: a, location: `${config.location}/${await this.account.manager.oracle.hash(a)}`} ], identifier: args[0].value.address, location: config.location, }, {chain}) result[a] = res.type === 'instance' ? res.address : res.value } break; case 'findindex': for (let a=0; a< me.length; a++){ let item = me[a] let res = await this.account.vm.execute({ type: 'call', arguments: [ {type:'address', value: item}, {type: 'number', value: a, location: `${config.location}/${await this.account.manager.oracle.hash(a)}`} ], identifier: args[0].value.address, location: config.location, }, {chain, cloth: true}) if(await res.literal()){ result = a break } } if (result == null)result = -1 break; case 'find': for (let a=0; a< me.length; a++){ let item = me[a] let res = await this.account.vm.execute({ type: 'call', arguments: [ {type:'address', value: item}, {type: 'number', value: a, location: `${config.location}/${await this.account.manager.oracle.hash(a)}`} ], identifier: args[0].value.address, location: config.location, }, {chain, cloth: true}) if(await res.literal()){ result = {type:'address', value: res. type === 'instance'? res.address: res.value} break } } break; case 'filter': result = [] for (let a=0; a< me.length; a++){ let item = me[a] let res = await this.account.vm.execute({ type: 'call', arguments: [ {type:'address', value: item}, {type: 'number', value: a, location: `${config.location}/${await this.account.manager.oracle.hash(a)}`} ], identifier: args[0].value.address, location: config.location, }, {chain, cloth: true}) if(await res.literal()){ result.push(item) } } break case 'includes': if(!['number', 'string', 'void', 'boolean'].includes(other.type)) result = false; else { let search = other? await args[0].literal(): null if (!other)result = false; else { for (let a=0; a< me.length; a++){ let item = me[a] let obj = await this.account.vm.cloth_object(item) if(['array', 'twain'].includes(obj.type)){ result = false } else if(await obj.literal() === search){ result = true; break }else result = false } } } } if(store){ await this.account.manager.oracle.write(spli.slice(0,-1).join('/'), {type: this.type, values: me}, {config_hash, config: true}) } return result } configs = (name)=>{ let conf = {push: { parameters: [{name: 'item', position: 0}] }} return conf[name] } push = async({item})=>{ if (!item)return null; let path = this.config.path; this.value.push(item.to_static()) this.config.content = this.to_static() await this.manager.oracle.fs.write(path, JSON.stringify(this.config)) return this.value.length; } } export default Arr;