comparison core/src/luan/LuanPropertyMeta.java @ 407:7fd9f1b7b878

replace LuanPropertyTable with LuanPropertyMeta
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 29 Apr 2015 13:01:00 -0600
parents
children 1b38de2b1845
comparison
equal deleted inserted replaced
406:9321a33b9b1c 407:7fd9f1b7b878
1 package luan;
2
3
4 public class LuanPropertyMeta extends LuanMeta implements DeepCloneable<LuanPropertyMeta> {
5 private LuanTable getters;
6 private LuanTable setters;
7
8 private LuanPropertyMeta() {}
9
10 public static LuanPropertyMeta newInstance() {
11 LuanPropertyMeta meta = new LuanPropertyMeta();
12 meta.getters = new LuanTableImpl();
13 meta.setters = new LuanTableImpl();
14 return meta;
15 }
16
17 public LuanTable getters() {
18 return getters;
19 }
20
21 public LuanTable setters() {
22 return setters;
23 }
24
25 @Override public LuanPropertyMeta shallowClone() {
26 return new LuanPropertyMeta();
27 }
28
29 @Override public void deepenClone(LuanPropertyMeta clone,DeepCloner cloner) {
30 clone.getters = cloner.get(getters);
31 clone.setters = cloner.get(setters);
32 }
33
34 @Override public Object __index(LuanState luan,LuanTable tbl,Object key) throws LuanException {
35 Object obj = getters.get(key);
36 if( obj == null )
37 return null;
38 if( !(obj instanceof LuanFunction) )
39 throw luan.exception("get for '"+key+"' isn't a function");
40 LuanFunction fn = (LuanFunction)obj;
41 return luan.call(fn);
42 }
43
44 @Override public boolean canNewindex() {
45 return true;
46 }
47
48 @Override public void __newindex(LuanState luan,LuanTable tbl,Object key,Object value) throws LuanException {
49 Object obj = setters.get(key);
50 if( obj == null )
51 throw luan.exception("can't set property '"+key+"'");
52 if( !(obj instanceof LuanFunction) )
53 throw luan.exception("set for '"+key+"' isn't a function");
54 LuanFunction fn = (LuanFunction)obj;
55 luan.call(fn,new Object[]{value});
56 }
57
58 @Override public LuanTable newMetatable() {
59 LuanTable mt = super.newMetatable();
60 mt.put( "get", getters );
61 mt.put( "set", setters );
62 return mt;
63 }
64
65 }