Mercurial Hosting > luan
view core/src/luan/LuanPropertyMeta.java @ 594:e91e476186c7
theme indentation
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 03 Sep 2015 12:23:53 -0600 |
parents | 60c549d43988 |
children | cdc70de628b5 |
line wrap: on
line source
package luan; import java.util.Map; import java.util.Iterator; public final class LuanPropertyMeta extends LuanMeta { public static final LuanPropertyMeta INSTANCE = new LuanPropertyMeta(); private LuanPropertyMeta() {} public LuanTable getters(LuanTable tbl) { return (LuanTable)tbl.getMetatable().rawGet("get"); } public LuanTable setters(LuanTable tbl) { return (LuanTable)tbl.getMetatable().rawGet("set"); } protected String type(LuanTable tbl) { return (String)tbl.getMetatable().rawGet("type"); } @Override public Object __index(LuanState luan,LuanTable tbl,Object key) throws LuanException { Object obj = getters(tbl).rawGet(key); if( obj == null ) return null; if( !(obj instanceof LuanFunction) ) throw new LuanException(luan,"get for '"+key+"' isn't a function"); LuanFunction fn = (LuanFunction)obj; return fn.call(luan); } @Override protected Iterator keys(final LuanTable tbl) { return new Iterator() { final Iterator<Map.Entry<Object,Object>> iter = getters(tbl).rawIterator(); @Override public boolean hasNext() { return iter.hasNext(); } @Override public Object next() { return iter.next().getKey(); } @Override public void remove() { throw new UnsupportedOperationException(); } }; } @Override public boolean canNewindex() { return true; } @Override public void __new_index(LuanState luan,LuanTable tbl,Object key,Object value) throws LuanException { Object obj = setters(tbl).rawGet(key); if( obj == null ) { tbl.rawPut(key,value); return; } if( !(obj instanceof LuanFunction) ) throw new LuanException(luan,"set for '"+key+"' isn't a function"); LuanFunction fn = (LuanFunction)obj; fn.call(luan,new Object[]{value}); } @Override public LuanTable newMetatable() { LuanTable mt = super.newMetatable(); mt.rawPut( "get", new LuanTable() ); mt.rawPut( "set", new LuanTable() ); mt.rawPut( "type", "property" ); return mt; } }