view src/luan/LuanClosure.java @ 1330:f41919741100

fix security
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 11 Feb 2019 01:38:55 -0700
parents src/luan/impl/Closure.java@3ef883468fd0
children 25746915a241
line wrap: on
line source

package luan;

import luan.impl.Pointer;


public abstract class LuanClosure extends LuanFunction implements LuanCloneable, Cloneable {
	public Pointer[] upValues;
	public boolean javaOk;
	public final String sourceName;
	private LuanCloner cloner;

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

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

	@Override public void deepenClone(LuanCloneable dc,LuanCloner cloner) {
		LuanClosure clone = (LuanClosure)dc;
		switch( cloner.type ) {
		case COMPLETE:
			clone.upValues = (Pointer[])cloner.clone(upValues);
			return;
		case INCREMENTAL:
			clone.cloner = cloner;
			clone.upValues = upValues;
			return;
		}
	}

	private void check() {
		if( cloner != null ) {
			upValues = (Pointer[])cloner.clone(upValues);
			cloner = null;
		}
	}

	@Override public final Object call(LuanState luan,Object[] args) throws LuanException {
		if( luan.isLocked )
			throw new RuntimeException("luan is locked");
		check();
		luan.push(this);
		try {
			return doCall(luan,args);
		} catch(StackOverflowError e) {
			throw new LuanException( "stack overflow" );
		} finally {
			luan.pop();
		}	
	}

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