godprotocol
Version:
A distributed computing environment for Web 4.0 — integrating AI, decentralisation, and virtual computation.
62 lines (54 loc) • 1.76 kB
JavaScript
const import_ = async(args, {vm, chain, thread})=>{
let {address, identifiers} = args, account = vm.account;
address = await address.literal()
identifiers = identifiers && await identifiers.literal()
console.log(address, identifiers)
if (address.startsWith('@/'))
address = address.slice(2)
let res = await account.run({
address,
thread:{_id: thread._id, pointer: thread.pointer}
, callback: async(res)=>{
let id = Array.isArray(identifiers) ? [...identifiers] : [identifiers]
let twa = {}
for (let i=0; i< id.length; i++){
let iden = id[i]
for (let r=res.length-1; r>=0; r--){
let obj = res[r]
if (!obj) continue;
if (obj.type === 'address'){
if (obj.identifier === iden){
twa[iden] = obj;
}
}else if (obj.type === 'function'){
if (obj.address.endsWith(`/${iden}`)){
twa[iden] = obj
}
}
if (twa[iden]) break;
}
if (!twa[iden]) {
thread.results[thread.pointer-1] = await vm.throw_error({
type:"ImportError",
message: `No export - ${iden}`
})
return
}
}
let result = twa[id[0]]
if (Array.isArray(identifiers)){
let actual_twain = {type:'twain', value: {}, location: await vm.gen_location()}
for (let prop in twa){
let res = await vm.instantiate({
type: 'string',
value: prop,
location: await vm.gen_location()
}, {chain})
actual_twain.value[await vm.hash(prop, 'sha1')] = [res, twa[prop]]
}
result = await vm.instantiate(actual_twain, {chain})
}
thread.results[thread.pointer-1] = result
}})
}
export default import_