view src/luan/interp/SetTableEntry.java @ 37:8a57ebfdfd78

add JavaLib git-svn-id: https://luan-java.googlecode.com/svn/trunk@38 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Thu, 20 Dec 2012 02:36:07 +0000
parents 2a35154aec14
children e3624b7cd603
line wrap: on
line source

package luan.interp;

import luan.LuaException;
import luan.LuaTable;
import luan.Lua;
import luan.LuaFunction;


final class SetTableEntry implements Settable {
	private final Expr tableExpr;
	private final Expr keyExpr;

	SetTableEntry(Expr tableExpr,Expr keyExpr) {
		this.tableExpr = tableExpr;
		this.keyExpr = keyExpr;
	}

	@Override public void set(LuaStateImpl lua,Object value) throws LuaException {
		newindex( lua, tableExpr.eval(lua), keyExpr.eval(lua), value );
	}

	private static void newindex(LuaStateImpl lua,Object t,Object key,Object value) throws LuaException {
		Object h;
		if( t instanceof LuaTable ) {
			LuaTable table = (LuaTable)t;
			Object old = table.put(key,value);
			if( old != null )
				return;
			h = lua.getHandler("__newindex",t);
			if( h==null )
				return;
			table.put(key,old);
		} else {
			h = lua.getHandler("__newindex",t);
			if( h==null )
				throw new LuaException( "attempt to index a " + Lua.type(t) + " value" );
		}
		if( h instanceof LuaFunction ) {
			LuaFunction fn = (LuaFunction)h;
			fn.call(lua,t,key,value);
		}
		newindex(lua,h,key,value);
	}

}