Mercurial Hosting > luan
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/src/luan/LuanPropertyMeta.java Wed Apr 29 13:01:00 2015 -0600 @@ -0,0 +1,65 @@ +package luan; + + +public class LuanPropertyMeta extends LuanMeta implements DeepCloneable<LuanPropertyMeta> { + private LuanTable getters; + private LuanTable setters; + + private LuanPropertyMeta() {} + + public static LuanPropertyMeta newInstance() { + LuanPropertyMeta meta = new LuanPropertyMeta(); + meta.getters = new LuanTableImpl(); + meta.setters = new LuanTableImpl(); + return meta; + } + + public LuanTable getters() { + return getters; + } + + public LuanTable setters() { + return setters; + } + + @Override public LuanPropertyMeta shallowClone() { + return new LuanPropertyMeta(); + } + + @Override public void deepenClone(LuanPropertyMeta clone,DeepCloner cloner) { + clone.getters = cloner.get(getters); + clone.setters = cloner.get(setters); + } + + @Override public Object __index(LuanState luan,LuanTable tbl,Object key) throws LuanException { + Object obj = getters.get(key); + if( obj == null ) + return null; + if( !(obj instanceof LuanFunction) ) + throw luan.exception("get for '"+key+"' isn't a function"); + LuanFunction fn = (LuanFunction)obj; + return luan.call(fn); + } + + @Override public boolean canNewindex() { + return true; + } + + @Override public void __newindex(LuanState luan,LuanTable tbl,Object key,Object value) throws LuanException { + Object obj = setters.get(key); + if( obj == null ) + throw luan.exception("can't set property '"+key+"'"); + if( !(obj instanceof LuanFunction) ) + throw luan.exception("set for '"+key+"' isn't a function"); + LuanFunction fn = (LuanFunction)obj; + luan.call(fn,new Object[]{value}); + } + + @Override public LuanTable newMetatable() { + LuanTable mt = super.newMetatable(); + mt.put( "get", getters ); + mt.put( "set", setters ); + return mt; + } + +}