comparison src/luan/impl/LuanStateImpl.java @ 166:4eaee12f6c65

move luan/interp to impl git-svn-id: https://luan-java.googlecode.com/svn/trunk@167 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 22 Jun 2014 04:17:38 +0000
parents src/luan/interp/LuanStateImpl.java@14281d5bd36f
children
comparison
equal deleted inserted replaced
165:94bbc4cbc106 166:4eaee12f6c65
1 package luan.impl;
2
3 import java.util.List;
4 import java.util.ArrayList;
5 import java.util.Map;
6 import java.util.HashMap;
7 import luan.Luan;
8 import luan.LuanState;
9 import luan.LuanTable;
10 import luan.LuanFunction;
11 import luan.MetatableGetter;
12 import luan.LuanException;
13 import luan.LuanElement;
14 import luan.DeepCloner;
15
16
17 final class LuanStateImpl extends LuanState {
18
19 private static class Frame {
20 final Frame previousFrame;
21 final Closure closure;
22 final Object[] stack;
23 final Object[] varArgs;
24 UpValue[] downValues = null;
25
26 Frame( Frame previousFrame, Closure closure, int stackSize, Object[] varArgs) {
27 this.previousFrame = previousFrame;
28 this.closure = closure;
29 this.stack = new Object[stackSize];
30 this.varArgs = varArgs;
31 }
32
33 void stackClear(int start,int end) {
34 if( downValues != null ) {
35 for( int i=start; i<end; i++ ) {
36 UpValue downValue = downValues[i];
37 if( downValue != null ) {
38 downValue.close();
39 downValues[i] = null;
40 }
41 }
42 }
43 for( int i=start; i<end; i++ ) {
44 stack[i] = null;
45 }
46 }
47
48 UpValue getUpValue(int index) {
49 if( downValues==null )
50 downValues = new UpValue[stack.length];
51 if( downValues[index] == null )
52 downValues[index] = new UpValue(stack,index);
53 return downValues[index];
54 }
55 }
56
57 private Frame frame = null;
58 Object returnValues;
59 Closure tailFn;
60 Map<UpValue.EnvGetter,UpValue> envs = new HashMap<UpValue.EnvGetter,UpValue>();
61
62 LuanStateImpl() {}
63
64 private LuanStateImpl(LuanStateImpl luan) {
65 super(luan);
66 }
67
68 @Override public LuanState shallowClone() {
69 // if( frame != null )
70 // throw new IllegalStateException("frame isn't null");
71 return new LuanStateImpl(this);
72 }
73
74 @Override public void deepenClone(LuanState clone,DeepCloner cloner) {
75 super.deepenClone(clone,cloner);
76 LuanStateImpl cloneImpl = (LuanStateImpl)clone;
77 cloneImpl.envs = new HashMap<UpValue.EnvGetter,UpValue>();
78 for( Map.Entry<UpValue.EnvGetter,UpValue> entry : envs.entrySet() ) {
79 cloneImpl.envs.put( entry.getKey(), cloner.deepClone(entry.getValue()) );
80 }
81 }
82
83 // returns stack
84 Object[] newFrame(Closure closure, int stackSize, Object[] varArgs) {
85 returnValues = LuanFunction.NOTHING;
86 tailFn = null;
87 frame = new Frame(frame,closure,stackSize,varArgs);
88 return frame.stack;
89 }
90
91 void popFrame() {
92 returnValues = LuanFunction.NOTHING;
93 tailFn = null;
94 frame = frame.previousFrame;
95 }
96
97 Object stackGet(int index) {
98 return frame.stack[index];
99 }
100
101 void stackSet(int index,Object value) {
102 frame.stack[index] = value;
103 }
104
105 void stackClear(int start,int end) {
106 frame.stackClear(start,end);
107 }
108
109 Object[] varArgs() {
110 return frame.varArgs;
111 }
112
113 Closure closure() {
114 return frame.closure;
115 }
116
117 UpValue getUpValue(int index) {
118 return frame.getUpValue(index);
119 }
120
121 UpValue getUpValue(UpValue.EnvGetter getter) throws LuanException {
122 UpValue uv = envs.get(getter);
123 if( uv == null ) {
124 LuanTable env = new LuanTable();
125 uv = new UpValue(env);
126 envs.put(getter,uv);
127 }
128 return uv;
129 }
130
131 @Override public LuanTable currentEnvironment() {
132 if( frame==null )
133 return null;
134 return (LuanTable)frame.closure.upValues()[0].get();
135 }
136
137 }