use of lucee.runtime.type.FunctionValueImpl 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);
}
use of lucee.runtime.type.FunctionValueImpl in project Lucee by lucee.
the class Struct_ method _call.
protected static Struct _call(Object[] objArr, String expMessage, int type) throws PageException {
StructImpl sct = type < 0 ? new StructImpl() : new StructImpl(type);
FunctionValueImpl fv;
for (int i = 0; i < objArr.length; i++) {
if (objArr[i] instanceof FunctionValue) {
fv = ((FunctionValueImpl) objArr[i]);
if (fv.getNames() == null) {
sct.set(fv.getNameAsKey(), fv.getValue());
} else {
String[] arr = fv.getNames();
Struct s = sct;
for (int y = 0; y < arr.length - 1; y++) {
s = touch(s, arr[y]);
}
s.set(KeyImpl.init(arr[arr.length - 1]), fv.getValue());
}
} else {
throw new ExpressionException(expMessage);
}
}
return sct;
}
use of lucee.runtime.type.FunctionValueImpl in project Lucee by lucee.
the class Caster method toFunctionValues.
public static Struct toFunctionValues(Object[] args, int offset, int len) throws ExpressionException {
// TODO nicht sehr optimal
Struct sct = new StructImpl(StructImpl.TYPE_LINKED);
for (int i = offset; i < offset + len; i++) {
if (args[i] instanceof FunctionValueImpl) {
FunctionValueImpl value = (FunctionValueImpl) args[i];
sct.setEL(value.getNameAsKey(), value.getValue());
} else
throw new ExpressionException("Missing argument name, when using named parameters to a function, every parameter must have a name [" + i + ":" + args[i].getClass().getName() + "].");
}
return sct;
}
use of lucee.runtime.type.FunctionValueImpl in project Lucee by lucee.
the class Caster method toFunctionValues.
public static Object[] toFunctionValues(Struct args) {
// TODO nicht sehr optimal
Iterator<Entry<Key, Object>> it = args.entryIterator();
Entry<Key, Object> e;
List<FunctionValue> fvalues = new ArrayList<FunctionValue>();
while (it.hasNext()) {
e = it.next();
fvalues.add(new FunctionValueImpl(e.getKey().getString(), e.getValue()));
}
return fvalues.toArray(new FunctionValue[fvalues.size()]);
}
Aggregations