view src/luan/interp/TableExpr.java @ 111:2428ecfed375

change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection git-svn-id: https://luan-java.googlecode.com/svn/trunk@112 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 23 May 2014 20:40:05 +0000
parents 8ede219cd111
children
line wrap: on
line source

package luan.interp;

import luan.LuanException;
import luan.LuanTable;
import luan.LuanSource;


final class TableExpr extends CodeImpl implements Expr {

	static class Field {
		final Expr key;
		final Expr value;

		Field(Expr key,Expr value) {
			this.key = key;
			this.value = value;
		}
	}

	private final Field[] fields;
	private final Expressions expressions;

	TableExpr(LuanSource.Element se,Field[] fields,Expressions expressions) {
		super(se);
		this.fields = fields;
		this.expressions = expressions;
	}

	@Override public Object eval(LuanStateImpl luan) throws LuanException {
		LuanTable table = new LuanTable();
		for( Field field : fields ) {
			table.put( field.key.eval(luan), field.value.eval(luan) );
		}
		Object obj = expressions.eval(luan);
		if( obj instanceof Object[] ) {
			Object[] a = (Object[])obj;
			for( int i=0; i<a.length; i++ ) {
				table.put( i+1, a[i] );
			}
		} else {
			table.put( 1, obj );
		}
		return table;
	}
}