Mercurial Hosting > luan
comparison core/src/luan/impl/LuanStateImpl.java @ 208:5ba136769034
remove MetatableGetter and use a generic metatable instead
git-svn-id: https://luan-java.googlecode.com/svn/trunk@209 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 08 Jul 2014 07:04:47 +0000 |
parents | 9fb218211763 |
children | a6bf8ff720f8 |
comparison
equal
deleted
inserted
replaced
207:5aafb5b9f70f | 208:5ba136769034 |
---|---|
6 import java.util.HashMap; | 6 import java.util.HashMap; |
7 import luan.Luan; | 7 import luan.Luan; |
8 import luan.LuanState; | 8 import luan.LuanState; |
9 import luan.LuanTable; | 9 import luan.LuanTable; |
10 import luan.LuanFunction; | 10 import luan.LuanFunction; |
11 import luan.MetatableGetter; | |
12 import luan.LuanException; | 11 import luan.LuanException; |
13 import luan.LuanElement; | 12 import luan.LuanElement; |
14 import luan.DeepCloner; | 13 import luan.DeepCloner; |
15 | 14 |
16 | 15 |
19 private static class Frame { | 18 private static class Frame { |
20 final Frame previousFrame; | 19 final Frame previousFrame; |
21 final Closure closure; | 20 final Closure closure; |
22 final Object[] stack; | 21 final Object[] stack; |
23 final Object[] varArgs; | 22 final Object[] varArgs; |
24 MtGetterLink mtGetterLink; | |
25 UpValue[] downValues = null; | 23 UpValue[] downValues = null; |
26 | 24 |
27 Frame( Frame previousFrame, Closure closure, int stackSize, Object[] varArgs) { | 25 Frame( Frame previousFrame, Closure closure, int stackSize, Object[] varArgs) { |
28 this.previousFrame = previousFrame; | 26 this.previousFrame = previousFrame; |
29 this.closure = closure; | 27 this.closure = closure; |
30 this.stack = new Object[stackSize]; | 28 this.stack = new Object[stackSize]; |
31 this.varArgs = varArgs; | 29 this.varArgs = varArgs; |
32 this.mtGetterLink = closure.mtGetterLink(); | |
33 } | 30 } |
34 | 31 |
35 void stackClear(int start,int end) { | 32 void stackClear(int start,int end) { |
36 if( downValues != null ) { | 33 if( downValues != null ) { |
37 for( int i=start; i<end; i++ ) { | 34 for( int i=start; i<end; i++ ) { |
52 downValues = new UpValue[stack.length]; | 49 downValues = new UpValue[stack.length]; |
53 if( downValues[index] == null ) | 50 if( downValues[index] == null ) |
54 downValues[index] = new UpValue(stack,index); | 51 downValues[index] = new UpValue(stack,index); |
55 return downValues[index]; | 52 return downValues[index]; |
56 } | 53 } |
57 | |
58 void addMetatableGetter(MetatableGetter mg) { | |
59 if( mtGetterLink==null || !mtGetterLink.contains(mg) ) | |
60 mtGetterLink = new MtGetterLink(mg,mtGetterLink); | |
61 } | |
62 } | 54 } |
63 | 55 |
64 private Frame frame = null; | 56 private Frame frame = null; |
65 Object returnValues; | 57 Object returnValues; |
66 Closure tailFn; | 58 Closure tailFn; |
67 MtGetterLink mtGetterLink = null; | |
68 | 59 |
69 LuanStateImpl() {} | 60 LuanStateImpl() {} |
70 | 61 |
71 private LuanStateImpl(LuanStateImpl luan) { | 62 private LuanStateImpl(LuanStateImpl luan) { |
72 super(luan); | 63 super(luan); |
87 } | 78 } |
88 | 79 |
89 void popFrame() { | 80 void popFrame() { |
90 returnValues = LuanFunction.NOTHING; | 81 returnValues = LuanFunction.NOTHING; |
91 tailFn = null; | 82 tailFn = null; |
92 mtGetterLink = frame.mtGetterLink; | |
93 frame = frame.previousFrame; | 83 frame = frame.previousFrame; |
94 } | 84 } |
95 | 85 |
96 Object stackGet(int index) { | 86 Object stackGet(int index) { |
97 return frame.stack[index]; | 87 return frame.stack[index]; |
121 if( frame==null ) | 111 if( frame==null ) |
122 return null; | 112 return null; |
123 return (LuanTable)frame.closure.upValues()[0].get(); | 113 return (LuanTable)frame.closure.upValues()[0].get(); |
124 } | 114 } |
125 | 115 |
126 MtGetterLink mtGetterLink() { | |
127 return frame==null ? null : frame.mtGetterLink; | |
128 } | |
129 | |
130 @Override public LuanTable getMetatable(Object obj,MetatableGetter beforeThis) { | |
131 if( obj instanceof LuanTable ) { | |
132 LuanTable table = (LuanTable)obj; | |
133 return table.getMetatable(); | |
134 } | |
135 MtGetterLink mtGetterLink = mtGetterLink(); | |
136 return mtGetterLink==null ? null : mtGetterLink.getMetatable(obj,beforeThis); | |
137 } | |
138 | |
139 @Override public void addMetatableGetter(MetatableGetter mg) { | |
140 frame.addMetatableGetter(mg); | |
141 } | |
142 | |
143 } | 116 } |