view src/luan/LuanClosure.java @ 1830:d72a52232f79

mail - minor
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 24 Sep 2024 17:17:57 -0600
parents c922446f53aa
children
line wrap: on
line source

package luan;

import luan.impl.Pointer;


public abstract class LuanClosure extends LuanFunction implements LuanMutable {
	public Pointer[] upValues;
	public boolean javaOk;
	public final String sourceName;
	private boolean immutable = false;

	public LuanClosure(Pointer[] upValues,boolean javaOk,String sourceName) throws LuanException {
		this.upValues = upValues;
		this.javaOk = javaOk;
		this.sourceName = sourceName;
	}

	@Override public final boolean isImmutable() {
		return immutable;
	}

	@Override public final void makeImmutable() {
		if(immutable)
			return;
		immutable = true;
		LuanMutable.makeImmutable(upValues);
	}

	@Override public final Object call(Luan luan,Object... args) throws LuanException {
		luan.push(this);
		try {
			return doCall(luan,args);
		} catch(StackOverflowError e) {
			throw new LuanException( "stack overflow", e );
		} finally {
			luan.pop();
		}	
	}

	@Override public final String toString() {
		return super.toString()+"="+sourceName;
	}

	public abstract Object doCall(Luan luan,Object[] args) throws LuanException;
}