UNPKG

ifc-expressions

Version:

Parsing and evaluation of IFC expressions

56 lines (55 loc) 2.55 kB
import { ExprBase } from "../ExprBase.js"; import { ExprKind } from "../ExprKind.js"; import { ExprEvalErrorObj, ExprEvalStatus, isExprEvalError, } from "../ExprEvalResult.js"; import { ContextObjectValue } from "../../value/ContextObjectValue.js"; import { getBuiltinMemberValue, toExpressionValue, unwrapExpressionValue, } from "../../builtin/BuiltinRuntimeValueConverter.js"; export class BuiltinFunctionCallExpr extends ExprBase { constructor(target, name, args, type, targetName) { super(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 (isExprEvalError(targetResult)) { return targetResult; } if (!(targetResult.result instanceof ContextObjectValue)) { return new ExprEvalErrorObj(this.getKind(), 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 (isExprEvalError(argResult)) { return argResult; } evaluatedArgs.push(argResult.result); } const fn = getBuiltinMemberValue(targetResult.result, this.name); if (typeof fn !== "function") { return new ExprEvalErrorObj(this.getKind(), ExprEvalStatus.NOT_FOUND, `No builtin function '${this.name}' found on '${this.targetName}'`, this.getTextSpan()); } const rawResult = fn.apply(targetResult.result.getValue(), evaluatedArgs.map((arg) => unwrapExpressionValue(arg))); if (rawResult === undefined || rawResult === null) { return new ExprEvalErrorObj(this.getKind(), ExprEvalStatus.NOT_FOUND, `Builtin function '${this.name}' on '${this.targetName}' returned no value`, this.getTextSpan()); } return this.wrapInResultIfNecessary(toExpressionValue(rawResult, this.type)); } buildExprString(builder) { builder .appendExpr(this.target) .appendString(`.${this.name}(`) .appendExprArray(this.args) .appendString(")"); } getType() { return this.type; } } //# sourceMappingURL=BuiltinFunctionCallExpr.js.map