use of lucee.runtime.type.FunctionArgument in project Lucee by lucee.
the class ComponentController method _invoke.
public static Object _invoke(String name, Object[] args) throws PageException {
Key key = Caster.toKey(name);
Component c = component.get();
PageContext p = pagecontext.get();
MessageContext mc = messageContext.get();
if (c == null)
throw new ApplicationException("missing component");
if (p == null)
throw new ApplicationException("missing pagecontext");
UDF udf = Caster.toFunction(c.get(p, key, null), null);
FunctionArgument[] fa = null;
if (udf != null)
fa = udf.getFunctionArguments();
for (int i = 0; i < args.length; i++) {
if (fa != null && i < fa.length && fa[i].getType() == CFTypes.TYPE_UNKNOW) {
args[i] = AxisCaster.toLuceeType(p, fa[i].getTypeAsString(), args[i]);
} else
args[i] = AxisCaster.toLuceeType(p, args[i]);
}
// return type
String rtnType = udf != null ? udf.getReturnTypeAsString() : "any";
Object rtn = c.call(p, key, args);
// cast return value to Axis type
try {
RPCServer server = RPCServer.getInstance(p.getId(), p, p.getServletContext());
TypeMapping tm = mc != null ? mc.getTypeMapping() : TypeMappingUtil.getServerTypeMapping(server.getEngine().getTypeMappingRegistry());
rtn = Caster.castTo(p, rtnType, rtn, false);
Class<?> clazz = Caster.cfTypeToClass(rtnType);
return AxisCaster.toAxisType(tm, rtn, clazz.getComponentType() != null ? clazz : null);
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
throw Caster.toPageException(t);
}
}
use of lucee.runtime.type.FunctionArgument in project Lucee by lucee.
the class Props method translate.
public static Struct translate(Component c, String strMethodName, Struct params) {
Collection.Key methodName = KeyImpl.init(strMethodName);
Key[] keys = CollectionUtil.keys(params);
FunctionArgument[] args = null;
int index = -1;
Object value;
for (int i = 0; i < keys.length; i++) {
index = Caster.toIntValue(keys[i].getString(), 0);
if (index > 0) {
if (args == null)
args = _getArgs(c, methodName);
if (args != null && index <= args.length) {
value = params.removeEL(keys[i]);
if (value != null)
params.setEL(args[index - 1].getName(), value);
}
}
}
return params;
}
use of lucee.runtime.type.FunctionArgument in project Lucee by lucee.
the class ComponentUtil method createMethod.
private static int createMethod(ConstrBytecodeContext constr, java.util.List<LitString> keys, ClassWriter cw, String className, Object member, int max, boolean writeLog, boolean suppressWSbeforeArg, boolean output, boolean returnValue) throws PageException {
boolean hasOptionalArgs = false;
if (member instanceof UDF) {
UDF udf = (UDF) member;
FunctionArgument[] args = udf.getFunctionArguments();
Type[] types = new Type[max < 0 ? args.length : max];
for (int y = 0; y < types.length; y++) {
types[y] = toType(args[y].getTypeAsString(), true);
if (!args[y].isRequired())
hasOptionalArgs = true;
}
Type rtnType = toType(udf.getReturnTypeAsString(), true);
Method method = new Method(udf.getFunctionName(), rtnType, types);
GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, method, null, null, cw);
BytecodeContext bc = new BytecodeContext(null, constr, getPage(constr), keys, cw, className, adapter, method, writeLog, suppressWSbeforeArg, output, returnValue);
Label start = adapter.newLabel();
adapter.visitLabel(start);
// ComponentController.invoke(name, args);
// name
adapter.push(udf.getFunctionName());
// args
ArrayVisitor av = new ArrayVisitor();
av.visitBegin(adapter, Types.OBJECT, types.length);
for (int y = 0; y < types.length; y++) {
av.visitBeginItem(adapter, y);
adapter.loadArg(y);
av.visitEndItem(bc.getAdapter());
}
av.visitEnd();
adapter.invokeStatic(Types.COMPONENT_CONTROLLER, INVOKE);
adapter.checkCast(rtnType);
// ASMConstants.NULL(adapter);
adapter.returnValue();
Label end = adapter.newLabel();
adapter.visitLabel(end);
for (int y = 0; y < types.length; y++) {
adapter.visitLocalVariable(args[y].getName().getString(), types[y].getDescriptor(), null, start, end, y + 1);
}
adapter.endMethod();
if (hasOptionalArgs) {
if (max == -1)
max = args.length - 1;
else
max--;
return max;
}
}
return -1;
}
use of lucee.runtime.type.FunctionArgument in project Lucee by lucee.
the class UDFUtil method toDumpData.
public static DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp, UDF udf, short type) {
if (!dp.getShowUDFs()) {
if (TYPE_UDF == type)
return new SimpleDumpData("<UDF>");
if (TYPE_BIF == type)
return new SimpleDumpData("<BIF>");
if (TYPE_CLOSURE == type)
return new SimpleDumpData("<Closure>");
if (TYPE_LAMBDA == type)
return new SimpleDumpData("<Lambda>");
}
// arguments
FunctionArgument[] args = udf.getFunctionArguments();
DumpTable atts;
if (TYPE_UDF == type)
atts = new DumpTable("udf", "#ca5095", "#e9accc", "#000000");
else if (TYPE_CLOSURE == type)
atts = new DumpTable("udf", "#9cb770", "#c7e1ba", "#000000");
else if (TYPE_BIF == type)
atts = new DumpTable("udf", "#e1c039", "#f1e2a3", "#000000");
else
atts = new DumpTable("udf", "#f3d5bd", "#f6e4cc", "#000000");
atts.appendRow(new DumpRow(63, new DumpData[] { new SimpleDumpData("label"), new SimpleDumpData("name"), new SimpleDumpData("required"), new SimpleDumpData("type"), new SimpleDumpData("default"), new SimpleDumpData("hint") }));
for (int i = 0; i < args.length; i++) {
FunctionArgument arg = args[i];
DumpData def;
try {
Object oa = udf.getDefaultValue(pageContext, i, null);
if (oa == null)
oa = "null";
def = new SimpleDumpData(Caster.toString(oa));
} catch (PageException e) {
def = new SimpleDumpData("");
}
atts.appendRow(new DumpRow(0, new DumpData[] { new SimpleDumpData(arg.getDisplayName()), new SimpleDumpData(arg.getName().getString()), new SimpleDumpData(arg.isRequired()), new SimpleDumpData(arg.getTypeAsString()), def, new SimpleDumpData(arg.getHint()) }));
// atts.setRow(0,arg.getHint());
}
DumpTable func;
String label = udf.getDisplayName();
if (TYPE_CLOSURE == type) {
func = new DumpTable("#9cb770", "#c7e1ba", "#000000");
func.setTitle(StringUtil.isEmpty(label) ? "Closure" : "Closure " + label);
} else if (TYPE_UDF == type) {
func = new DumpTable("#ca5095", "#e9accc", "#000000");
String f = "Function ";
try {
f = StringUtil.ucFirst(ComponentUtil.toStringAccess(udf.getAccess()).toLowerCase()) + " " + f;
} catch (ApplicationException e) {
}
f += udf.getFunctionName();
if (udf instanceof UDFGSProperty)
f += " (generated)";
func.setTitle(f);
} else if (TYPE_BIF == type) {
String f = "Build in Function " + (!StringUtil.isEmpty(label) ? label : udf.getFunctionName());
func = new DumpTable("#e1c039", "#f1e2a3", "#000000");
func.setTitle(f);
} else {
func = new DumpTable("#f3d5bd", "#f6e4cc", "#000000");
func.setTitle(StringUtil.isEmpty(label) ? "Lambda" : "Lambda " + label);
}
// Source
String src = udf.getSource();
if (!StringUtil.isEmpty(src))
func.setComment("source:" + src);
String hint = udf.getHint();
String desc = udf.getDescription();
if (!StringUtil.isEmpty(desc))
addComment(func, desc);
if (!StringUtil.isEmpty(hint))
addComment(func, hint);
if (Component.MODIFIER_NONE != udf.getModifier())
func.appendRow(1, new SimpleDumpData("modifier"), new SimpleDumpData(ComponentUtil.toModifier(udf.getModifier(), "")));
func.appendRow(1, new SimpleDumpData("arguments"), atts);
func.appendRow(1, new SimpleDumpData("return type"), new SimpleDumpData(udf.getReturnTypeAsString()));
return func;
}
use of lucee.runtime.type.FunctionArgument in project Lucee by lucee.
the class GetFunctionData method cfmlBasedFunction.
private static Struct cfmlBasedFunction(PageContext pc, FunctionLibFunction function) throws PageException {
Struct sct = new StructImpl();
ArrayList<FunctionLibFunctionArg> args = function.getArg();
String filename = Caster.toString(args.get(0).getDefaultValue());
Key name = KeyImpl.toKey(args.get(1).getDefaultValue());
boolean isWeb = Caster.toBooleanValue(args.get(2).getDefaultValue());
UDF udf = CFFunction.loadUDF(pc, filename, name, isWeb);
sct.set(KeyConstants._name, function.getName());
sct.set(ARGUMENT_TYPE, "fixed");
sct.set(KeyConstants._description, StringUtil.emptyIfNull(udf.getHint()));
sct.set(RETURN_TYPE, StringUtil.emptyIfNull(udf.getReturnTypeAsString()));
sct.set(KeyConstants._type, "cfml");
sct.set(SOURCE, udf.getSource());
sct.set(KeyConstants._status, "implemeted");
FunctionArgument[] fas = udf.getFunctionArguments();
Array _args = new ArrayImpl();
sct.set(KeyConstants._arguments, _args);
int min = 0, max = 0;
for (int i = 0; i < fas.length; i++) {
FunctionArgument fa = fas[i];
Struct meta = fa.getMetaData();
Struct _arg = new StructImpl();
if (fa.isRequired())
min++;
max++;
_arg.set(KeyConstants._required, fa.isRequired() ? Boolean.TRUE : Boolean.FALSE);
_arg.set(KeyConstants._type, StringUtil.emptyIfNull(fa.getTypeAsString()));
_arg.set(KeyConstants._name, StringUtil.emptyIfNull(fa.getName()));
_arg.set(KeyConstants._description, StringUtil.emptyIfNull(fa.getHint()));
String status;
if (meta == null)
status = "implemeted";
else
status = TagLibFactory.toStatus(TagLibFactory.toStatus(Caster.toString(meta.get(KeyConstants._status, "implemeted"))));
_arg.set(KeyConstants._status, status);
_args.append(_arg);
}
sct.set(ARG_MIN, Caster.toDouble(min));
sct.set(ARG_MAX, Caster.toDouble(max));
return sct;
}
Aggregations