comparison src/luan/impl/UpValue.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/UpValue.java@f99fd64291b3
children
comparison
equal deleted inserted replaced
165:94bbc4cbc106 166:4eaee12f6c65
1 package luan.impl;
2
3 import luan.DeepCloner;
4 import luan.DeepCloneable;
5 import luan.LuanException;
6
7
8 final class UpValue implements DeepCloneable<UpValue> {
9 private Object[] stack;
10 private int index;
11 private boolean isClosed = false;
12 private Object value;
13
14 UpValue(Object[] stack,int index) {
15 this.stack = stack;
16 this.index = index;
17 }
18
19 UpValue(Object value) {
20 this.value = value;
21 this.isClosed = true;
22 }
23
24 private UpValue() {}
25
26 @Override public UpValue shallowClone() {
27 return new UpValue();
28 }
29
30 @Override public void deepenClone(UpValue clone,DeepCloner cloner) {
31 clone.isClosed = isClosed;
32 if( isClosed ) {
33 clone.value = cloner.get(value);
34 } else {
35 clone.stack = cloner.deepClone(stack);
36 clone.index = index;
37 }
38 }
39
40 Object get() {
41 return isClosed ? value : stack[index];
42 }
43
44 void set(Object value) {
45 if( isClosed ) {
46 this.value = value;
47 } else {
48 stack[index] = value;
49 }
50 }
51
52 void close() {
53 value = stack[index];
54 isClosed = true;
55 stack = null;
56 }
57
58 static interface Getter {
59 public UpValue get(LuanStateImpl luan) throws LuanException;
60 }
61
62 static final class StackGetter implements Getter {
63 private final int index;
64
65 StackGetter(int index) {
66 this.index = index;
67 }
68
69 public UpValue get(LuanStateImpl luan) {
70 return luan.getUpValue(index);
71 }
72 }
73
74 static final class NestedGetter implements Getter {
75 private final int index;
76
77 NestedGetter(int index) {
78 this.index = index;
79 }
80
81 public UpValue get(LuanStateImpl luan) {
82 return luan.closure().upValues()[index];
83 }
84 }
85
86 static final class EnvGetter implements Getter {
87
88 public UpValue get(LuanStateImpl luan) throws LuanException {
89 return luan.getUpValue(this);
90 }
91 }
92
93 static final class ValueGetter implements Getter {
94 private final UpValue upValue;
95
96 ValueGetter(Object value) {
97 this.upValue = new UpValue(value);
98 }
99
100 public UpValue get(LuanStateImpl luan) {
101 return upValue;
102 }
103 }
104
105 }