From c6518a92434c150c8b99914c049f835dfd9850db Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Wed, 30 Nov 2011 05:15:56 +0100 Subject: [PATCH] Added a way to retrieve the current expression instance. --- .../worldedit/expression/Expression.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/main/java/com/sk89q/worldedit/expression/Expression.java b/src/main/java/com/sk89q/worldedit/expression/Expression.java index 5ee202544..7d5ffa55f 100644 --- a/src/main/java/com/sk89q/worldedit/expression/Expression.java +++ b/src/main/java/com/sk89q/worldedit/expression/Expression.java @@ -22,6 +22,7 @@ package com.sk89q.worldedit.expression; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Stack; import com.sk89q.worldedit.expression.lexer.Lexer; import com.sk89q.worldedit.expression.lexer.tokens.Token; @@ -57,6 +58,8 @@ import com.sk89q.worldedit.expression.runtime.Variable; * @author TomyLobo */ public class Expression { + private static final ThreadLocal> instance = new ThreadLocal>(); + private final Map variables = new HashMap(); private final String[] variableNames; private RValue root; @@ -98,10 +101,13 @@ public class Expression { ((Variable) invokable).value = values[i]; } + pushInstance(); try { return root.getValue(); } catch (ReturnException e) { return e.getValue(); + } finally { + popInstance(); } } @@ -122,4 +128,27 @@ public class Expression { return variable; } + + public static Expression getInstance() { + return instance.get().peek(); + } + + private void pushInstance() { + Stack foo = instance.get(); + if (foo == null) { + instance.set(foo = new Stack()); + } + + foo.push(this); + } + + private void popInstance() { + Stack foo = instance.get(); + + foo.pop(); + + if (foo.isEmpty()) { + instance.set(null); + } + } }