Mercurial Hosting > luan
comparison src/luan/interp/UpValue.java @ 34:0cdc1da466ee
implement _G and _ENV
git-svn-id: https://luan-java.googlecode.com/svn/trunk@35 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Sun, 16 Dec 2012 09:23:56 +0000 |
parents | c3eab5a3ce3c |
children | 64ecb7a3aad7 |
comparison
equal
deleted
inserted
replaced
33:8793c71ad47a | 34:0cdc1da466ee |
---|---|
2 | 2 |
3 | 3 |
4 final class UpValue { | 4 final class UpValue { |
5 private Object[] stack; | 5 private Object[] stack; |
6 private int index; | 6 private int index; |
7 private boolean isClosed = false; | |
8 private Object value; | |
7 | 9 |
8 UpValue(Object[] stack,int index) { | 10 UpValue(Object[] stack,int index) { |
9 this.stack = stack; | 11 this.stack = stack; |
10 this.index = index; | 12 this.index = index; |
11 } | 13 } |
12 | 14 |
15 UpValue(Object value) { | |
16 this.value = value; | |
17 this.isClosed = true; | |
18 } | |
19 | |
13 Object get() { | 20 Object get() { |
14 return stack[index]; | 21 return isClosed ? value : stack[index]; |
15 } | 22 } |
16 | 23 |
17 void set(Object value) { | 24 void set(Object value) { |
18 stack[index] = value; | 25 if( isClosed ) { |
26 this.value = value; | |
27 } else { | |
28 stack[index] = value; | |
29 } | |
19 } | 30 } |
20 | 31 |
21 void close() { | 32 void close() { |
22 stack = new Object[]{get()}; | 33 value = stack[index]; |
23 index = 0; | 34 isClosed = true; |
35 stack = null; | |
24 } | 36 } |
25 | 37 |
26 static interface Getter { | 38 static interface Getter { |
27 public UpValue get(LuaStateImpl lua); | 39 public UpValue get(LuaStateImpl lua); |
28 } | 40 } |
49 public UpValue get(LuaStateImpl lua) { | 61 public UpValue get(LuaStateImpl lua) { |
50 return lua.closure().upValues[index]; | 62 return lua.closure().upValues[index]; |
51 } | 63 } |
52 } | 64 } |
53 | 65 |
66 static final Getter globalGetter = new Getter() { | |
67 public UpValue get(LuaStateImpl lua) { | |
68 return new UpValue(lua.global()); | |
69 } | |
70 }; | |
71 | |
54 } | 72 } |