godprotocol
Version:
A distributed computing environment
136 lines (106 loc) • 3.5 kB
JavaScript
import Utils from "./Utils";
import register from "../callables/register";
import Chain from "./Chain";
import Virtual_machine from "./Virtual_machine";
class Account extends Utils {
constructor(name, options) {
super();
this.name = name;
this.manager = options.manager;
this.parent = options.parent || this;
this.repository = this.manager.repository;
this.account = this;
this.physical_address = `Accounts/${name}`;
this.buffs = new Array();
this.chain = new Chain(this.physical_address, this.parent);
this.path = this.chain.path;
console.log(`Account Created: ${this.name}`);
}
load = async (payload) => {
let { sequence: codes, account, physical_address, options } = payload;
console.log(payload);
if (account && account !== this.name) {
account = await this.manager.get_account(account);
if (account) {
return await account.load(payload);
} else return { error: true, message: "Account not found!" };
}
if (typeof codes === "string") {
codes = await this.compiler.to_sequence(codes, physical_address);
if (options) {
codes = codes[0].body[options.line];
}
}
if (Array.isArray(codes)) {
codes = codes[0];
}
physical_address = `${this.physical_address}/${physical_address}`;
let chain = await this.chain.chain(physical_address);
let res = { value: await chain.add_config(codes), type: "address" };
console.log(res, codes, chain.physical_address);
return res;
};
run = async (payload) => {
let { physical_address, account, options } = payload;
let acc = (await this.manager.get_account(account)) || this;
let chain = await acc.chain.chain(
`${acc.physical_address}/${physical_address}`
);
console.log(chain.physical_address);
let config = await chain.read({ config: true });
if (options.env) {
let res = await this.load({
sequence: `env = ${JSON.stringify(options.env)}`,
physical_address: `${physical_address}`,
options: { line: 0 },
});
await this.vm.execute(res.value, { chain });
}
let res = await this.vm.execute(config, { chain, main: true });
if (options && options.literal) {
if (res.type === "address") {
let conf = await this.vm.resolve_address(res.value);
res = {
address: res,
literal: await (await this.vm.cloth_content(conf)).literal(),
};
}
}
return res;
};
parse = async (payload) => {
let { account, physical_address, depth, history, query } = payload;
let acc = (await this.manager.get_account(account)) || this;
let chain = await acc.chain.chain(
`${acc.physical_address}/${physical_address}`
);
let config = await chain.read({ query, depth, history });
return config;
};
add_account = async (name, options) => {
let acc = new Account(name, {
...options,
manager: this.manager,
parent: this,
});
await acc.sync();
return acc;
};
sync = async () => {
this.servers = new Array();
this.vm = new Virtual_machine(this);
this.compiler = new this.manager.compiler(this);
await this.chain.sync();
await this.vm.sync();
await register(this);
};
stringify = () => {
return {
physical_address: this.physical_address,
path: this.path,
name: this.name,
private: this.private,
};
};
}
export default Account;