view core/src/luan/modules/BasicLuan.java @ 296:7ea6dfdf81ba

add env param to load_file() git-svn-id: https://luan-java.googlecode.com/svn/trunk@297 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Mon, 15 Dec 2014 08:03:32 +0000
parents 9a0cc6d9b27b
children 899253043270
line wrap: on
line source

package luan.modules;

import java.io.InputStreamReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import luan.Luan;
import luan.LuanState;
import luan.LuanTable;
import luan.LuanFunction;
import luan.LuanJavaFunction;
import luan.LuanException;
import luan.LuanSource;
import luan.LuanElement;
import luan.impl.LuanCompiler;


public final class BasicLuan {

	public static final LuanFunction LOADER = new LuanFunction() {
		@Override public Object call(LuanState luan,Object[] args) {
			LuanTable module = Luan.newTable();
			try {
				module.put( "assert", new LuanJavaFunction(BasicLuan.class.getMethod("assert_",LuanState.class,Object.class,String.class),null) );
				add( module, "assert_boolean", LuanState.class, Boolean.TYPE );
				add( module, "assert_nil", LuanState.class, Object.class );
				add( module, "assert_number", LuanState.class, Number.class );
				add( module, "assert_string", LuanState.class, String.class );
				add( module, "assert_table", LuanState.class, LuanTable.class );
				add( module, "assert_integer", LuanState.class, Integer.TYPE );
				add( module, "assert_long", LuanState.class, Long.TYPE );
				add( module, "do_file", LuanState.class, String.class );
				add( module, "error", LuanState.class, Object.class );
				add( module, "get_metatable", LuanState.class, Object.class );
				add( module, "ipairs", LuanState.class, LuanTable.class );
				add( module, "load", LuanState.class, String.class, String.class, LuanTable.class, Boolean.class );
				add( module, "load_file", LuanState.class, String.class, LuanTable.class );
				add( module, "pairs", LuanState.class, LuanTable.class );
				add( module, "range", LuanState.class, Double.TYPE, Double.TYPE, Double.class );
				add( module, "raw_equal", Object.class, Object.class );
				add( module, "raw_get", LuanTable.class, Object.class );
				add( module, "raw_len", LuanState.class, Object.class );
				add( module, "raw_set", LuanTable.class, Object.class, Object.class );
				add( module, "repr", LuanState.class, Object.class );
				add( module, "set_metatable", LuanTable.class, LuanTable.class );
				add( module, "to_boolean", Object.class );
				add( module, "to_number", Object.class, Integer.class );
				add( module, "to_string", LuanState.class, Object.class );
				add( module, "type", Object.class );
				add( module, "values", new Object[0].getClass() );
			} catch(NoSuchMethodException e) {
				throw new RuntimeException(e);
			}
			return module;
		}
	};

