ifc-expressions
Version:
Parsing and evaluation of IFC expressions
60 lines (59 loc) • 3.02 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BuiltinFunctionCallExpr = void 0;
const ExprBase_js_1 = require("../ExprBase.js");
const ExprKind_js_1 = require("../ExprKind.js");
const ExprEvalResult_js_1 = require("../ExprEvalResult.js");
const ContextObjectValue_js_1 = require("../../value/ContextObjectValue.js");
const BuiltinRuntimeValueConverter_js_1 = require("../../builtin/BuiltinRuntimeValueConverter.js");
class BuiltinFunctionCallExpr extends ExprBase_js_1.ExprBase {
constructor(target, name, args, type, targetName) {
super(ExprKind_js_1.ExprKind.REF_BUILTIN_FUNCTION);
this.target = target;
this.name = name;
this.args = args;
this.type = type;
this.targetName = targetName;
}
getChildren() {
return [this.target, ...this.args];
}
evaluate(ctx, localCtx) {
const targetResult = this.target.evaluate(ctx, localCtx);
if ((0, ExprEvalResult_js_1.isExprEvalError)(targetResult)) {
return targetResult;
}
if (!(targetResult.result instanceof ContextObjectValue_js_1.ContextObjectValue)) {
return new ExprEvalResult_js_1.ExprEvalErrorObj(this.getKind(), ExprEvalResult_js_1.ExprEvalStatus.TYPE_ERROR, `Builtin function '${this.name}' requires a context object target`, this.getTextSpan());
}
const evaluatedArgs = [];
for (const arg of this.args) {
const argResult = arg.evaluate(ctx, localCtx);
if ((0, ExprEvalResult_js_1.isExprEvalError)(argResult)) {
return argResult;
}
evaluatedArgs.push(argResult.result);
}
const fn = (0, BuiltinRuntimeValueConverter_js_1.getBuiltinMemberValue)(targetResult.result, this.name);
if (typeof fn !== "function") {
return new ExprEvalResult_js_1.ExprEvalErrorObj(this.getKind(), ExprEvalResult_js_1.ExprEvalStatus.NOT_FOUND, `No builtin function '${this.name}' found on '${this.targetName}'`, this.getTextSpan());
}
const rawResult = fn.apply(targetResult.result.getValue(), evaluatedArgs.map((arg) => (0, BuiltinRuntimeValueConverter_js_1.unwrapExpressionValue)(arg)));
if (rawResult === undefined || rawResult === null) {
return new ExprEvalResult_js_1.ExprEvalErrorObj(this.getKind(), ExprEvalResult_js_1.ExprEvalStatus.NOT_FOUND, `Builtin function '${this.name}' on '${this.targetName}' returned no value`, this.getTextSpan());
}
return this.wrapInResultIfNecessary((0, BuiltinRuntimeValueConverter_js_1.toExpressionValue)(rawResult, this.type));
}
buildExprString(builder) {
builder
.appendExpr(this.target)
.appendString(`.${this.name}(`)
.appendExprArray(this.args)
.appendString(")");
}
getType() {
return this.type;
}
}
exports.BuiltinFunctionCallExpr = BuiltinFunctionCallExpr;
//# sourceMappingURL=BuiltinFunctionCallExpr.js.map