function callExpr()

in packages/sqrl/src/ast/AstExpr.ts [275:317]


function callExpr(state: AstExprState, func: string, args: Ast[]): Expr {
  const { instance } = state;
  const props = instance.getProps(func);

  if (props.pure) {
    const argExprs = args.map((arg) => _astToExpr(arg, state));
    const allConstant = argExprs.every((expr) => expr.type === "constant");
    if (allConstant) {
      const constantExprs: ConstantExpr[] = argExprs.map((expr) => {
        // @TODO: This is an unfortunate hack to make typescript happy
        if (expr.type !== "constant") {
          throw new Error("expected constant");
        }
        return expr;
      });
      const rv = state.instance.pureFunction[func](
        ...constantExprs.map((expr) => expr.value)
      );
      return constantExpr(rv);
    }
  }

  if (func === "_slotWait") {
    return Object.assign(constantExpr(true), {
      load: args
        .filter((arg) => {
          // Filter out wait for anything that is constant
          const expr = _astToExpr(arg, state);
          return expr.type !== "constant";
        })
        .map((arg) => {
          if (arg.type !== "slot") {
            throw new Error("Expected slot ast for wait call");
          }
          return state.getSlot(arg.slotName);
        }),
    });
  }

  // If the function takes a promise
  const lazy = !!props.lazyArguments;
  return _astToExprList(args, state, { type: "call", func }, lazy);
}