Mercurial Hosting > luan
comparison src/luan/interp/LuaStateImpl.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 | 5cf15507d77e |
children | 0cdc1da466ee |
comparison
equal
deleted
inserted
replaced
31:5cf15507d77e | 32:c3eab5a3ce3c |
---|---|
6 | 6 |
7 | 7 |
8 final class LuaStateImpl implements LuaState { | 8 final class LuaStateImpl implements LuaState { |
9 private final LuaTable env = new LuaTable(); | 9 private final LuaTable env = new LuaTable(); |
10 | 10 |
11 public LuaTable env() { | 11 @Override public LuaTable env() { |
12 return env; | 12 return env; |
13 } | 13 } |
14 | 14 |
15 | 15 |
16 private static class LuaStack { | 16 private static class Frame { |
17 final LuaStack previousStack; | 17 final Frame previousFrame; |
18 final Object[] a; | 18 final LuaClosure closure; |
19 final Object[] stack; | |
19 final Object[] varArgs; | 20 final Object[] varArgs; |
21 UpValue[] downValues = null; | |
20 | 22 |
21 LuaStack( LuaStack previousStack, int stackSize, Object[] varArgs) { | 23 Frame( Frame previousFrame, LuaClosure closure, int stackSize, Object[] varArgs) { |
22 this.previousStack = previousStack; | 24 this.previousFrame = previousFrame; |
23 this.a = new Object[stackSize]; | 25 this.closure = closure; |
26 this.stack = new Object[stackSize]; | |
24 this.varArgs = varArgs; | 27 this.varArgs = varArgs; |
28 } | |
29 | |
30 void stackClear(int start,int end) { | |
31 if( downValues != null ) { | |
32 for( int i=start; i<end; i++ ) { | |
33 UpValue downValue = downValues[i]; | |
34 if( downValue != null ) { | |
35 downValue.close(); | |
36 downValues[i] = null; | |
37 } | |
38 } | |
39 } | |
40 for( int i=start; i<end; i++ ) { | |
41 stack[i] = null; | |
42 } | |
43 } | |
44 | |
45 UpValue getUpValue(int index) { | |
46 if( downValues==null ) | |
47 downValues = new UpValue[stack.length]; | |
48 if( downValues[index] == null ) | |
49 downValues[index] = new UpValue(stack,index); | |
50 return downValues[index]; | |
25 } | 51 } |
26 } | 52 } |
27 | 53 |
28 private LuaStack stack = null; | 54 private Frame frame = null; |
29 public Object[] returnValues; | 55 Object[] returnValues; |
30 public LuaClosure tailFn; | 56 LuaClosure tailFn; |
31 | 57 |
32 Object[] newStack(int stackSize, Object[] varArgs) { | 58 // returns stack |
33 stack = new LuaStack(stack,stackSize,varArgs); | 59 Object[] newFrame(LuaClosure closure, int stackSize, Object[] varArgs) { |
34 return stack.a; | 60 frame = new Frame(frame,closure,stackSize,varArgs); |
61 return frame.stack; | |
35 } | 62 } |
36 | 63 |
37 void popStack() { | 64 void popFrame() { |
38 returnValues = LuaFunction.EMPTY_RTN; | 65 returnValues = LuaFunction.EMPTY_RTN; |
39 tailFn = null; | 66 tailFn = null; |
40 stack = stack.previousStack; | 67 frame = frame.previousFrame; |
41 } | 68 } |
42 | 69 |
43 public Object[] stack() { | 70 Object stackGet(int index) { |
44 return stack.a; | 71 return frame.stack[index]; |
45 } | 72 } |
46 | 73 |
47 public Object[] varArgs() { | 74 void stackSet(int index,Object value) { |
48 return stack.varArgs; | 75 frame.stack[index] = value; |
76 } | |
77 | |
78 void stackClear(int start,int end) { | |
79 frame.stackClear(start,end); | |
80 } | |
81 | |
82 Object[] varArgs() { | |
83 return frame.varArgs; | |
84 } | |
85 | |
86 LuaClosure closure() { | |
87 return frame.closure; | |
88 } | |
89 | |
90 UpValue getUpValue(int index) { | |
91 return frame.getUpValue(index); | |
49 } | 92 } |
50 } | 93 } |