diff core/src/luan/LuanMeta.java @ 405:3e68917a0dc6

add LuanMeta
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 29 Apr 2015 11:10:11 -0600
parents
children 1b38de2b1845
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/src/luan/LuanMeta.java	Wed Apr 29 11:10:11 2015 -0600
@@ -0,0 +1,48 @@
+package luan;
+
+import java.util.Map;
+
+
+public abstract class LuanMeta /*implements Iterable<Map.Entry<Object,Object>>*/ {
+
+	public abstract Object __index(LuanState luan,LuanTable tbl,Object key) throws LuanException;
+
+//	public abstract LuanFunction __pairs(LuanState luan,LuanTable tbl) throws LuanException;
+
+	public boolean canNewindex() {
+		return false;
+	}
+
+	public void __newindex(LuanState luan,LuanTable tbl,Object key,Object value) throws LuanException {
+		throw new UnsupportedOperationException();
+	}
+
+	public LuanTable newMetatable() {
+		LuanTable mt = new LuanTableImpl();
+/*
+		try {
+			mt.put( "__index", new LuanJavaFunction(
+				LuanMeta.class.getMethod( "__index", LuanState.class, LuanTable.class, Object.class ), this
+			) );
+			if( canNewindex() ) {
+				mt.put( "__newindex", new LuanJavaFunction(
+					LuanMeta.class.getMethod( "__newindex", LuanState.class, LuanTable.class, Object.class, Object.class ), mt
+				) );
+			}
+		} catch(NoSuchMethodException e) {
+			throw new RuntimeException(e);
+		}
+*/
+		mt.put( "__index", this );
+//		mt.put( "__pairs", this );
+		if( canNewindex() )
+			mt.put( "__newindex", this );
+		return mt;
+	}
+
+	public LuanTable newTable() {
+		LuanTable tbl = new LuanTableImpl();
+		tbl.setMetatable( newMetatable() );
+		return tbl;
+	}
+}