use of lucee.runtime.interpreter.InterpreterException in project Lucee by lucee.
the class VT method getMatchingValueAndType.
private VT getMatchingValueAndType(FunctionLibFunctionArg flfa, FunctionValue[] fvalues, String[] names) throws ExpressionException {
String flfan = flfa.getName();
// first search if a argument match
for (int i = 0; i < names.length; i++) {
if (names[i] != null && names[i].equalsIgnoreCase(flfan)) {
return new VT(fvalues[i].getValue(), flfa.getTypeAsString(), i);
}
}
// then check if a alias match
String alias = flfa.getAlias();
if (!StringUtil.isEmpty(alias)) {
for (int i = 0; i < names.length; i++) {
if (names[i] != null && lucee.runtime.type.util.ListUtil.listFindNoCase(alias, names[i], ",") != -1) {
return new VT(fvalues[i].getValue(), flfa.getTypeAsString(), i);
}
}
}
// if not required return the default value
if (!flfa.getRequired()) {
String defaultValue = flfa.getDefaultValue();
String type = flfa.getTypeAsString().toLowerCase();
if (defaultValue == null) {
if (type.equals("boolean") || type.equals("bool"))
return new VT(Boolean.FALSE, type, -1);
if (type.equals("number") || type.equals("numeric") || type.equals("double"))
return new VT(Constants.DOUBLE_ZERO, type, -1);
return new VT(null, type, -1);
}
return new VT(defaultValue, type, -1);
}
ExpressionException ee = new InterpreterException("missing required argument [" + flfan + "] for function [" + flfa.getFunction().getName() + "]");
UDFUtil.addFunctionDoc(ee, flfa.getFunction());
throw ee;
}
use of lucee.runtime.interpreter.InterpreterException in project Lucee by lucee.
the class Elvis method getValue.
@Override
public Object getValue(PageContext pc) throws PageException {
if (limited)
throw new InterpreterException("invalid syntax, this operation is not supported in a json string.");
if (left instanceof Variable) {
Variable var = (Variable) left;
String[] arr = LFunctionValue.toStringArray(pc, var);
return lucee.runtime.op.Elvis.operate(pc, arr) ? left.getValue(pc) : right.getValue(pc);
}
Object val = left.getValue(pc);
if (val != null)
return val;
return right.getValue(pc);
}
use of lucee.runtime.interpreter.InterpreterException in project Lucee by lucee.
the class VT method isNamed.
private boolean isNamed(PageContext pc, Ref[] refArgs) throws PageException {
if (ArrayUtil.isEmpty(refArgs))
return false;
Casting cast;
int count = 0;
for (int i = 0; i < refArgs.length; i++) {
if (refArgs[i] instanceof Casting) {
cast = (Casting) refArgs[i];
if (cast.getRef() instanceof LFunctionValue && ((LFunctionValue) cast.getRef()).getValue(pc) instanceof FunctionValue) {
count++;
}
}
}
if (count != 0 && count != refArgs.length) {
ExpressionException ee = new InterpreterException("invalid argument for function " + flf.getName() + ", you can not mix named and unnamed arguments");
UDFUtil.addFunctionDoc(ee, flf);
throw ee;
}
return count != 0;
}
use of lucee.runtime.interpreter.InterpreterException in project Lucee by lucee.
the class VT method getValue.
@Override
public Object getValue(PageContext pc) throws PageException {
Object[] arguments = null;
if (isDynamic()) {
arguments = RefUtil.getValue(pc, refArgs);
if (flf.hasDefaultValues()) {
List<Object> tmp = new ArrayList<Object>();
ArrayList<FunctionLibFunctionArg> args = flf.getArg();
Iterator<FunctionLibFunctionArg> it = args.iterator();
FunctionLibFunctionArg arg;
while (it.hasNext()) {
arg = it.next();
if (arg.getDefaultValue() != null)
tmp.add(new FunctionValueImpl(arg.getName(), arg.getDefaultValue()));
}
for (int i = 0; i < arguments.length; i++) {
tmp.add(arguments[i]);
}
arguments = tmp.toArray();
}
arguments = new Object[] { arguments };
} else {
if (isNamed(pc, refArgs)) {
FunctionValue[] fvalues = getFunctionValues(pc, refArgs);
String[] names = getNames(fvalues);
ArrayList<FunctionLibFunctionArg> list = flf.getArg();
Iterator<FunctionLibFunctionArg> it = list.iterator();
arguments = new Object[list.size()];
FunctionLibFunctionArg flfa;
int index = 0;
VT vt;
while (it.hasNext()) {
flfa = it.next();
vt = getMatchingValueAndType(flfa, fvalues, names);
if (vt.index != -1)
names[vt.index] = null;
arguments[index++] = new Casting(vt.type, CFTypes.toShort(vt.type, false, CFTypes.TYPE_UNKNOW), vt.value).getValue(pc);
}
for (int y = 0; y < names.length; y++) {
if (names[y] != null) {
ExpressionException ee = new InterpreterException("argument [" + names[y] + "] is not allowed for function [" + flf.getName() + "]");
UDFUtil.addFunctionDoc(ee, flf);
throw ee;
}
}
} else {
arguments = RefUtil.getValue(pc, refArgs);
}
}
BIF bif = flf.getBIF();
if (flf.getMemberChaining() && obj != null) {
bif.invoke(pc, arguments);
return obj;
}
if (!isDynamic() && flf.getArgMin() > arguments.length) {
throw new FunctionException(pc, flf.getName(), flf.getArgMin(), flf.getArgMax(), arguments.length);
}
return Caster.castTo(pc, flf.getReturnTypeAsString(), bif.invoke(pc, arguments), false);
}
Aggregations