view core/src/luan/impl/Closure.java @ 670:58ebfec6178b

all luan now compiles
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Apr 2016 01:05:57 -0600
parents 7780cafca27f
children d3e5414bdf4c
line wrap: on
line source

package luan.impl;

import luan.Luan;
import luan.LuanFunction;
import luan.LuanState;
import luan.LuanException;
import luan.DeepCloner;
import luan.DeepCloneable;


public abstract class Closure extends LuanFunction implements DeepCloneable, Cloneable {
	public Pointer[] upValues;

	public Closure(int nUpValues) throws LuanException {
		this.upValues = new Pointer[nUpValues];
	}

	@Override public Closure shallowClone() {
		try {
			return (Closure)clone();
		} catch(CloneNotSupportedException e) {
			throw new RuntimeException(e);
		}
	}

	@Override public void deepenClone(DeepCloneable clone,DeepCloner cloner) {
		((Closure)clone).upValues = (Pointer[])cloner.deepClone(upValues);
	}

	@Override public final Object call(LuanState ls,Object[] args) throws LuanException {
		LuanStateImpl luan = (LuanStateImpl)ls;
		Closure old = luan.closure;
		luan.closure = this;
		try {
			return doCall(luan,args);
		} finally {
			luan.closure = old;
		}	
	}

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