view src/luan/LuanClosure.java @ 1809:90187946d1a4

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 12 May 2024 17:15:33 -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;
}