diff src/luan/LuanState.java @ 1264:d41997776788

fix onClose issues
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 25 Sep 2018 17:03:57 -0600
parents cb422386f6b5
children 9fa8b8389578
line wrap: on
line diff
--- a/src/luan/LuanState.java	Mon Sep 24 22:06:25 2018 -0600
+++ b/src/luan/LuanState.java	Tue Sep 25 17:03:57 2018 -0600
@@ -2,10 +2,6 @@
 
 import java.io.Closeable;
 import java.io.IOException;
-import java.lang.ref.Reference;
-import java.lang.ref.WeakReference;
-import java.util.List;
-import java.util.ArrayList;
 import java.util.Map;
 import java.util.HashMap;
 import org.slf4j.Logger;
@@ -20,9 +16,13 @@
 
 	public LuanJavaOk javaOk;
 	private Map registry;
-	private final List<Reference<Closeable>> onClose = new ArrayList<Reference<Closeable>>();
 	public boolean isLocked = false;
 
+	public interface OnClose extends Closeable {
+		public void onClose(Closeable c);
+	}
+	public OnClose onClose;
+
 	public LuanState() {
 		javaOk = new LuanJavaOk();
 		registry = new HashMap();
@@ -47,25 +47,8 @@
 	}
 
 	public void onClose(Closeable c) {
-		onClose.add(new WeakReference<Closeable>(c));
-	}
-
-	public void close() {
-		for( Reference<Closeable> ref : onClose ) {
-			Closeable c = ref.get();
-			if( c != null ) {
-				try {
-					c.close();
-				} catch(IOException e) {
-					logger.error(c.toString(),e);
-				}
-			}
-		}
-		onClose.clear();
-	}
-
-	protected void finalize() throws Throwable {
-		close();
+		if( onClose != null )
+			onClose.onClose(c);
 	}
 
 	public final Object eval(String cmd,Object... args) throws LuanException {