Mercurial Hosting > luan
view core/src/luan/LuanMeta.java @ 701:d0280c7fdc3a
add Io.ip()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 04 May 2016 17:49:41 -0600 |
parents | d9df6d6cb927 |
children |
line wrap: on
line source
package luan; import java.util.Map; import java.util.Iterator; import java.util.Set; import java.util.HashSet; public abstract class LuanMeta { public abstract Object __index(LuanState luan,LuanTable tbl,Object key) throws LuanException; protected abstract Iterator keys(LuanTable tbl); public LuanFunction __pairs(final LuanState luan,final LuanTable tbl) { return new LuanFunction() { final Iterator<Map.Entry<Object,Object>> iter1 = tbl.rawIterator(); final Iterator<Object> iter2 = keys(tbl); final Set<Object> set = new HashSet<Object>(); @Override public Object[] call(LuanState luan,Object[] args) throws LuanException { if( iter1.hasNext() ) { Map.Entry<Object,Object> entry = iter1.next(); Object key = entry.getKey(); set.add(key); return new Object[]{key,entry.getValue()}; } while( iter2.hasNext() ) { Object key = iter2.next(); if( set.add(key) ) { Object value = __index(luan,tbl,key); return new Object[]{key,value}; } } return LuanFunction.NOTHING; } }; } public boolean canNewindex() { return false; } public void __new_index(LuanState luan,LuanTable tbl,Object key,Object value) throws LuanException { throw new UnsupportedOperationException(); } protected abstract String type(LuanTable tbl); public String __to_string(LuanState luan,LuanTable tbl) throws LuanException { return type(tbl) + "-" + tbl.rawToString(); } public LuanTable newMetatable() { LuanTable mt = new LuanTable(); mt.rawPut( "__index", this ); mt.rawPut( "__pairs", this ); mt.rawPut( "__to_string", this ); if( canNewindex() ) mt.rawPut( "__new_index", this ); return mt; } public LuanTable newTable() { LuanTable tbl = new LuanTable(); tbl.setMetatable( newMetatable() ); return tbl; } }