comparison core/src/luan/impl/LuanStateImpl.java @ 670:58ebfec6178b

all luan now compiles
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Apr 2016 01:05:57 -0600
parents b438a47196bc
children
comparison
equal deleted inserted replaced
669:e320488819b6 670:58ebfec6178b
11 import luan.LuanException; 11 import luan.LuanException;
12 import luan.DeepCloner; 12 import luan.DeepCloner;
13 13
14 14
15 public final class LuanStateImpl extends LuanState { 15 public final class LuanStateImpl extends LuanState {
16 16 Closure closure;
17 private static class Frame {
18 final Frame previousFrame;
19 final Closure closure;
20 final Object[] stack;
21 final Object[] varArgs;
22 UpValue[] downValues = null;
23
24 Frame( Frame previousFrame, Closure closure, int stackSize, Object[] varArgs) {
25 this.previousFrame = previousFrame;
26 this.closure = closure;
27 this.stack = new Object[stackSize];
28 this.varArgs = varArgs;
29 }
30
31 void stackClear(int start,int end) {
32 if( downValues != null ) {
33 for( int i=start; i<end; i++ ) {
34 UpValue downValue = downValues[i];
35 if( downValue != null ) {
36 downValue.close();
37 downValues[i] = null;
38 }
39 }
40 }
41 for( int i=start; i<end; i++ ) {
42 stack[i] = null;
43 }
44 }
45
46 UpValue getUpValue(int index) {
47 if( downValues==null )
48 downValues = new UpValue[stack.length];
49 if( downValues[index] == null )
50 downValues[index] = new UpValue(stack,index);
51 return downValues[index];
52 }
53 }
54
55 private Frame frame = null;
56 17
57 LuanStateImpl() {} 18 LuanStateImpl() {}
58 19
59 private LuanStateImpl(LuanStateImpl luan) { 20 private LuanStateImpl(LuanStateImpl luan) {
60 super(luan); 21 super(luan);
61 } 22 }
62 23
63 @Override public LuanState shallowClone() { 24 @Override public LuanState shallowClone() {
64 // if( frame != null )
65 // throw new IllegalStateException("frame isn't null");
66 return new LuanStateImpl(this); 25 return new LuanStateImpl(this);
67 } 26 }
68 27
69 // returns stack
70 Object[] newFrame(Closure closure, int stackSize, Object[] varArgs) {
71 frame = new Frame(frame,closure,stackSize,varArgs);
72 return frame.stack;
73 }
74
75 void popFrame() {
76 frame = frame.previousFrame;
77 }
78
79 Object stackGet(int index) {
80 return frame.stack[index];
81 }
82
83 public void stackSet(int index,Object value) {
84 frame.stack[index] = value;
85 }
86
87 public void stackClear(int start,int end) {
88 frame.stackClear(start,end);
89 }
90
91 Object[] varArgs() {
92 return frame.varArgs;
93 }
94
95 Closure closure() {
96 return frame.closure;
97 }
98
99 UpValue getUpValue(int index) {
100 return frame.getUpValue(index);
101 }
102
103 @Override public boolean hasJava() { 28 @Override public boolean hasJava() {
104 if( frame==null ) 29 if( closure==null )
105 return false; 30 return false;
106 return ((LuanTable)frame.closure.upValues()[0].get()).hasJava; 31 return ((LuanTable)closure.upValues[0].o).hasJava;
107 } 32 }
108 33
109 @Override public void setJava() { 34 @Override public void setJava() {
110 ((LuanTable)frame.closure.upValues()[0].get()).hasJava = true; 35 ((LuanTable)closure.upValues[0].o).hasJava = true;
111 } 36 }
112 37
113
114 } 38 }