2022-08-27 17:46:43 +00:00
|
|
|
import * as RSLambda from "../rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue_Lambda.gen";
|
2022-08-30 11:16:57 +00:00
|
|
|
import { SqError } from "./SqError";
|
|
|
|
import { SqValue } from "./SqValue";
|
2022-08-29 21:51:44 +00:00
|
|
|
import { SqValueLocation } from "./SqValueLocation";
|
2022-08-30 11:16:57 +00:00
|
|
|
import { result } from "./types";
|
2022-08-27 17:46:43 +00:00
|
|
|
|
|
|
|
type T = RSLambda.squiggleValue_Lambda;
|
|
|
|
|
2022-08-28 16:19:44 +00:00
|
|
|
export class SqLambda {
|
2022-08-29 21:51:44 +00:00
|
|
|
constructor(private _value: T, public location: SqValueLocation) {}
|
2022-08-27 19:50:03 +00:00
|
|
|
|
|
|
|
parameters() {
|
|
|
|
return RSLambda.parameters(this._value);
|
|
|
|
}
|
2022-08-30 11:16:57 +00:00
|
|
|
|
|
|
|
call(args: (number | string)[]): result<SqValue, SqError> {
|
|
|
|
const { project, sourceId } = this.location;
|
|
|
|
// Might be good to use uuid instead, but there's no way to remove sources from projects.
|
|
|
|
// So this is not thread-safe.
|
|
|
|
const callId = "__lambda__";
|
|
|
|
const quote = (arg: string) =>
|
|
|
|
`"${arg.replace(new RegExp('"', "g"), '\\"')}"`;
|
|
|
|
const argsSource = args
|
|
|
|
.map((arg) => (typeof arg === "number" ? arg : quote(arg)))
|
|
|
|
.join(",");
|
2022-09-01 15:11:49 +00:00
|
|
|
|
|
|
|
// end expression values are exposed in bindings via secret `__result__` variable and we can access them through it
|
|
|
|
const pathItems = [
|
|
|
|
...(this.location.path.root === "result" ? ["__result__"] : []),
|
|
|
|
...this.location.path.items,
|
|
|
|
];
|
|
|
|
|
|
|
|
// full function name, e.g. foo.bar[3].baz
|
|
|
|
const functionNameSource = pathItems
|
2022-08-30 11:16:57 +00:00
|
|
|
.map((item, i) =>
|
|
|
|
typeof item === "string" ? (i ? "." + item : item) : `[${item}]`
|
|
|
|
)
|
|
|
|
.join("");
|
2022-09-01 15:11:49 +00:00
|
|
|
|
|
|
|
// something like: foo.bar[3].baz(1,2,3)
|
2022-08-30 11:16:57 +00:00
|
|
|
const source = `${functionNameSource}(${argsSource})`;
|
2022-09-01 15:11:49 +00:00
|
|
|
|
2022-08-30 11:16:57 +00:00
|
|
|
project.setSource(callId, source);
|
|
|
|
project.setContinues(callId, [sourceId]);
|
|
|
|
project.run(callId);
|
|
|
|
return project.getResult(callId);
|
|
|
|
}
|
2022-08-27 17:46:43 +00:00
|
|
|
}
|