Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
31:5cf15507d77e | 32:c3eab5a3ce3c |
---|---|
1 package luan.interp; | |
2 | |
3 | |
4 final class UpValue { | |
5 private Object[] stack; | |
6 private int index; | |
7 | |
8 UpValue(Object[] stack,int index) { | |
9 this.stack = stack; | |
10 this.index = index; | |
11 } | |
12 | |
13 Object get() { | |
14 return stack[index]; | |
15 } | |
16 | |
17 void set(Object value) { | |
18 stack[index] = value; | |
19 } | |
20 | |
21 void close() { | |
22 stack = new Object[]{get()}; | |
23 index = 0; | |
24 } | |
25 | |
26 static interface Getter { | |
27 public UpValue get(LuaStateImpl lua); | |
28 } | |
29 | |
30 static final class StackGetter implements Getter { | |
31 private final int index; | |
32 | |
33 StackGetter(int index) { | |
34 this.index = index; | |
35 } | |
36 | |
37 public UpValue get(LuaStateImpl lua) { | |
38 return lua.getUpValue(index); | |
39 } | |
40 } | |
41 | |
42 static final class NestedGetter implements Getter { | |
43 private final int index; | |
44 | |
45 NestedGetter(int index) { | |
46 this.index = index; | |
47 } | |
48 | |
49 public UpValue get(LuaStateImpl lua) { | |
50 return lua.closure().upValues[index]; | |
51 } | |
52 } | |
53 | |
54 } |