view core/src/luan/LuanState.java @ 574:6cc2f047019b

remove LuanState.call()
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 13 Jul 2015 12:31:53 -0600
parents f1601a4ce1aa
children 7c3ad6db8ac3
line wrap: on
line source

package luan;

import java.io.Closeable;
import java.io.IOException;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
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;
	private final List<Reference<Closeable>> onClose = new ArrayList<Reference<Closeable>>();

	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 boolean hasJava();
	public abstract void setJava();
	public abstract LuanSource currentSource();

	public final Map registry() {
		return registry;
	}

	public void onClose(Closeable c) {
		onClose.add(new WeakReference<Closeable>(c));
	}

	public void close() throws IOException {
		for( Reference<Closeable> ref : onClose ) {
			Closeable c = ref.get();
			if( c != null )
				c.close();
		}
		onClose.clear();
	}

	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 fn.call(this);
	}

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

	// convenience methods

	public final LuanBit JAVA = bit(null);

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

	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 boolean isLessThan(Object o1,Object o2) throws LuanException {
		return JAVA.isLessThan(o1,o2);
	}
}