view src/luan/modules/BasicLuan.java @ 1171:794ddcfbee20

remove http/impl
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 11 Feb 2018 02:41:23 -0700
parents 7ef40e1923b7
children 73d754b1889f
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.LuanException;
import luan.LuanMethod;


public final class BasicLuan {

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

	public static LuanFunction load(String text,String sourceName,LuanTable env)
		throws LuanException
	{
		Utils.checkNotNull(text);
		Utils.checkNotNull(sourceName,1);
		return Luan.load(text,sourceName,env);
	}

	public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException {
		if( fileName == null ) {
			fileName = "stdin:";
		} else if( fileName.indexOf(':') == -1 ) {
			fileName = "file:" + fileName;
		}
		String src = PackageLuan.read(luan,fileName);
		if( src == null )
			throw new LuanException("file '"+fileName+"' not found" );
		return load(src,fileName,null);
	}

	public static LuanFunction pairs(final LuanState luan,final LuanTable t) throws LuanException {
		Utils.checkNotNull(t);
		return t.pairs(luan);
	}

	public static LuanFunction ipairs(final LuanTable t) throws LuanException {
		Utils.checkNotNull(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 Object get_metatable(LuanTable table) throws LuanException {
		Utils.checkNotNull(table);
		LuanTable metatable = table.getMetatable();
		if( metatable == null )
			return null;
		Object obj = metatable.rawGet("__metatable");
		return obj!=null ? obj : metatable;
	}

	public static void set_metatable(LuanState luan,LuanTable table,LuanTable metatable) throws LuanException {
		Utils.checkNotNull(table);
		if( table.getHandler(luan,"__metatable") != null )
			throw new LuanException("cannot change a protected metatable");
		table.setMetatable(metatable);
	}

	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.rawGet(index);
	}

	public static void raw_set(LuanTable table,Object index,Object value) {
		table.rawPut(index,value);
	}

	public static int raw_len(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.rawLength();
		}
		throw new LuanException( "bad argument #1 to 'raw_len' (table or string expected)" );
	}

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

	public static LuanTable new_error(LuanState luan,Object msg) throws LuanException {
		String s = luan.toString(msg);
		LuanTable tbl = new LuanException(s).table();
		tbl.rawPut( "message", msg );
		return tbl;
	}

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

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

	public static double assert_double(double v) throws LuanException {
		return v;
	}

	public static float assert_float(float v) throws LuanException {
		return v;
	}

	public static LuanFunction range(final double from,final double to,Double stepV) throws LuanException {
		final double step = stepV==null ? 1.0 : stepV;
		if( step == 0.0 )
			throw new LuanException("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 args[i++];
			}
		};
	}

	private LuanFunction fn(Object obj) {
		return obj instanceof LuanFunction ? (LuanFunction)obj : null;
	}

	public static Object try_(LuanState luan,LuanTable blocks,Object... args) throws LuanException {
		Utils.checkNotNull(blocks);
		Object obj = blocks.get(luan,1);
		if( obj == null )
			throw new LuanException("missing 'try' value");
		if( !(obj instanceof LuanFunction) )
			throw new LuanException("bad 'try' value (function expected, got "+Luan.type(obj)+")");
		LuanFunction tryFn = (LuanFunction)obj;
		LuanFunction catchFn = null;
		obj = blocks.get(luan,"catch");
		if( obj != null ) {
			if( !(obj instanceof LuanFunction) )
				throw new LuanException("bad 'catch' value (function expected, got "+Luan.type(obj)+")");
			catchFn = (LuanFunction)obj;
		}
		LuanFunction finallyFn = null;
		obj = blocks.get(luan,"finally");
		if( obj != null ) {
			if( !(obj instanceof LuanFunction) )
				throw new LuanException("bad 'finally' value (function expected, got "+Luan.type(obj)+")");
			finallyFn = (LuanFunction)obj;
		}
		try {
			return tryFn.call(luan,args);
		} catch(LuanException e) {
			if( catchFn == null )
				throw e;
			return catchFn.call(luan,new Object[]{e.table()});
		} finally {
			if( finallyFn != null )
				finallyFn.call(luan);
		}
	}

	@LuanMethod public static Object[] pcall(LuanState luan,LuanFunction f,Object... args) {
		try {
			Object[] r = Luan.array(f.call(luan,args));
			Object[] rtn = new Object[r.length+1];
			rtn[0] = true;
			for( int i=0; i<r.length; i++ ) {
				rtn[i+1] = r[i];
			}
			return rtn;
		} catch(LuanException e) {
			return new Object[]{false,e.table()};
		}
	}

	public static String number_type(Number v) throws LuanException {
		Utils.checkNotNull(v);
		return v.getClass().getSimpleName().toLowerCase();
	}

	public static int hash_code(Object obj) throws LuanException {
		if( obj == null ) {
			return 0;
		} else {
			return obj.hashCode();
		}
	}

}