Skip to content

Commit

Permalink
fixes NPE occured while invoking getTemplate().render(...) in functio…
Browse files Browse the repository at this point in the history
…ns/methods/tags (#6)
  • Loading branch information
subchen committed Dec 1, 2014
1 parent 5edf2ea commit fb97cbb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
Expand Up @@ -148,19 +148,29 @@ public void render(Map<String, Object> context, OutputStream out) {
}

private void doInterpret(Map<String, Object> context, JetWriter writer) {
InterpretContextImpl ctx = new InterpretContextImpl(engine, writer, context, securityManager);
// 如果在扩展函数,方法,tag 等里面,再次调用 getTemplate().render(),需要保持当前环境,在运行完之后,进行现场恢复
// see: https://github.com/subchen/jetbrick-template-2x/issues/6
InterpretContextImpl last = (InterpretContextImpl) InterpretContext.current();
try {
ctx.getTemplateStack().push(this);
ctx.getValueStack().push(option.getSymbols(), null, true);

astNode.execute(ctx);

ctx.getValueStack().pop();
ctx.getTemplateStack().pop();
} catch (InterpretException e) {
throw e.set(ctx.getSource());
InterpretContextImpl ctx = new InterpretContextImpl(engine, writer, context, securityManager);
try {
ctx.getTemplateStack().push(this);
ctx.getValueStack().push(option.getSymbols(), null, true);

astNode.execute(ctx);

ctx.getValueStack().pop();
ctx.getTemplateStack().pop();
} catch (InterpretException e) {
throw e.set(ctx.getSource());
} finally {
ctx.freeThreadLocal();
}
} finally {
ctx.remove();
// 进行现场恢复
if (last != null) {
last.setThreadLocal();
}
}
}

Expand Down
Expand Up @@ -45,10 +45,14 @@ public InterpretContextImpl(JetEngine engine, JetWriter writer, Map<String, Obje
this.valueStack = new ValueStack(engine.getGlobalContext(), context);
this.writer = writer;
this.signal = InterpretContext.SIGNAL_NONE;
setThreadLocal();
}

public void setThreadLocal() {
threadLocal.set(this);
}

public void remove() {
public void freeThreadLocal() {
threadLocal.remove();
}

Expand Down

0 comments on commit fb97cbb

Please sign in to comment.