view core/src/luan/impl/Closure.java @ 681:f1c935be546d

improve stack trace
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Apr 2016 20:10:38 -0600
parents d3e5414bdf4c
children
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;
import luan.LuanJava;


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

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

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

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

	@Override public final Object call(LuanState luan,Object[] args) throws LuanException {
		LuanJava old = luan.java;
		luan.java = java;
		try {
			return doCall(luan,args);
		} catch(StackOverflowError e) {
			throw new LuanException( "stack overflow" );
		} finally {
			luan.java = old;
		}	
	}

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