	private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
		t.put( method, new LuanJavaFunction(BasicLuan.class.getMethod(method,parameterTypes),null) );
	}

	public static String type(Object obj) {
		return Luan.type(obj);
	}

	public static LuanFunction load(LuanState luan,String text,String sourceName,LuanTable env,Boolean allowExpr)
		throws LuanException
	{
		if( allowExpr==null )
			allowExpr = false;
		return LuanCompiler.compile(luan,new LuanSource(sourceName,text),env,allowExpr);
	}

	public static LuanFunction load_file(LuanState luan,String fileName,LuanTable env) throws LuanException {
		if( fileName == null )
			fileName = "stdin:";
		LuanFunction fn = PackageLuan.loader(luan,fileName,false,env);
		if( fn == null )
			throw luan.exception( "file '"+fileName+"' not found" );
		return fn;
	}

	public static Object do_file(LuanState luan,String fileName) throws LuanException {
		LuanFunction fn = load_file(luan,fileName,null);
		return luan.call(fn);
	}

	public static LuanFunction pairs(LuanState luan,final LuanTable t) throws LuanException {
		Utils.checkNotNull(luan,t);
		return new LuanFunction() {
			Iterator<Map.Entry<Object,Object>> iter = t.iterator();

			@Override public Object[] call(LuanState luan,Object[] args) {
				if( !iter.hasNext() )
					return LuanFunction.NOTHING;
				Map.Entry<Object,Object> entry = iter.next();
				return new Object[]{entry.getKey(),entry.getValue()};
			}
		};
	}

	public static LuanFunction ipairs(LuanState luan,final LuanTable t) throws LuanException {
		Utils.checkNotNull(luan,t);
		return new LuanFunction() {
			List<Object> list = t.asList();
			int i = 0;
			final int size = list.size();

			@Override public Object[] call(LuanState luan,Object[] args) {
				if( i >= size )
					return LuanFunction.NOTHING;
				Object val = list.get(i++);
				return new Object[]{i,val};
			}
		};
	}

	public static LuanTable get_metatable(LuanState luan,Object obj) {
		return luan.getMetatable(obj);
	}

	public static LuanTable set_metatable(LuanTable table,LuanTable metatable) {
		table.setMetatable(metatable);
		return table;
	}

	public static boolean raw_equal(Object v1,Object v2) {
		return v1 == v2 || v1 != null && v1.equals(v2);
	}

	public static Object raw_get(LuanTable table,Object index) {
		return table.get(index);
	}

	public static LuanTable raw_set(LuanTable table,Object index,Object value) {
		table.put(index,value);
		return table;
	}

	public static int raw_len(LuanState luan,Object v) throws LuanException {
		if( v instanceof String ) {
			String s = (String)v;
			return s.length();
		}
		if( v instanceof LuanTable ) {
			LuanTable t = (LuanTable)v;
			return t.length();
		}
		throw luan.exception( "bad argument #1 to 'raw_len' (table or string expected)" );
	}

	public static Number to_number(Object e,Integer base) {
		return Luan.toNumber(e,base);
	}

	public static String to_string(LuanState luan,Object v) throws LuanException {
		return luan.toString(v);
	}

	public static boolean to_boolean(Object v) throws LuanException {
		return Luan.toBoolean(v);
	}

	public static void error(LuanState luan,Object msg) throws LuanException {
		throw luan.exception(msg);
	}

	public static Object assert_(LuanState luan,Object v,String msg) throws LuanException {
		if( Luan.toBoolean(v) )
			return v;
		if( msg == null )
			msg = "assertion failed!";
		throw luan.exception( msg );
	}

	public static String assert_string(LuanState luan,String v) throws LuanException {
		Utils.checkNotNull(luan,v);
		return v;
	}

	public static Number assert_number(LuanState luan,Number v) throws LuanException {
		Utils.checkNotNull(luan,v);
		return v;
	}

	public static LuanTable assert_table(LuanState luan,LuanTable v) throws LuanException {
		Utils.checkNotNull(luan,v);
		return v;
	}

	public static boolean assert_boolean(LuanState luan,boolean v) throws LuanException {
		return v;
	}

	public static Object assert_nil(LuanState luan,Object v) throws LuanException {
		if( v != null )
			throw luan.exception("bad argument #1 (nil expected, got "+Luan.type(v)+")");
		return v;
	}

	public static int assert_integer(LuanState luan,int v) throws LuanException {
		return v;
	}

	public static long assert_long(LuanState luan,long v) throws LuanException {
		return v;
	}

	public static String repr(LuanState luan,Object v) throws LuanException {
		return luan.repr(v);
	}

	public static LuanFunction range(LuanState luan,final double from,final double to,Double stepV) throws LuanException {
		final double step = stepV==null ? 1.0 : stepV;
		if( step == 0.0 )
			throw luan.exception("bad argument #3 (step may not be zero)");
		return new LuanFunction() {
			double v = from;

			@Override public Object call(LuanState luan,Object[] args) {
				if( step > 0.0 && v > to || step < 0.0 && v < to )
					return LuanFunction.NOTHING;
				double rtn = v;
				v += step;
				return rtn;
			}
		};
	}

	public static LuanFunction values(final Object... args) throws LuanException {
		return new LuanFunction() {
			int i = 0;

			@Override public Object call(LuanState luan,Object[] unused) {
				if( ++i > args.length )
					return LuanFunction.NOTHING;
				return new Object[]{i,args[i-1]};
			}
		};
	}

}