diff core/src/luan/LuanTable.java @ 427:dae264ad6a7b

fix LuanTable.put() to use metatables
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 01 May 2015 19:29:07 -0600
parents 23a93c118042
children df95199ca4c0
line wrap: on
line diff
--- a/core/src/luan/LuanTable.java	Fri May 01 18:44:20 2015 -0600
+++ b/core/src/luan/LuanTable.java	Fri May 01 19:29:07 2015 -0600
@@ -126,7 +126,31 @@
 		return map.get(key);
 	}
 
-	public void put(Object key,Object val) {
+	public void put(LuanState luan,Object key,Object value) throws LuanException {
+		Object h = getHandler("__newindex");
+		if( h==null || rawGet(key)!=null ) {
+			rawPut(key,value);
+			return;
+		}
+		if( h instanceof LuanFunction ) {
+			LuanFunction fn = (LuanFunction)h;
+			luan.call(fn,"__newindex",new Object[]{this,key,value});
+			return;
+		}
+		if( h instanceof LuanMeta ) {
+			LuanMeta meta = (LuanMeta)h;
+			meta.__newindex(luan,this,key,value);
+			return;
+		}
+		if( h instanceof LuanTable ) {
+			LuanTable tbl = (LuanTable)h;
+			tbl.put(luan,key,value);
+			return;
+		}
+		throw luan.exception("invalid type "+Luan.type(h)+" for metamethod __newindex");
+	}
+
+	public void rawPut(Object key,Object val) {
 		Integer iT = Luan.asInteger(key);
 		if( iT != null ) {
 			int i = iT - 1;