view src/luan/lib/BinaryLib.java @ 120:8d7730a5e3b4

move standalone logic from Java to Luan git-svn-id: https://luan-java.googlecode.com/svn/trunk@121 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Mon, 02 Jun 2014 04:43:45 +0000
parents 8c706d6eb5dc
children f537ff5e511d
line wrap: on
line source

package luan.lib;

import luan.LuanState;
import luan.LuanTable;
import luan.LuanFunction;
import luan.LuanJavaFunction;


public final class BinaryLib {

	public static final String NAME = "Binary";

	public static final LuanFunction LOADER = new LuanFunction() {
		@Override public Object call(LuanState luan,Object[] args) {
			LuanTable module = new LuanTable();
			try {
				add( module, "to_string", new byte[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(BinaryLib.class.getMethod(method,parameterTypes),null) );
	}

	public static String to_string(byte[] bytes) {
		return new String(bytes);
	}

}