godprotocol
Version:
A distributed computing environment
212 lines (195 loc) • 4.46 kB
JavaScript
import Arr from "../Datatypes/arr";
import Callable from "../Datatypes/callable";
import Num from "../Datatypes/num";
import Str from "../Datatypes/str";
import Twa from "../Datatypes/twa";
import Ins from "../Datatypes/instance";
import Voi from "../Datatypes/voi";
import Bool from "../Datatypes/bool";
import Instance from "../Datatypes/instance";
import Func from "../Datatypes/func";
import Cls from "../Datatypes/cls";
import { ops } from "./ops";
class Vm_utils {
constructor() {
this.ops = ops;
this.literal_methods = {
number: [
"add",
"sub",
"div",
"mul",
"exp",
"floordiv",
"eq",
"ne",
"lt",
"gt",
"gte",
"lte",
"neg",
"abs",
"and",
"or",
"xor",
"lshift",
"rshift",
"invert",
]
.map((i) => `__${i}__`)
.concat([
"to_int",
"to_float",
"to_string",
"sqrt",
"log",
"sin",
"sinh",
"tanh",
"cosh",
"atanh",
"acosh",
"asinh",
"atan",
"acos",
"asin",
"cos",
"tan",
"round",
"factorial",
"ceil",
"floor",
"is_prime",
"is_even",
"is_odd",
"is_perfect_square",
"to_degrees",
"to_radians",
]),
string: [
"upper",
"lower",
"title",
"slice",
"concat",
"find",
"includes",
"index",
"trim",
"split",
"padstart",
"padend",
"repeat",
"replace",
"startswith",
"endswith",
"count",
"length",
"reverse",
"is_numeric",
"is_empty",
"is_blank",
"charcode",
],
array: [
"push",
"pop",
"length",
"run",
"copy",
"deepcopy",
"extend",
"slice",
"index",
"includes",
"replace",
"map",
"find",
"filter",
"padstart",
"padend",
"clear",
"to_string",
"reverse",
"repeat",
"join",
"splice",
],
twain: [
"entries",
"get",
"set",
"keys",
"clear",
"is_empty",
"values",
"copy",
"deepcopy",
"remove",
],
};
this.datatypes = ["string", "number", "void", "boolean", "array", "twain"];
}
gen_uid = () => {
return `_${Math.random().toString().replace(".", "")}_${Date.now()}`;
};
parse_dynamic = async (literal, options = {}) => {
let { chain, config } = options;
let aircode = this.account.aircode;
let sequence = aircode.to_sequence(
JSON.stringify(literal),
chain.physical_address
);
let res = await chain.add_config(sequence[0].body[0]);
let address = await this.execute(res, { chain });
return address;
};
resolve_addr = (addr) => {
if (!addr) return addr;
if (addr.startsWith("@")) {
addr = `${this.account.physical_address}/${addr.slice(2)}`;
}
return addr;
};
cloth_object = async (addr) => {};
cloth_content = async (content, options) => {
let obj;
if (content instanceof Instance) return content;
switch (content.type) {
case "number":
obj = new Num(content, this.account);
break;
case "string":
obj = new Str(content, this.account);
break;
case "boolean":
obj = new Bool(content, this.account);
break;
case "void":
obj = new Voi(content, this.account);
break;
case "array":
obj = new Arr(content, this.account);
break;
case "twain":
obj = new Twa(content, this.account);
break;
case "instance":
obj = new Instance(content, this.account);
break;
case "function":
obj = new Func(content, this.account);
break;
case "class":
obj = new Cls(content, this.account);
break;
default:
if (content.type === "address") {
obj = new Func(content, this.account);
} else obj = content;
// console.log(content, 'uh')
}
return await obj.sync(options);
};
}
export default Vm_utils;