Mercurial Hosting > luan
diff src/luan/interp/UpValue.java @ 32:c3eab5a3ce3c
implement closures
git-svn-id: https://luan-java.googlecode.com/svn/trunk@33 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 14 Dec 2012 05:40:35 +0000 |
parents | |
children | 0cdc1da466ee |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/interp/UpValue.java Fri Dec 14 05:40:35 2012 +0000 @@ -0,0 +1,54 @@ +package luan.interp; + + +final class UpValue { + private Object[] stack; + private int index; + + UpValue(Object[] stack,int index) { + this.stack = stack; + this.index = index; + } + + Object get() { + return stack[index]; + } + + void set(Object value) { + stack[index] = value; + } + + void close() { + stack = new Object[]{get()}; + index = 0; + } + + static interface Getter { + public UpValue get(LuaStateImpl lua); + } + + static final class StackGetter implements Getter { + private final int index; + + StackGetter(int index) { + this.index = index; + } + + public UpValue get(LuaStateImpl lua) { + return lua.getUpValue(index); + } + } + + static final class NestedGetter implements Getter { + private final int index; + + NestedGetter(int index) { + this.index = index; + } + + public UpValue get(LuaStateImpl lua) { + return lua.closure().upValues[index]; + } + } + +}