godprotocol
Version:
A distributed computing environment for Web 4.0 — integrating AI, decentralisation, and virtual computation.
120 lines (99 loc) • 2.82 kB
JavaScript
import Storage from "godprotocol/Datatypes/functions/storage.js";
class Instance extends Storage{
constructor(config, account){
super(account)
this.config = config;
this.type = 'instance'
this.lits = ['listen', 'emit']
}
get_cls_fold = async()=>{
let cls = await this.account.manager.gds.folder(this.config.classifier)
return cls;
}
run = async(call_config, options={})=>{
let {thread} = options;
let {identifier, arguments: args} = call_config;
let {value, _id} = identifier;
let func = await this.account.vm.cloth({
type: 'function',
address: value,
_id
})
await func.invoke([...args], {
thread,
instance_: {
type: 'address',
value: this.config.location,
_id: this.config._id
}
})
}
invoke = async(args, options)=>{
let cll = await this.get_('__call__')
let {vm} = this.account
if (!cll) return await vm.throw_error({type:"TypeError", message:`${this.config.name} Instance has no __call__ operator`})
let {chain} = options;
return await vm.execute_call({
type: 'call',
identifier: cll,
arguments: args,
location: vm.gen_location()
}, {chain})
}
literal = async (options={})=>{
let {raw, no_lit} = options
let chain = await this.chain(),
print = await this.get_('__print__', true), result;
if (print){
console.log(print, 'uh')
}else {
result = `[Instance ${this.config.name}]`
}
return result
}
set_ = async(prop, value)=>{
let {vm, manager} = this.account
let propobj = await vm.cloth(prop)
let valobj = await vm.cloth(value)
let prop_lit = await propobj.literal()
if (['function', 'class'].includes(valobj.type)){
this.config.methods[prop_lit] = valobj.ref()
delete this.config.properties[prop_lit]
}else {
this.config.properties[prop_lit] = valobj.ref()
delete this.config.methods[prop_lit]
}
await (await manager.ds.folder(this.config.location)).write(this.config, {replace: true})
}
get_ = async(prop, raw)=>{
let res = this.config.properties[prop], is_method;
if (!res){
res = this.config.methods[prop]
is_method = true
}
if (!res){
if (this.methods[prop]){
res = {
type: 'address',
value: prop,
_id: prop.repeat(2),
object: this.ref(),
}
return res;
} else return !raw && await this.account.vm.voided()
}else{
}
res = res.split('/')
res = {
type: 'address',
value: res.slice(0,-1).join('/'),
_id: res.slice(-1)[0],
}
if (is_method) res.object = this.ref()
return res;
}
sync = async()=>{
return this;
}
}
export default Instance