view core/src/luan/impl/SetTableEntry.java @ 404:d55e873e1f0d

metatables now only apply to tables
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 29 Apr 2015 07:04:40 -0600
parents 05eb2837ddbf
children 3e68917a0dc6
line wrap: on
line source

package luan.impl;

import luan.LuanException;
import luan.LuanTable;
import luan.Luan;
import luan.LuanFunction;
import luan.LuanSource;
import luan.modules.JavaLuan;


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 {
		if( t instanceof LuanTable ) {
			LuanTable table = (LuanTable)t;
			Object h = luan.getHandler("__newindex",table);
			if( h==null || table.get(key)!=null ) {
				try {
					table.put(key,value);
				} catch(IllegalArgumentException e) {
					throw luan.bit(se).exception(e);
				} catch(UnsupportedOperationException e) {
					throw luan.bit(se).exception(e);
				}
				return;
			}
			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);
		}
		if( !JavaLuan.__newindex(luan,t,key,value) )
			throw luan.bit(se).exception( "attempt to index '"+tableExpr.se().text()+"' (a " + Luan.type(t) + " value)" );
	}

}