Mercurial Hosting > luan
comparison core/src/luan/LuanPropertyMeta.java @ 415:ce8e19567911
make LuanPropertyMeta a singleton
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 30 Apr 2015 06:04:17 -0600 |
parents | 8937263f59f6 |
children | 91af5337b9ae |
comparison
equal
deleted
inserted
replaced
414:d2ab2240cc65 | 415:ce8e19567911 |
---|---|
2 | 2 |
3 import java.util.Map; | 3 import java.util.Map; |
4 import java.util.Iterator; | 4 import java.util.Iterator; |
5 | 5 |
6 | 6 |
7 public final class LuanPropertyMeta extends LuanMeta implements DeepCloneable<LuanPropertyMeta> { | 7 public final class LuanPropertyMeta extends LuanMeta { |
8 private LuanTable getters; | 8 public static final LuanPropertyMeta INSTANCE = new LuanPropertyMeta(); |
9 private LuanTable setters; | |
10 | 9 |
11 private LuanPropertyMeta() {} | 10 private LuanPropertyMeta() {} |
12 | 11 |
13 public static LuanPropertyMeta newInstance() { | 12 public LuanTable getters(LuanTable tbl) { |
14 LuanPropertyMeta meta = new LuanPropertyMeta(); | 13 return (LuanTable)tbl.getMetatable().get("get"); |
15 meta.getters = new LuanTable(); | |
16 meta.setters = new LuanTable(); | |
17 return meta; | |
18 } | 14 } |
19 | 15 |
20 public LuanTable getters() { | 16 public LuanTable setters(LuanTable tbl) { |
21 return getters; | 17 return (LuanTable)tbl.getMetatable().get("set"); |
22 } | |
23 | |
24 public LuanTable setters() { | |
25 return setters; | |
26 } | |
27 | |
28 @Override public LuanPropertyMeta shallowClone() { | |
29 return new LuanPropertyMeta(); | |
30 } | |
31 | |
32 @Override public void deepenClone(LuanPropertyMeta clone,DeepCloner cloner) { | |
33 clone.getters = cloner.get(getters); | |
34 clone.setters = cloner.get(setters); | |
35 } | 18 } |
36 | 19 |
37 @Override public Object __index(LuanState luan,LuanTable tbl,Object key) throws LuanException { | 20 @Override public Object __index(LuanState luan,LuanTable tbl,Object key) throws LuanException { |
38 Object obj = getters.get(key); | 21 Object obj = getters(tbl).get(key); |
39 if( obj == null ) | 22 if( obj == null ) |
40 return null; | 23 return null; |
41 if( !(obj instanceof LuanFunction) ) | 24 if( !(obj instanceof LuanFunction) ) |
42 throw luan.exception("get for '"+key+"' isn't a function"); | 25 throw luan.exception("get for '"+key+"' isn't a function"); |
43 LuanFunction fn = (LuanFunction)obj; | 26 LuanFunction fn = (LuanFunction)obj; |
44 return luan.call(fn); | 27 return luan.call(fn); |
45 } | 28 } |
46 | 29 |
47 @Override protected Iterator keys() { | 30 @Override protected Iterator keys(final LuanTable tbl) { |
48 return new Iterator() { | 31 return new Iterator() { |
49 final Iterator<Map.Entry<Object,Object>> iter = getters.iterator(); | 32 final Iterator<Map.Entry<Object,Object>> iter = getters(tbl).iterator(); |
50 | 33 |
51 @Override public boolean hasNext() { | 34 @Override public boolean hasNext() { |
52 return iter.hasNext(); | 35 return iter.hasNext(); |
53 } | 36 } |
54 @Override public Object next() { | 37 @Override public Object next() { |
64 @Override public boolean canNewindex() { | 47 @Override public boolean canNewindex() { |
65 return true; | 48 return true; |
66 } | 49 } |
67 | 50 |
68 @Override public void __newindex(LuanState luan,LuanTable tbl,Object key,Object value) throws LuanException { | 51 @Override public void __newindex(LuanState luan,LuanTable tbl,Object key,Object value) throws LuanException { |
69 Object obj = setters.get(key); | 52 Object obj = setters(tbl).get(key); |
70 if( obj == null ) | 53 if( obj == null ) |
71 throw luan.exception("can't set property '"+key+"'"); | 54 throw luan.exception("can't set property '"+key+"'"); |
72 if( !(obj instanceof LuanFunction) ) | 55 if( !(obj instanceof LuanFunction) ) |
73 throw luan.exception("set for '"+key+"' isn't a function"); | 56 throw luan.exception("set for '"+key+"' isn't a function"); |
74 LuanFunction fn = (LuanFunction)obj; | 57 LuanFunction fn = (LuanFunction)obj; |
75 luan.call(fn,new Object[]{value}); | 58 luan.call(fn,new Object[]{value}); |
76 } | 59 } |
77 | 60 |
78 @Override public LuanTable newMetatable() { | 61 @Override public LuanTable newMetatable() { |
79 LuanTable mt = super.newMetatable(); | 62 LuanTable mt = super.newMetatable(); |
80 mt.put( "get", getters ); | 63 mt.put( "get", new LuanTable() ); |
81 mt.put( "set", setters ); | 64 mt.put( "set", new LuanTable() ); |
82 return mt; | 65 return mt; |
83 } | 66 } |
84 | 67 |
85 } | 68 } |