use of lucee.transformer.expression.literal.Literal in project Lucee by lucee.
the class Function method toLocalMode.
public static ExprInt toLocalMode(Expression expr, ExprInt defaultValue) {
int mode = -1;
if (expr instanceof Literal) {
String str = ((Literal) expr).getString();
str = str.trim().toLowerCase();
mode = AppListenerUtil.toLocalMode(str, -1);
}
if (mode == -1)
return defaultValue;
return expr.getFactory().createLitInteger(mode);
}
use of lucee.transformer.expression.literal.Literal in project Lucee by lucee.
the class Function method addAttribute.
public final void addAttribute(Attribute attr) throws TemplateException {
String name = attr.getName().toLowerCase();
// name
if ("name".equals(name)) {
throw new TransformerException("name cannot be defined twice", getStart());
} else if ("returntype".equals(name)) {
this.returnType = toLitString(name, attr.getValue());
} else if ("access".equals(name)) {
LitString ls = toLitString(name, attr.getValue());
String strAccess = ls.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());
access = acc;
} else if ("output".equals(name))
this.output = toLitBoolean(name, attr.getValue());
else if ("bufferoutput".equals(name))
this.bufferOutput = toLitBoolean(name, attr.getValue());
else if ("displayname".equals(name))
this.displayName = toLitString(name, attr.getValue());
else if ("hint".equals(name))
this.hint = toLitString(name, attr.getValue());
else if ("description".equals(name))
this.description = toLitString(name, attr.getValue());
else if ("returnformat".equals(name))
this.returnFormat = toLitString(name, attr.getValue());
else if ("securejson".equals(name))
this.secureJson = toLitBoolean(name, attr.getValue());
else if ("verifyclient".equals(name))
this.verifyClient = toLitBoolean(name, attr.getValue());
else if ("localmode".equals(name)) {
Expression v = attr.getValue();
if (v != null) {
String str = ASMUtil.toString(v, null);
if (!StringUtil.isEmpty(str)) {
int mode = AppListenerUtil.toLocalMode(str, -1);
if (mode != -1)
this.localMode = v.getFactory().createLitInteger(mode);
else
throw new TransformerException("Attribute localMode of the Tag Function, must be a literal value (modern, classic, true or false)", getStart());
}
}
} else if ("cachedwithin".equals(name)) {
try {
// ASMUtil.timeSpanToLong(attr.getValue());
this.cachedWithin = ASMUtil.cachedWithinValue(attr.getValue());
} catch (EvaluatorException e) {
throw new TemplateException(e.getMessage());
}
} else if ("modifier".equals(name)) {
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 {
// needed for testing
toLitString(name, attr.getValue());
if (metadata == null)
metadata = new HashMap<String, Attribute>();
metadata.put(attr.getName(), attr);
}
}
use of lucee.transformer.expression.literal.Literal in project Lucee by lucee.
the class ASMUtil method cachedWithinValue.
public static Literal cachedWithinValue(Expression val) throws EvaluatorException {
if (val instanceof Literal) {
Literal l = (Literal) val;
// double == days
Double d = l.getDouble(null);
if (d != null) {
return val.getFactory().createLitLong(TimeSpanImpl.fromDays(d.doubleValue()).getMillis(), null, null);
}
return l;
} else // createTimespan
if (val instanceof Variable) {
Variable var = (Variable) val;
if (var.getMembers().size() == 1) {
Member first = var.getFirstMember();
if (first instanceof BIF) {
BIF bif = (BIF) first;
if ("createTimeSpan".equalsIgnoreCase(bif.getFlf().getName())) {
Argument[] args = bif.getArguments();
int len = ArrayUtil.size(args);
if (len >= 4 && len <= 5) {
double days = toDouble(args[0].getValue());
double hours = toDouble(args[1].getValue());
double minutes = toDouble(args[2].getValue());
double seconds = toDouble(args[3].getValue());
double millis = len == 5 ? toDouble(args[4].getValue()) : 0;
return val.getFactory().createLitLong(new TimeSpanImpl((int) days, (int) hours, (int) minutes, (int) seconds, (int) millis).getMillis(), null, null);
}
}
}
}
}
throw cacheWithinException();
}
use of lucee.transformer.expression.literal.Literal in project Lucee by lucee.
the class BytecodeFactory method registerKey.
@Override
public void registerKey(Context c, Expression name, boolean doUpperCase) throws TransformerException {
BytecodeContext bc = (BytecodeContext) c;
if (name instanceof Literal) {
Literal l = (Literal) name;
LitString ls = name instanceof LitString ? (LitString) l : c.getFactory().createLitString(l.getString());
if (doUpperCase) {
ls = ls.duplicate();
ls.upperCase();
}
String key = KeyConstants.getFieldName(ls.getString());
if (key != null) {
bc.getAdapter().getStatic(KEY_CONSTANTS, key, Types.COLLECTION_KEY);
return;
}
int index = bc.registerKey(ls);
bc.getAdapter().visitVarInsn(Opcodes.ALOAD, 0);
bc.getAdapter().visitFieldInsn(Opcodes.GETFIELD, bc.getClassName(), "keys", Types.COLLECTION_KEY_ARRAY.toString());
bc.getAdapter().push(index);
bc.getAdapter().visitInsn(Opcodes.AALOAD);
return;
}
name.writeOut(bc, Expression.MODE_REF);
bc.getAdapter().invokeStatic(Page.KEY_IMPL, INIT);
// bc.getAdapter().invokeStatic(Types.CASTER, TO_KEY);
return;
}
use of lucee.transformer.expression.literal.Literal in project Lucee by lucee.
the class OpBool method toExprBoolean.
/**
* Create a String expression from a Expression
* @param left
* @param right
*
* @return String expression
* @throws TemplateException
*/
public static ExprBoolean toExprBoolean(Expression left, Expression right, int operation) {
if (left instanceof Literal && right instanceof Literal) {
Boolean l = ((Literal) left).getBoolean(null);
Boolean r = ((Literal) right).getBoolean(null);
if (l != null && r != null) {
switch(operation) {
case Factory.OP_BOOL_AND:
return left.getFactory().createLitBoolean(l.booleanValue() && r.booleanValue(), left.getStart(), right.getEnd());
case Factory.OP_BOOL_OR:
return left.getFactory().createLitBoolean(l.booleanValue() || r.booleanValue(), left.getStart(), right.getEnd());
case Factory.OP_BOOL_XOR:
return left.getFactory().createLitBoolean(l.booleanValue() ^ r.booleanValue(), left.getStart(), right.getEnd());
}
}
}
return new OpBool(left, right, operation);
}
Aggregations