diff src/luan/impl/Closure.java @ 775:1a68fc55a80c

simplify dir structure
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 26 Aug 2016 14:36:40 -0600
parents core/src/luan/impl/Closure.java@f1c935be546d
children fbbdd369a13a
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/impl/Closure.java	Fri Aug 26 14:36:40 2016 -0600
@@ -0,0 +1,48 @@
+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;
+}