use of lucee.transformer.expression.literal.Literal in project Lucee by lucee.
the class AbstrCFMLExprTransformer method getFunctionMember.
/**
* Liest die Argumente eines Funktonsaufruf ein und prueft ob die Funktion
* innerhalb der FLD (Function Library Descriptor) definiert ist.
* Falls sie existiert wird die Funktion gegen diese geprueft und ein build-in-function CFXD Element generiert,
* ansonsten ein normales funcion-call Element.
* <br />
* EBNF:<br />
* <code>[impOp{"," impOp}];</code>
* @param name Identifier der Funktion als Zeichenkette
* @param checkLibrary Soll geprueft werden ob die Funktion innerhalb der Library existiert.
* @return CFXD Element
* @throws TemplateException
*/
private FunctionMember getFunctionMember(ExprData data, final ExprString name, boolean checkLibrary) throws TemplateException {
// get Function Library
checkLibrary = checkLibrary && data.flibs != null;
FunctionLibFunction flf = null;
if (checkLibrary) {
if (!(name instanceof Literal))
// should never happen!
throw new TemplateException(data.srcCode, "syntax error");
for (int i = 0; i < data.flibs.length; i++) {
flf = data.flibs[i].getFunction(((Literal) name).getString());
if (flf != null)
break;
}
if (flf == null) {
checkLibrary = false;
}
}
FunctionMember fm = null;
while (true) {
int pos = data.srcCode.getPos();
// Element Function
if (checkLibrary) {
BIF bif = new BIF(data.factory, data.settings, flf);
// TODO data.ep.add(flf, bif, data.srcCode);
bif.setArgType(flf.getArgType());
try {
bif.setClassDefinition(flf.getFunctionClassDefinition());
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
throw new PageRuntimeException(t);
}
bif.setReturnType(flf.getReturnTypeAsString());
fm = bif;
if (flf.getArgType() == FunctionLibFunction.ARG_DYNAMIC && flf.hasDefaultValues()) {
ArrayList<FunctionLibFunctionArg> args = flf.getArg();
Iterator<FunctionLibFunctionArg> it = args.iterator();
FunctionLibFunctionArg arg;
while (it.hasNext()) {
arg = it.next();
if (arg.getDefaultValue() != null)
bif.addArgument(new NamedArgument(data.factory.createLitString(arg.getName()), data.factory.createLitString(arg.getDefaultValue()), arg.getTypeAsString(), false));
}
}
} else {
fm = new UDF(name);
}
int count = getFunctionMemberAttrs(data, name, checkLibrary, fm, flf);
if (checkLibrary) {
// pre
if (flf.hasTteClass()) {
FunctionLibFunction tmp = flf.getEvaluator().pre((BIF) fm, flf);
if (tmp != null && tmp != flf) {
flf = tmp;
data.srcCode.setPos(pos);
continue;
}
}
// check max attributes
{
boolean isDynamic = flf.getArgType() == FunctionLibFunction.ARG_DYNAMIC;
int max = flf.getArgMax();
// Dynamic
if (isDynamic) {
if (max != -1 && max < fm.getArguments().length)
throw new TemplateException(data.srcCode, "too many Attributes (" + max + ":" + fm.getArguments().length + ") in function [ " + ASMUtil.display(name) + " ]");
} else // Fix
{
if (flf.getArg().size() < fm.getArguments().length) {
TemplateException te = new TemplateException(data.srcCode, "too many Attributes (" + flf.getArg().size() + ":" + fm.getArguments().length + ") in function call [" + ASMUtil.display(name) + "]");
UDFUtil.addFunctionDoc(te, flf);
throw te;
}
}
}
// check min attributes
if (flf.getArgMin() > count) {
TemplateException te = new TemplateException(data.srcCode, "too few attributes in function [" + ASMUtil.display(name) + "]");
if (flf.getArgType() == FunctionLibFunction.ARG_FIX)
UDFUtil.addFunctionDoc(te, flf);
throw te;
}
// evaluator
if (flf.hasTteClass()) {
flf.getEvaluator().execute((BIF) fm, flf);
}
}
comments(data);
if (checkLibrary)
data.ep.add(flf, (BIF) fm, data.srcCode);
break;
}
return fm;
}
use of lucee.transformer.expression.literal.Literal in project Lucee by lucee.
the class OpElvis method _writeOutPureDataMember.
public Type _writeOutPureDataMember(BytecodeContext bc, int mode) throws TransformerException {
// TODO use function isNull for this
GeneratorAdapter adapter = bc.getAdapter();
Label yes = new Label();
Label end = new Label();
List<Member> members = left.getMembers();
// to array
Iterator<Member> it = members.iterator();
List<DataMember> list = new ArrayList<DataMember>();
while (it.hasNext()) {
list.add((DataMember) it.next());
}
DataMember[] arr = list.toArray(new DataMember[members.size()]);
ExpressionUtil.visitLine(bc, left.getStart());
// public static boolean call(PageContext pc , double scope,String[] varNames)
// pc
adapter.loadArg(0);
// scope
adapter.push((double) left.getScope());
// varNames
// all literal string?
boolean allLiteral = true;
for (int i = 0; i < arr.length; i++) {
if (!(arr[i].getName() instanceof Literal))
allLiteral = false;
}
ArrayVisitor av = new ArrayVisitor();
if (!allLiteral) {
// String Array
av.visitBegin(adapter, Types.STRING, arr.length);
for (int i = 0; i < arr.length; i++) {
av.visitBeginItem(adapter, i);
arr[i].getName().writeOut(bc, MODE_REF);
av.visitEndItem(adapter);
}
} else {
// Collection.Key Array
av.visitBegin(adapter, Types.COLLECTION_KEY, arr.length);
for (int i = 0; i < arr.length; i++) {
av.visitBeginItem(adapter, i);
getFactory().registerKey(bc, arr[i].getName(), false);
av.visitEndItem(adapter);
}
}
av.visitEnd();
// allowNull
// adapter.push(false);
// ASMConstants.NULL(adapter);
// call IsDefined.invoke
adapter.invokeStatic(ELVIS, allLiteral ? INVOKE_KEY : INVOKE_STR);
ExpressionUtil.visitLine(bc, left.getEnd());
adapter.visitJumpInsn(Opcodes.IFEQ, yes);
// left
ExpressionUtil.visitLine(bc, left.getStart());
left.writeOut(bc, MODE_REF);
ExpressionUtil.visitLine(bc, left.getEnd());
adapter.visitJumpInsn(Opcodes.GOTO, end);
// right
ExpressionUtil.visitLine(bc, right.getStart());
adapter.visitLabel(yes);
right.writeOut(bc, MODE_REF);
ExpressionUtil.visitLine(bc, right.getEnd());
adapter.visitLabel(end);
return Types.OBJECT;
}
use of lucee.transformer.expression.literal.Literal in project Lucee by lucee.
the class TagFunction method createFunction.
private Function createFunction(Page page, Body body, RefBoolean isStatic, boolean defaultOutput) throws TransformerException {
Attribute attr;
LitString ANY = page.getFactory().createLitString("any");
LitString PUBLIC = page.getFactory().createLitString("public");
// name
Expression name = removeAttribute("name").getValue();
/*if(name instanceof LitString) {
((LitString)name).upperCase();
}*/
// return
attr = removeAttribute("returntype");
// if(attr==null) attr = getAttribute("return");
// if(attr==null) attr = getAttribute("type");
Expression returnType = (attr == null) ? ANY : attr.getValue();
// output
attr = removeAttribute("output");
Expression output = (attr == null) ? (defaultOutput ? page.getFactory().TRUE() : page.getFactory().TRUE()) : attr.getValue();
// bufferOutput
attr = removeAttribute("bufferoutput");
Expression bufferOutput = (attr == null) ? null : attr.getValue();
// modifier
isStatic.setValue(false);
int modifier = Component.MODIFIER_NONE;
attr = removeAttribute("modifier");
if (attr != null) {
Expression val = attr.getValue();
if (val instanceof Literal) {
Literal l = (Literal) val;
String str = StringUtil.emptyIfNull(l.getString()).trim();
if ("abstract".equalsIgnoreCase(str))
modifier = Component.MODIFIER_ABSTRACT;
else if ("final".equalsIgnoreCase(str))
modifier = Component.MODIFIER_FINAL;
else if ("static".equalsIgnoreCase(str))
isStatic.setValue(true);
}
}
// access
attr = removeAttribute("access");
Expression access = (attr == null) ? PUBLIC : attr.getValue();
// dspLabel
attr = removeAttribute("displayname");
Expression displayname = (attr == null) ? page.getFactory().EMPTY() : attr.getValue();
// hint
attr = removeAttribute("hint");
Expression hint = (attr == null) ? page.getFactory().EMPTY() : attr.getValue();
// description
attr = removeAttribute("description");
Expression description = (attr == null) ? page.getFactory().EMPTY() : attr.getValue();
// returnformat
attr = removeAttribute("returnformat");
Expression returnFormat = (attr == null) ? null : attr.getValue();
// secureJson
attr = removeAttribute("securejson");
Expression secureJson = (attr == null) ? null : attr.getValue();
// verifyClient
attr = removeAttribute("verifyclient");
Expression verifyClient = (attr == null) ? null : attr.getValue();
// localMode
attr = removeAttribute("localmode");
Expression localMode = (attr == null) ? null : attr.getValue();
// cachedWithin
Literal cachedWithin = null;
attr = removeAttribute("cachedwithin");
if (attr != null) {
Expression val = attr.getValue();
if (val instanceof Literal)
cachedWithin = ((Literal) val);
}
String strAccess = ((LitString) access).getString();
int acc = ComponentUtil.toIntAccess(strAccess, -1);
if (acc == -1)
throw new TransformerException("invalid access type [" + strAccess + "], access types are remote, public, package, private", getStart());
Function func = new FunctionImpl(page, name, returnType, returnFormat, output, bufferOutput, acc, displayname, description, hint, secureJson, verifyClient, localMode, cachedWithin, modifier, body, getStart(), getEnd());
// %**%
Map attrs = getAttributes();
Iterator it = attrs.entrySet().iterator();
HashMap<String, Attribute> metadatas = new HashMap<String, Attribute>();
while (it.hasNext()) {
attr = (Attribute) ((Map.Entry) it.next()).getValue();
metadatas.put(attr.getName(), attr);
}
func.setMetaData(metadatas);
return func;
}
use of lucee.transformer.expression.literal.Literal in project Lucee by lucee.
the class ASMUtil method removeLiterlChildren.
public static void removeLiterlChildren(Tag tag, boolean recursive) {
Body body = tag.getBody();
if (body != null) {
List<Statement> list = body.getStatements();
Statement[] stats = list.toArray(new Statement[list.size()]);
PrintOut po;
Tag t;
for (int i = 0; i < stats.length; i++) {
if (stats[i] instanceof PrintOut) {
po = (PrintOut) stats[i];
if (po.getExpr() instanceof Literal) {
body.getStatements().remove(po);
}
} else if (recursive && stats[i] instanceof Tag) {
t = (Tag) stats[i];
if (t.getTagLibTag().isAllowRemovingLiteral()) {
removeLiterlChildren(t, recursive);
}
}
}
}
}
use of lucee.transformer.expression.literal.Literal in project Lucee by lucee.
the class SourceLastModifiedClassAdapter method _createMetaDataStruct.
private static void _createMetaDataStruct(BytecodeContext bc, GeneratorAdapter adapter, int sct, Map attrs) throws TransformerException {
Attribute attr;
Iterator it = attrs.entrySet().iterator();
Entry entry;
while (it.hasNext()) {
entry = (Map.Entry) it.next();
attr = (Attribute) entry.getValue();
adapter.loadLocal(sct);
adapter.push(attr.getName());
if (attr.getValue() instanceof Literal)
ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_REF);
else
adapter.push("[runtime expression]");
adapter.invokeVirtual(Types.STRUCT_IMPL, SET_EL);
adapter.pop();
}
}
Aggregations