Add Array support to functions

This commit is contained in:
K35
2022-01-02 20:51:45 +00:00
parent a8f49d117d
commit db21c1c42e
7 changed files with 143 additions and 40 deletions

View File

@ -7193,7 +7193,12 @@ function parse(tokens, file) {
const [name2] = eatText();
eatToken(":");
const [type] = eatText();
inputs.push({ name: name2, type });
let array = false;
if (currentToken.type === "array") {
array = true;
eatToken("[]");
}
inputs.push({ name: name2, type, array });
if (currentToken.value !== ",")
break;
eatToken(",");
@ -7203,7 +7208,16 @@ function parse(tokens, file) {
let return_type = void 0;
if (!notification) {
eatToken(":");
return_type = eatText()[0];
let [type] = eatText();
let array = false;
if (currentToken.type === "array") {
array = true;
eatToken("[]");
}
return_type = {
type,
array
};
}
eatToken(";");
return {
@ -7365,11 +7379,11 @@ function get_ir(parsed) {
throw new IRError(fnc, `Function with name ${fnc.name} already defined!`);
alreadyFoundFunctions.add(fnc.name);
if (fnc.return_type) {
if (defined.indexOf(fnc.return_type) >= 0) {
if (!depends.some((a) => a === fnc.return_type))
depends.push(fnc.return_type);
if (defined.indexOf(fnc.return_type.type) >= 0) {
if (!depends.some((a) => a === fnc.return_type.type))
depends.push(fnc.return_type.type);
} else {
if (fnc.return_type !== "void" && builtin.indexOf(fnc.return_type) < 0) {
if (fnc.return_type.type !== "void" && builtin.indexOf(fnc.return_type.type) < 0) {
throw new IRError(fnc, `Type ${fnc.return_type} is not defined`);
}
}
@ -7642,7 +7656,7 @@ var TypescriptTarget = class extends CompileTarget {
a(2, `super(provider, "${def.name}");`);
a(1, `}`);
for (const fnc of def.functions) {
const params = fnc.inputs.map((e) => `${e.name}: ${toJSType(e.type)}`).join(",");
const params = fnc.inputs.map((e) => `${e.name}: ${toJSType(e.type) + (e.array ? "[]" : "")}`).join(", ");
if (!fnc.return) {
a(1, `${fnc.name}(${params}): void {`);
a(2, `this._provider.sendMessage({`);
@ -7652,7 +7666,7 @@ var TypescriptTarget = class extends CompileTarget {
a(2, `});`);
a(1, `}`);
} else {
const retType = fnc.return ? toJSType(fnc.return) : "void";
const retType = fnc.return ? toJSType(fnc.return.type) + (fnc.return.array ? "[]" : "") : "void";
a(1, `${fnc.name}(${params}): Promise<${retType}> {`);
a(2, `return new Promise<${retType}>((ok, err) => {`);
a(3, `this._provider.sendMessage({`);
@ -7664,7 +7678,13 @@ var TypescriptTarget = class extends CompileTarget {
a(4, `ok, err`);
a(3, `});`);
a(2, `}).then(result => {`);
a(3, `if(!verify_${fnc.return}(result)) throw new Error("Invalid result data!");`);
if (fnc.return.array) {
a(2, `for(const elm of result) {`);
a(3, `if(!verify_${fnc.return.type}(elm)) throw new Error("Invalid result data!");`);
a(2, `}`);
} else {
a(3, `if(!verify_${fnc.return.type}(result)) throw new Error("Invalid result data!");`);
}
a(3, `return result;`);
a(2, `});`);
a(1, `}`);
@ -7675,6 +7695,7 @@ var TypescriptTarget = class extends CompileTarget {
this.writeFormattedFile(this.getFileName(def.name + "_client"), lines.join("\n"));
}
generateServiceServer(def) {
var _a;
let lines = [];
const a = (i, t) => {
if (!Array.isArray(t)) {
@ -7703,10 +7724,10 @@ var TypescriptTarget = class extends CompileTarget {
a(0, ``);
for (const fnc of def.functions) {
const params = [
...fnc.inputs.map((e) => `${e.name}: ${toJSType(e.type)}`),
...fnc.inputs.map((e) => `${e.name}: ${toJSType(e.type) + (e.array ? "[]" : "")}`),
`ctx: T`
].join(", ");
const retVal = fnc.return ? `Promise<${toJSType(fnc.return)}>` : `void`;
const retVal = fnc.return ? `Promise<${toJSType(fnc.return.type) + (fnc.return.array ? "[]" : "")}>` : `void`;
a(1, `abstract ${fnc.name}(${params}): ${retVal};`);
a(1, `_${fnc.name}(params: any[] | any, ctx: T): ${retVal} {`);
a(2, `let p: any[] = [];`);
@ -7720,12 +7741,18 @@ var TypescriptTarget = class extends CompileTarget {
a(2, ``);
for (let i = 0; i < fnc.inputs.length; i++) {
a(2, `if(p[${i}] !== null && p[${i}] !== undefined) {`);
a(2, `if(!verify_${fnc.inputs[i].type}(p[${i}])) throw new Error("Parameter verification failed!")`);
if (fnc.inputs[i].array) {
a(2, `for(const elm of p[${i}]) {`);
a(3, `if(!verify_${fnc.inputs[i].type}(elm)) throw new Error("Parameter verification failed!")`);
a(2, `}`);
} else {
a(2, `if(!verify_${fnc.inputs[i].type}(p[${i}])) throw new Error("Parameter verification failed!")`);
}
a(2, `}`);
}
a(2, ``);
a(2, `p.push(ctx);`);
a(2, `return this.${fnc.name}.call(this, ...p)${fnc.return == "void" ? ".then(res => undefined)" : ""};`);
a(2, `return this.${fnc.name}.call(this, ...p)${((_a = fnc.return) == null ? void 0 : _a.type) == "void" ? ".then(res => undefined)" : ""};`);
a(1, `}`);
a(0, ``);
}