view src/luan/LuanClosure.java @ 1561:e1a13e707bf3

start immutable
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 05 Nov 2020 20:24:09 -0700
parents 221eedb0f54e
children 8fbcc4747091
line wrap: on
line source

package luan;

import luan.impl.Pointer;


public abstract class LuanClosure extends LuanFunction {
	public Pointer[] upValues;
	public boolean javaOk;
	public final String sourceName;

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

	@Override protected void completeClone(LuanFunction dc,LuanCloner cloner) {
		LuanClosure clone = (LuanClosure)dc;
		clone.upValues = (Pointer[])cloner.clone(upValues);
		super.completeClone(dc,cloner);
	}

	@Override public void makeImmutable(LuanImmutabler immutabler) throws LuanException {
		immutabler.makeImmutable(upValues);
		super.makeImmutable(immutabler);
	}

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

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

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