diff src/luan/LuanClosure.java @ 1563:8fbcc4747091

remove LuanFunction.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 09 Nov 2020 01:37:57 -0700
parents e1a13e707bf3
children c922446f53aa
line wrap: on
line diff
--- a/src/luan/LuanClosure.java	Sun Nov 08 16:50:59 2020 -0700
+++ b/src/luan/LuanClosure.java	Mon Nov 09 01:37:57 2020 -0700
@@ -3,31 +3,68 @@
 import luan.impl.Pointer;
 
 
-public abstract class LuanClosure extends LuanFunction {
+public abstract class LuanClosure extends LuanFunction implements LuanCloneable, Cloneable {
 	public Pointer[] upValues;
 	public boolean javaOk;
 	public final String sourceName;
+	private LuanCloner cloner;
+	private Luan luan;
 
-	public LuanClosure(Luan luan,Pointer[] upValues,boolean javaOk,String sourceName) throws LuanException {
-		super(luan);
+	public LuanClosure(Pointer[] upValues,boolean javaOk,String sourceName) throws LuanException {
 		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 final LuanClosure shallowClone() {
+		check();
+		try {
+			return (LuanClosure)clone();
+		} catch(CloneNotSupportedException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	private void check() {
+		if( cloner != null ) {
+			completeClone(this,cloner);
+			cloner = null;
+		}
+	}
+
+	private void checkLuan(Luan luan) {
+		check();
+		if( this.luan==null ) {
+			this.luan = luan;
+		} else if( this.luan != luan ) {
+			throw new RuntimeException("wrong luan");
+		}
 	}
 
-	@Override public void makeImmutable(LuanImmutabler immutabler) throws LuanException {
-		immutabler.makeImmutable(upValues);
-		super.makeImmutable(immutabler);
+	@Override public final void deepenClone(LuanCloneable dc,LuanCloner cloner) {
+		LuanClosure clone = (LuanClosure)dc;
+		switch( cloner.type ) {
+		case COMPLETE:
+			completeClone(clone,cloner);
+			return;
+		case INCREMENTAL:
+			clone.cloner = cloner;
+			return;
+		}
 	}
 
-	@Override public final Object call(Object... args) throws LuanException {
-		Luan luan = luan();
+	private void completeClone(LuanClosure dc,LuanCloner cloner) {
+		LuanClosure clone = (LuanClosure)dc;
+		clone.upValues = (Pointer[])cloner.clone(upValues);
+		clone.luan = (Luan)cloner.clone(luan);
+	}
+
+	@Override public final void makeImmutable(LuanImmutabler immutabler) throws LuanException {
+		immutabler.makeImmutable(upValues);
+	}
+
+	@Override public final Object call(Luan luan,Object... args) throws LuanException {
+		check();
 		luan.push(this);
 		try {
 			return doCall(luan,args);