diff core/src/luan/LuanState.java @ 521:8a217fe5b4f3

cleaner LuanState.onClose()
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 27 May 2015 03:12:28 -0600
parents 8dcf9e12446b
children ef0336efe33c
line wrap: on
line diff
--- a/core/src/luan/LuanState.java	Wed May 27 01:30:25 2015 -0600
+++ b/core/src/luan/LuanState.java	Wed May 27 03:12:28 2015 -0600
@@ -1,5 +1,7 @@
 package luan;
 
+import java.lang.ref.Reference;
+import java.lang.ref.WeakReference;
 import java.util.List;
 import java.util.ArrayList;
 import java.util.Map;
@@ -13,7 +15,7 @@
 	final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();
 
 	private Map registry;
-	private final List<LuanFunction> onClose = new ArrayList<LuanFunction>();
+	private final List<Reference<Runnable>> onClose = new ArrayList<Reference<Runnable>>();
 
 	protected LuanState() {
 		registry = new HashMap();
@@ -33,13 +35,15 @@
 		return registry;
 	}
 
-	public void onClose(LuanFunction fn) {
-		onClose.add(fn);
+	public void onClose(Runnable fn) {
+		onClose.add(new WeakReference<Runnable>(fn));
 	}
 
-	public void close() throws LuanException {
-		for( LuanFunction fn : onClose ) {
-			call(fn);
+	public void close() {
+		for( Reference<Runnable> ref : onClose ) {
+			Runnable r = ref.get();
+			if( r != null )
+				r.run();
 		}
 		onClose.clear();
 	}