godprotocol
Version:
A distributed computing environment for Web 4.0 — integrating AI, decentralisation, and virtual computation.
210 lines (168 loc) • 5.53 kB
JavaScript
import { generate_random_string } from "generalised-datastore/utils/functions.js";
import Storage from "godprotocol/Datatypes/functions/storage.js";
class Cls extends Storage{
constructor(config, account){
super(account)
this.account = account
this.config = config;
this.type = 'class'
this.lits = ['listen', 'emit']
this.methods = {
...this.methods,
'get_instance': {args: [['query', 'twain']], ret: ['object', 'instance | void']}
}
}
get_instance = async(query, is_obj)=>{
let account = this.account, res;
if (is_obj === true){
console.log(query)
res = query
} else {
let folder = await account.manager.ds.folder(`${this.config.address}/instances`, {account: account.physical_address})
res = await folder.readone(await query.literal())
}
// console.log(query, folder.address)
if(!res){
return
}
console.log(query)
let ponse = await (await account.manager.ds.folder(res.location)).write(res, {replace: true})
console.log(ponse)
for (let prop in res.properties){
let value = res.properties[prop]
let spli = account.vm._split_datapath(value)
let raw = await account.manager.ds.folder(spli[0], {account: account.physical_address})
raw = await raw.readone({_id: spli[1]})
if (!raw) continue
// console.log(raw)
if (raw.type === 'array'){
await (await account.vm.cloth_content(raw)).retrieve(raw.items)
} else if (raw.type === 'twain'){
await (await account.vm.cloth_content(raw)).retrieve(raw.entries)
} else {
await (await account.manager.ds.folder(raw.location)).write(raw, {replace: true})
}
}
return {
type: 'address',
_id: res._id,
value: res.location,
}
}
get_ = async(name)=>{
}
run_init = async ({ins, args, vm, thread})=>{
let init = this.config.methods.__init__
if (!init) return;
init = init.split('/')
let func = await vm.cloth({
type: 'function',
address: init.slice(0,-1).join('/'),
_id: init.slice(-1)[0]
})
await func.invoke([...args], {instance_:ins, thread})
}
handle_id = async(args)=>{
let _id, propaddr = this.config.class_.properties.__id_prop__, vm = this.account.vm
if (propaddr){
let spli = vm._split_datapath(propaddr)
let prop = await vm.cloth({
type: 'address',
value: spli[0],
_id: spli[1]
})
let lit = await prop.literal()
let arg = args.find(a=>a.identifier === lit)
let init = this.config.methods['__init__']
if (lit && init && !arg){
let spli = vm._split_datapath(init), indx;
init = await vm.cloth({address: spli[0], _id: spli[1], type: 'function'}, {just_content: true});
for (let p=0; p < init.parameters.length; p++){
let param = init.parameters[p].split('/')
let name = param.slice(-3)[0]
if(name === lit){
indx = p
break
}
}
arg = args[indx]
}
if (arg){
_id = await(await vm.cloth(arg)).literal()
_id = await vm.hash(_id)
}
}
if (!_id)
_id = generate_random_string(16, 'alnum')
return _id;
}
invoke = async(args, options)=>{
let {thread, call_config} = options
let _id = await this.handle_id(args);
let location = `${this.config.address}/instances`;
let folder = await this.account.manager.ds.folder(location)
let obj = {
type: 'instance',
name: this.config.name,
cls: this.config.address,
cls_id: this.config._id,
location,
_id,
properties: {},
methods: {},
inherited: []
}
let inherited = new Array(), lists = new Array(), benches = new Object(), {vm} = this.account;
await this.account.vm.base_classes(obj, this.config, {inherited, chain: this.account.chain, lists, benches})
await this.account.vm.spread_properties(obj, this.config, {lists, benches})
let init = this.config.methods.__init__
if (init){
let res = await folder.write(obj)
obj._id = res._id
obj.created = res.created
init = init.split('/')
let func = await vm.cloth({
type: 'function',
address: init.slice(0,-1).join('/'),
_id: init.slice(-1)[0]
})
await func.invoke([...args], {list: lists, instance_:{
type: 'address',
value: folder.address,
_id: obj._id
}})
}
let thread_id = await vm.account.manager.load(lists, vm.account, {
on_pointer: async (pointer, result)=>{
if (Object.values(benches).includes(pointer)){
for (let b in benches){
if (benches[b] === pointer){
obj.properties[b] = `${result.value}/${result._id}`
await folder.write(obj, {replace: true})
break
}
}
}
},
callback: async (result, thrd)=>{
let obj_addr = {
type:'address',
value: obj.location,
_id: obj._id
}
thread.results[thread.pointer-1] = obj_addr
},
spawn: thread._id,
pointer: thread.pointer
})
if (thread)
vm.envs[thread_id] = vm.envs[thread._id]
}
literal = (options={})=>{
return options.adm? this.config: `[${this.type} ${this.config.name}]`
}
sync = async()=>{
return this
}
}
export default Cls