comparison src/luan/interp/UpValue.java @ 77:4bf3d0c0b6b9

make LuanState cloneable git-svn-id: https://luan-java.googlecode.com/svn/trunk@78 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 15 Feb 2013 09:55:17 +0000
parents f86e4f77ef32
children 7c08b611125d
comparison
equal deleted inserted replaced
76:97b03fc807ad 77:4bf3d0c0b6b9
1 package luan.interp; 1 package luan.interp;
2 2
3 import luan.DeepCloner;
4 import luan.DeepCloneable;
3 5
4 final class UpValue { 6
7 final class UpValue implements DeepCloneable<UpValue> {
5 private Object[] stack; 8 private Object[] stack;
6 private int index; 9 private int index;
7 private boolean isClosed = false; 10 private boolean isClosed = false;
8 private Object value; 11 private Object value;
9 12
13 } 16 }
14 17
15 UpValue(Object value) { 18 UpValue(Object value) {
16 this.value = value; 19 this.value = value;
17 this.isClosed = true; 20 this.isClosed = true;
21 }
22
23 private UpValue() {}
24
25 @Override public UpValue shallowClone() {
26 return new UpValue();
27 }
28
29 @Override public void deepenClone(UpValue clone,DeepCloner cloner) {
30 clone.isClosed = isClosed;
31 if( isClosed ) {
32 clone.value = cloner.get(value);
33 } else {
34 clone.stack = stack.clone();
35 cloner.deepenClone(clone.stack);
36 clone.index = index;
37 }
18 } 38 }
19 39
20 Object get() { 40 Object get() {
21 return isClosed ? value : stack[index]; 41 return isClosed ? value : stack[index];
22 } 42 }
63 } 83 }
64 } 84 }
65 85
66 static final Getter globalGetter = new Getter() { 86 static final Getter globalGetter = new Getter() {
67 public UpValue get(LuanStateImpl luan) { 87 public UpValue get(LuanStateImpl luan) {
68 return new UpValue(luan.global); 88 return new UpValue(luan.global());
69 } 89 }
70 }; 90 };
71 91
72 } 92 }