diff src/luan/impl/SetTableEntry.java @ 166:4eaee12f6c65

move luan/interp to impl git-svn-id: https://luan-java.googlecode.com/svn/trunk@167 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 22 Jun 2014 04:17:38 +0000
parents src/luan/interp/SetTableEntry.java@f5af13062b10
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/impl/SetTableEntry.java	Sun Jun 22 04:17:38 2014 +0000
@@ -0,0 +1,48 @@
+package luan.impl;
+
+import luan.LuanException;
+import luan.LuanTable;
+import luan.Luan;
+import luan.LuanFunction;
+import luan.LuanSource;
+
+
+final class SetTableEntry extends CodeImpl implements Settable {
+	private final Expr tableExpr;
+	private final Expr keyExpr;
+
+	SetTableEntry(LuanSource.Element se,Expr tableExpr,Expr keyExpr) {
+		super(se);
+		this.tableExpr = tableExpr;
+		this.keyExpr = keyExpr;
+	}
+
+	@Override public void set(LuanStateImpl luan,Object value) throws LuanException {
+		newindex( luan, tableExpr.eval(luan), keyExpr.eval(luan), value );
+	}
+
+	private void newindex(LuanStateImpl luan,Object t,Object key,Object value) throws LuanException {
+		Object h;
+		if( t instanceof LuanTable ) {
+			LuanTable table = (LuanTable)t;
+			Object old = table.put(key,value);
+			if( old != null )
+				return;
+			h = luan.getHandler("__newindex",t);
+			if( h==null )
+				return;
+			table.put(key,old);
+		} else {
+			h = luan.getHandler("__newindex",t);
+			if( h==null )
+				throw luan.bit(se).exception( "attempt to index a " + Luan.type(t) + " value" );
+		}
+		if( h instanceof LuanFunction ) {
+			LuanFunction fn = (LuanFunction)h;
+			luan.bit(se).call(fn,"__newindex",new Object[]{t,key,value});
+			return;
+		}
+		newindex(luan,h,key,value);
+	}
+
+}