use of lucee.transformer.bytecode.BodyBase in project Lucee by lucee.
the class TagTry method _writeOut.
@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
final GeneratorAdapter adapter = bc.getAdapter();
adapter.visitLabel(begin);
Body tryBody = new BodyBase(getFactory());
List<Tag> catches = new ArrayList<Tag>();
Tag tmpFinal = null;
tryBody.setParent(getBody().getParent());
List<Statement> statements = getBody().getStatements();
Statement stat;
Tag tag;
{
Iterator<Statement> it = statements.iterator();
while (it.hasNext()) {
stat = it.next();
if (stat instanceof Tag) {
tag = (Tag) stat;
if (tag.getTagLibTag().getTagClassDefinition().isClassNameEqualTo("lucee.runtime.tag.Catch")) {
catches.add(tag);
continue;
} else if (tag.getTagLibTag().getTagClassDefinition().isClassNameEqualTo("lucee.runtime.tag.Finally")) {
tmpFinal = tag;
continue;
}
}
tryBody.addStatement(stat);
}
;
}
final Tag _finally = tmpFinal;
// has no try body, if there is no try body, no catches are executed, only finally
if (!tryBody.hasStatements()) {
if (_finally != null && _finally.getBody() != null) {
BodyBase.writeOut(bc, _finally.getBody());
// ExpressionUtil.writeOut(_finally.getBody(), bc);
}
return;
}
final int old = adapter.newLocal(Types.PAGE_EXCEPTION);
adapter.loadArg(0);
adapter.invokeVirtual(Types.PAGE_CONTEXT, GET_CATCH);
adapter.storeLocal(old);
TryCatchFinallyVisitor tcfv = new TryCatchFinallyVisitor(new OnFinally() {
@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
adapter.loadArg(0);
adapter.loadLocal(old);
adapter.invokeVirtual(Types.PAGE_CONTEXT, SET_CATCH_PE);
if (_finally != null) {
ExpressionUtil.visitLine(bc, _finally.getStart());
BodyBase.writeOut(bc, _finally.getBody());
// ExpressionUtil.writeOut(_finally.getBody(), bc);
}
}
}, getFlowControlFinal());
// Try
tcfv.visitTryBegin(bc);
BodyBase.writeOut(bc, tryBody);
// ExpressionUtil.writeOut(tryBody, bc);
int e = tcfv.visitTryEndCatchBeging(bc);
// if(e instanceof lucee.runtime.exp.Abort) throw e;
Label abortEnd = new Label();
adapter.loadLocal(e);
// Abort.isAbort(t);
adapter.invokeStatic(Types.ABORT, TryCatchFinally.IS_ABORT);
// adapter.instanceOf(Types.ABORT);
adapter.ifZCmp(Opcodes.IFEQ, abortEnd);
adapter.loadLocal(e);
adapter.throwException();
adapter.visitLabel(abortEnd);
// PageExceptionImpl old=pc.getCatch();
// PageException pe=Caster.toPageEception(e);
int pe = adapter.newLocal(Types.PAGE_EXCEPTION);
adapter.loadLocal(e);
adapter.invokeStatic(Types.CASTER, TO_PAGE_EXCEPTION);
adapter.storeLocal(pe);
Iterator<Tag> it = catches.iterator();
Attribute attrType;
Expression type;
Label endAllIfs = new Label();
Tag tagElse = null;
while (it.hasNext()) {
tag = it.next();
Label endIf = new Label();
attrType = tag.getAttribute("type");
type = bc.getFactory().createLitString("any");
if (attrType != null)
type = attrType.getValue();
if (type instanceof LitString && ((LitString) type).getString().equalsIgnoreCase("any")) {
tagElse = tag;
continue;
}
ExpressionUtil.visitLine(bc, tag.getStart());
// if(pe.typeEqual(@type)
adapter.loadLocal(pe);
type.writeOut(bc, Expression.MODE_REF);
adapter.invokeVirtual(Types.PAGE_EXCEPTION, TYPE_EQUAL);
adapter.ifZCmp(Opcodes.IFEQ, endIf);
catchBody(bc, adapter, tag, pe, true, true);
adapter.visitJumpInsn(Opcodes.GOTO, endAllIfs);
adapter.visitLabel(endIf);
}
// else
if (tagElse != null) {
catchBody(bc, adapter, tagElse, pe, true, true);
} else {
// pc.setCatch(pe,true);
adapter.loadArg(0);
adapter.loadLocal(pe);
adapter.push(false);
adapter.push(true);
adapter.invokeVirtual(Types.PAGE_CONTEXT, SET_CATCH3);
// throw pe;
adapter.loadLocal(pe);
adapter.throwException();
}
adapter.visitLabel(endAllIfs);
// PageExceptionImpl old=pc.getCatch();
tcfv.visitCatchEnd(bc);
}
use of lucee.transformer.bytecode.BodyBase in project Lucee by lucee.
the class TagTry2 method _writeOut.
@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
GeneratorAdapter adapter = bc.getAdapter();
adapter.visitLabel(begin);
Body tryBody = new BodyBase(getFactory());
List<Tag> catches = new ArrayList<Tag>();
Tag tmpFinal = null;
tryBody.setParent(getBody().getParent());
List<Statement> statements = getBody().getStatements();
Statement stat;
Tag tag;
{
Iterator<Statement> it = statements.iterator();
while (it.hasNext()) {
stat = it.next();
if (stat instanceof Tag) {
tag = (Tag) stat;
if (tag.getTagLibTag().getTagClassDefinition().isClassNameEqualTo("lucee.runtime.tag.Catch")) {
catches.add(tag);
continue;
} else if (tag.getTagLibTag().getTagClassDefinition().isClassNameEqualTo("lucee.runtime.tag.Finally")) {
tmpFinal = tag;
continue;
}
}
tryBody.addStatement(stat);
}
;
}
final Tag _finally = tmpFinal;
// has no try body, if there is no try body, no catches are executed, only finally
if (!tryBody.hasStatements()) {
if (_finally != null && _finally.getBody() != null) {
BodyBase.writeOut(bc, _finally.getBody());
// ExpressionUtil.writeOut(_finally.getBody(), bc);
}
return;
}
TryCatchFinallyVisitor tcfv = new TryCatchFinallyVisitor(new OnFinally() {
@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
if (_finally != null) {
ExpressionUtil.visitLine(bc, _finally.getStart());
BodyBase.writeOut(bc, _finally.getBody());
// ExpressionUtil.writeOut(_finally.getBody(), bc);
}
}
}, getFlowControlFinal());
// Try
tcfv.visitTryBegin(bc);
BodyBase.writeOut(bc, tryBody);
// ExpressionUtil.writeOut(tryBody, bc);
int e = tcfv.visitTryEndCatchBeging(bc);
// if(e instanceof lucee.runtime.exp.Abort) throw e;
Label abortEnd = new Label();
adapter.loadLocal(e);
// Abort.isAbort(t);
adapter.invokeStatic(Types.ABORT, TryCatchFinally.IS_ABORT);
// adapter.instanceOf(Types.ABORT);
adapter.ifZCmp(Opcodes.IFEQ, abortEnd);
adapter.loadLocal(e);
adapter.throwException();
adapter.visitLabel(abortEnd);
// PageExceptionImpl old=pc.getCatch();
int oldPE = adapter.newLocal(Types.PAGE_EXCEPTION);
int oldName = adapter.newLocal(Types.STRING);
adapter.loadArg(0);
adapter.invokeVirtual(Types.PAGE_CONTEXT, GET_CATCH);
adapter.storeLocal(oldPE);
adapter.loadArg(0);
adapter.checkCast(Types.PAGE_CONTEXT_IMPL);
adapter.invokeVirtual(Types.PAGE_CONTEXT_IMPL, GET_CATCH_NAME);
adapter.storeLocal(oldName);
// PageException pe=Caster.toPageEception(e);
int pe = adapter.newLocal(Types.PAGE_EXCEPTION);
adapter.loadLocal(e);
adapter.invokeStatic(Types.CASTER, TO_PAGE_EXCEPTION);
adapter.storeLocal(pe);
Iterator<Tag> it = catches.iterator();
Attribute attrType;
Expression type;
Label endAllIfs = new Label();
Tag tagElse = null;
while (it.hasNext()) {
tag = it.next();
Label endIf = new Label();
// type
attrType = tag.getAttribute("type");
type = bc.getFactory().createLitString("any");
if (attrType != null)
type = attrType.getValue();
if (type instanceof LitString && ((LitString) type).getString().equalsIgnoreCase("any")) {
tagElse = tag;
continue;
}
ExpressionUtil.visitLine(bc, tag.getStart());
// if(pe.typeEqual(@type)
adapter.loadLocal(pe);
type.writeOut(bc, Expression.MODE_REF);
adapter.invokeVirtual(Types.PAGE_EXCEPTION, TYPE_EQUAL);
adapter.ifZCmp(Opcodes.IFEQ, endIf);
catchBody(bc, adapter, tag, pe, true, true, extractName(tag));
adapter.visitJumpInsn(Opcodes.GOTO, endAllIfs);
adapter.visitLabel(endIf);
}
// else
if (tagElse != null) {
catchBody(bc, adapter, tagElse, pe, true, true, extractName(tagElse));
} else {
// pc.setCatch(pe,true);
adapter.loadArg(0);
adapter.loadLocal(pe);
adapter.push(false);
adapter.push(true);
adapter.invokeVirtual(Types.PAGE_CONTEXT, SET_CATCH3);
// throw pe;
adapter.loadLocal(pe);
adapter.throwException();
}
adapter.visitLabel(endAllIfs);
adapter.loadLocal(oldName);
Label notNull = new Label();
adapter.visitJumpInsn(Opcodes.IFNONNULL, notNull);
// NULL
adapter.loadArg(0);
adapter.loadLocal(oldPE);
adapter.invokeVirtual(Types.PAGE_CONTEXT, SET_CATCH_PE);
Label end = new Label();
adapter.visitJumpInsn(Opcodes.GOTO, end);
adapter.visitLabel(notNull);
// NOT NULL
adapter.loadArg(0);
adapter.checkCast(Types.PAGE_CONTEXT_IMPL);
adapter.loadLocal(oldPE);
adapter.loadLocal(oldName);
adapter.invokeVirtual(Types.PAGE_CONTEXT_IMPL, SET_CATCH_PE2);
adapter.visitLabel(end);
tcfv.visitCatchEnd(bc);
}
use of lucee.transformer.bytecode.BodyBase in project Lucee by lucee.
the class CFMLTransformer method tag.
/**
* Liest einen Tag ein, prueft hierbei ob das Tag innerhalb einer der geladenen Tag-Lib existiert,
* ansonsten wird ein Tag einfach als literal-string aufgenommen.
* <br />
* EBNF:<br />
* <code>name-space identifier spaces attributes ("/>" | ">" [body "</" identifier spaces ">"]);(* Ob dem Tag ein Body und ein End-Tag folgt ist abhaengig von Definition des body-content in Tag-Lib, gleices gilt fuer appendix *)</code>
* @param parent uebergeornetes Tag
* @param parseExpression sollen Expresson innerhalb des Body geparste werden oder nicht.
* @return Gibt zurueck ob es sich um ein Tag as einer Tag-Lib handelte oder nicht.
* @throws TemplateException
*/
private boolean tag(TagData data, Body parent, boolean parseExpression) throws TemplateException {
// lucee.print.ln("--->"+data.cfml.getCurrent());
boolean hasBody = false;
Position line = data.srcCode.getPosition();
// int column=data.cfml.getColumn();
int start = data.srcCode.getPos();
data.srcCode.next();
// read in namespace of tag
TagLib tagLib = nameSpace(data);
// return if no matching tag lib
if (tagLib == null) {
data.srcCode.previous();
return false;
}
// Get matching tag from tag lib
String strNameNormal = identifier(data.srcCode, false, true);
if (strNameNormal == null) {
data.srcCode.setPos((data.srcCode.getPos() - tagLib.getNameSpaceAndSeparator().length()) - 1);
return false;
}
String strName = strNameNormal.toLowerCase();
String appendix = null;
TagLibTag tagLibTag = tagLib.getTag(strName);
// get taglib
if (tagLibTag == null) {
tagLibTag = tagLib.getAppendixTag(strName);
if (tagLibTag == null) {
if (tagLib.getIgnoreUnknowTags()) {
data.srcCode.setPos(start);
return false;
}
throw new TemplateException(data.srcCode, "undefined tag [" + tagLib.getNameSpaceAndSeparator() + strName + "]");
}
appendix = StringUtil.removeStartingIgnoreCase(strNameNormal, tagLibTag.getName());
}
// CFXD Element
Tag tag;
try {
tag = tagLibTag.getTag(data.factory, line, data.srcCode.getPosition());
} catch (Exception e) {
throw new TemplateException(data.srcCode, e);
}
parent.addStatement(tag);
// get tag from tag library
if (appendix != null) {
tag.setAppendix(appendix);
tag.setFullname(tagLibTag.getFullName().concat(appendix));
} else {
tag.setFullname(tagLibTag.getFullName());
}
// if(tag.getFullname().equalsIgnoreCase("cfcomponent"))data.page.setIsComponent(true); // MUST to hardcoded, to better
// else if(tag.getFullname().equalsIgnoreCase("cfinterface"))data.page.setIsInterface(true); // MUST to hardcoded, to better
tag.setTagLibTag(tagLibTag);
comment(data.srcCode, true);
// Tag Translator Evaluator
if (tagLibTag.hasTTE()) {
data.ep.add(tagLibTag, tag, data.flibs, data.srcCode);
}
// get Attributes
attributes(data, tagLibTag, tag);
if (tagLibTag.hasAttributeEvaluator()) {
try {
tagLibTag = tagLibTag.getAttributeEvaluator().evaluate(tagLibTag, tag);
} catch (AttributeEvaluatorException e) {
throw new TemplateException(data.srcCode, e);
}
}
// TODO muss erlaubt sein
if (data.srcCode.forwardIfCurrent('>')) {
hasBody = tagLibTag.getHasBody();
} else if (data.srcCode.forwardIfCurrent('/', '>')) {
if (tagLibTag.getHasBody())
tag.setBody(new BodyBase(data.factory));
} else {
throw createTemplateException(data.srcCode, "tag [" + tagLibTag.getFullName() + "] is not closed", tagLibTag);
}
// Body
if (hasBody) {
// get Body
if (tagLibTag.isTagDependent()) {
// get TagDependentBodyTransformer
TagDependentBodyTransformer tdbt = null;
try {
tdbt = tagLibTag.getBodyTransformer();
} catch (TagLibException e) {
throw new TemplateException(data.srcCode, e);
}
if (tdbt == null)
throw createTemplateException(data.srcCode, "Tag dependent body Transformer is invalid for Tag [" + tagLibTag.getFullName() + "]", tagLibTag);
tag.setBody(tdbt.transform(data.factory, data.root, data.ep, data.tlibs, data.flibs, tagLibTag.getFullName(), data.scriptTags, data.srcCode, data.settings));
// get TagLib of end Tag
if (!data.srcCode.forwardIfCurrent("</")) {
// MUST this is a patch, do a more proper implementation
TemplateException te = new TemplateException(data.srcCode, "invalid construct");
if (tdbt instanceof CFMLScriptTransformer && ASMUtil.containsComponent(tag.getBody())) {
throw new CFMLScriptTransformer.ComponentTemplateException(te);
}
throw te;
}
TagLib tagLibEnd = nameSpace(data);
// same NameSpace
if (!(tagLibEnd != null && tagLibEnd.getNameSpaceAndSeparator().equals(tagLib.getNameSpaceAndSeparator())))
throw new TemplateException(data.srcCode, "invalid construct");
// get end Tag
String strNameEnd = identifier(data.srcCode, true, true).toLowerCase();
// not the same name Tag
if (!strName.equals(strNameEnd)) {
data.srcCode.setPos(start);
throw new TemplateException(data.srcCode, "Start and End Tag has not the same Name [" + tagLib.getNameSpaceAndSeparator() + strName + "-" + tagLibEnd.getNameSpaceAndSeparator() + strNameEnd + "]");
}
data.srcCode.removeSpace();
if (!data.srcCode.forwardIfCurrent('>'))
throw new TemplateException(data.srcCode, "End Tag [" + tagLibEnd.getNameSpaceAndSeparator() + strNameEnd + "] not closed");
} else {
// get body of Tag
BodyBase body = new BodyBase(data.factory);
body.setParent(tag);
// parseExpression=(tagLibTag.getParseBody())?true:parseExpression;
if (tagLibTag.getParseBody())
parseExpression = true;
while (true) {
// Load Expession Transformer from TagLib
ExprTransformer transfomer = null;
if (parseExpression) {
try {
transfomer = tagLibTag.getTagLib().getExprTransfomer();
} catch (TagLibException e) {
throw new TemplateException(data.srcCode, e);
}
}
// call body
body(data, body, parseExpression, transfomer);
// no End Tag
if (data.srcCode.isAfterLast()) {
if (tagLibTag.isBodyReq()) {
data.srcCode.setPos(start);
throw createTemplateException(data.srcCode, "No matching end tag found for tag [" + tagLibTag.getFullName() + "]", tagLibTag);
}
body.moveStatmentsTo(parent);
return executeEvaluator(data, tagLibTag, tag);
}
// Invalid Construct
int posBeforeEndTag = data.srcCode.getPos();
if (!data.srcCode.forwardIfCurrent('<', '/'))
throw createTemplateException(data.srcCode, "Missing end tag for [" + tagLibTag.getFullName() + "]", tagLibTag);
// get TagLib of end Tag
int _start = data.srcCode.getPos();
TagLib tagLibEnd = nameSpace(data);
// same NameSpace
if (tagLibEnd != null) {
String strNameEnd = "";
// lucee.print.ln(data.cfml.getLine()+" - "+data.cfml.getColumn()+" - "+tagLibEnd.getNameSpaceAndSeperator()+".equals("+tagLib.getNameSpaceAndSeperator()+")");
if (tagLibEnd.getNameSpaceAndSeparator().equals(tagLib.getNameSpaceAndSeparator())) {
// get end Tag
strNameEnd = identifier(data.srcCode, true, true).toLowerCase();
// not the same name Tag
// new part
data.srcCode.removeSpace();
if (strName.equals(strNameEnd)) {
if (!data.srcCode.forwardIfCurrent('>'))
throw new TemplateException(data.srcCode, "End Tag [" + tagLibEnd.getNameSpaceAndSeparator() + strNameEnd + "] not closed");
break;
}
}
// new part
if (tagLibTag.isBodyReq()) {
TagLibTag endTag = tagLibEnd.getTag(strNameEnd);
if (endTag != null && !endTag.getHasBody())
throw new TemplateException(data.srcCode, "End Tag [" + tagLibEnd.getNameSpaceAndSeparator() + strNameEnd + "] is not allowed, for this tag only a Start Tag is allowed");
data.srcCode.setPos(start);
if (tagLibEnd.getIgnoreUnknowTags() && (tagLibEnd.getTag(strNameEnd)) == null) {
data.srcCode.setPos(_start);
} else
throw new TemplateException(data.srcCode, "Start and End Tag has not the same Name [" + tagLib.getNameSpaceAndSeparator() + strName + "-" + tagLibEnd.getNameSpaceAndSeparator() + strNameEnd + "]");
} else {
body.moveStatmentsTo(parent);
data.srcCode.setPos(posBeforeEndTag);
return executeEvaluator(data, tagLibTag, tag);
}
// / new part
}
body.addPrintOut(data.factory, "</", null, null);
}
tag.setBody(body);
}
}
if (tag instanceof StatementBase)
((StatementBase) tag).setEnd(data.srcCode.getPosition());
return executeEvaluator(data, tagLibTag, tag);
}
use of lucee.transformer.bytecode.BodyBase in project Lucee by lucee.
the class Mail method evaluate.
@Override
public void evaluate(Tag tag, TagLibTag libTag) throws EvaluatorException {
if (tag.containsAttribute("query")) {
TagLib lib = libTag.getTagLib();
TagLibTag outputTag = lib.getTag("output");
TagOutput output = new TagOutput(tag.getFactory(), tag.getStart(), null);
output.setFullname(outputTag.getFullName());
output.setTagLibTag(outputTag);
output.addAttribute(new Attribute(false, "output", tag.getFactory().TRUE(), "boolean"));
output.addAttribute(new Attribute(false, "formail", tag.getFactory().TRUE(), "boolean"));
// output.getBody();
Body body = new BodyBase(tag.getFactory());
output.setBody(body);
ASMUtil.replace(tag, output, false);
body.addStatement(tag);
output.addAttribute(tag.removeAttribute("query"));
if (tag.containsAttribute("group"))
output.addAttribute(tag.removeAttribute("group"));
if (tag.containsAttribute("groupcasesensitive"))
output.addAttribute(tag.removeAttribute("groupcasesensitive"));
if (tag.containsAttribute("startrow"))
output.addAttribute(tag.removeAttribute("startrow"));
if (tag.containsAttribute("maxrows"))
output.addAttribute(tag.removeAttribute("maxrows"));
new Output().evaluate(output, outputTag);
}
}
use of lucee.transformer.bytecode.BodyBase in project Lucee by lucee.
the class AbstrCFMLScriptTransformer method __multiAttrStatement.
private final Tag __multiAttrStatement(Body parent, ExprData data, TagLibTag tlt) throws TemplateException {
if (data.ep == null)
return null;
String type = tlt.getName();
if (data.srcCode.forwardIfCurrent(type) || // lucee dialect support component as alias for class
(data.srcCode.getDialect() == CFMLEngine.DIALECT_LUCEE && type.equalsIgnoreCase(Constants.LUCEE_COMPONENT_TAG_NAME) && data.srcCode.forwardIfCurrent(Constants.CFML_COMPONENT_TAG_NAME))) {
boolean isValid = (data.srcCode.isCurrent(' ') || (tlt.getHasBody() && data.srcCode.isCurrent('{')));
if (!isValid) {
data.srcCode.setPos(data.srcCode.getPos() - type.length());
return null;
}
} else
return null;
Position line = data.srcCode.getPosition();
TagLibTagScript script = tlt.getScript();
// TagLibTag tlt = CFMLTransformer.getTLT(data.srcCode,type);
if (script.getContext() == CTX_CFC)
data.isCFC = true;
else if (script.getContext() == CTX_INTERFACE)
data.isInterface = true;
// Tag tag=new TagComponent(line);
Tag tag = getTag(data, parent, tlt, line, null);
tag.setTagLibTag(tlt);
tag.setScriptBase(true);
// add component meta data
if (data.isCFC) {
addMetaData(data, tag, IGNORE_LIST_COMPONENT);
}
if (data.isInterface) {
addMetaData(data, tag, IGNORE_LIST_INTERFACE);
}
// EvaluatorPool.getPool();
comments(data);
// attributes
// attributes(func,data);
Attribute[] attrs = attributes(tag, tlt, data, SEMI_BLOCK, data.factory.EMPTY(), script.getRtexpr() ? Boolean.TRUE : Boolean.FALSE, null, false, ',', false);
for (int i = 0; i < attrs.length; i++) {
tag.addAttribute(attrs[i]);
}
comments(data);
// body
if (tlt.getHasBody()) {
Body body = new BodyBase(data.factory);
boolean wasSemiColon = statement(data, body, script.getContext());
if (!wasSemiColon || !tlt.isBodyFree() || body.hasStatements())
tag.setBody(body);
} else
checkSemiColonLineFeed(data, true, true, true);
tag.setEnd(data.srcCode.getPosition());
eval(tlt, data, tag);
return tag;
}
Aggregations