view core/src/luan/LuanState.java @ 498:ee55be414a34

Http.response is now mostly luan
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 18 May 2015 00:25:35 -0600
parents 5d4a78c93383
children d3183a330ff5
line wrap: on
line source

package luan;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import luan.impl.LuanCompiler;
import luan.modules.BasicLuan;


public abstract class LuanState implements DeepCloneable {

	final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();

	private Map registry;

	protected LuanState() {
		registry = new HashMap();
	}

	protected LuanState(LuanState luan) {}

	@Override public void deepenClone(DeepCloneable clone,DeepCloner cloner) {
		((LuanState)clone).registry = cloner.deepClone(registry);
	}

	public abstract LuanTable currentEnvironment();
	public abstract LuanSource currentSource();

	public final Map registry() {
		return registry;
	}

	public static LuanState newInstance() {
		return LuanCompiler.newLuanState();
	}

	public final Object eval(String cmd) throws LuanException {
		return eval(cmd,new LuanTable());
	}

	public final Object eval(String cmd,LuanTable env) throws LuanException {
		LuanFunction fn = BasicLuan.load(this,cmd,"eval",env,true);
		return call(fn);
	}

	public final LuanBit bit(LuanElement el) {
		return new LuanBit(this,el);
	}

	// convenience methods

	private final LuanBit JAVA = bit(null);

	public LuanException exception(Object msg) throws LuanException {
		return JAVA.exception(msg);
	}

	public Object call(LuanFunction fn) throws LuanException {
		return call(fn,null,LuanFunction.NOTHING);
	}

	public Object call(LuanFunction fn,String fnName) throws LuanException {
		return call(fn,fnName,LuanFunction.NOTHING);
	}

	public Object call(LuanFunction fn,Object[] args) throws LuanException {
		return call(fn,null,args);
	}

	public Object call(LuanFunction fn,String fnName,Object[] args) throws LuanException {
		return JAVA.call(fn,fnName,args);
	}

	public Boolean checkBoolean(Object obj) throws LuanException {
		return JAVA.checkBoolean(obj);
	}

	public String checkString(Object obj) throws LuanException {
		return JAVA.checkString(obj);
	}

	public LuanFunction checkFunction(Object obj) throws LuanException {
		return JAVA.checkFunction(obj);
	}

	public String toString(Object obj) throws LuanException {
		return JAVA.toString(obj);
	}

	public boolean isLessThan(Object o1,Object o2) throws LuanException {
		return JAVA.isLessThan(o1,o2);
	}
}