Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
520:fcb9b4c8e972 | 521:8a217fe5b4f3 |
---|---|
1 package luan; | 1 package luan; |
2 | 2 |
3 import java.lang.ref.Reference; | |
4 import java.lang.ref.WeakReference; | |
3 import java.util.List; | 5 import java.util.List; |
4 import java.util.ArrayList; | 6 import java.util.ArrayList; |
5 import java.util.Map; | 7 import java.util.Map; |
6 import java.util.HashMap; | 8 import java.util.HashMap; |
7 import luan.impl.LuanCompiler; | 9 import luan.impl.LuanCompiler; |
11 public abstract class LuanState implements DeepCloneable { | 13 public abstract class LuanState implements DeepCloneable { |
12 | 14 |
13 final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>(); | 15 final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>(); |
14 | 16 |
15 private Map registry; | 17 private Map registry; |
16 private final List<LuanFunction> onClose = new ArrayList<LuanFunction>(); | 18 private final List<Reference<Runnable>> onClose = new ArrayList<Reference<Runnable>>(); |
17 | 19 |
18 protected LuanState() { | 20 protected LuanState() { |
19 registry = new HashMap(); | 21 registry = new HashMap(); |
20 } | 22 } |
21 | 23 |
31 | 33 |
32 public final Map registry() { | 34 public final Map registry() { |
33 return registry; | 35 return registry; |
34 } | 36 } |
35 | 37 |
36 public void onClose(LuanFunction fn) { | 38 public void onClose(Runnable fn) { |
37 onClose.add(fn); | 39 onClose.add(new WeakReference<Runnable>(fn)); |
38 } | 40 } |
39 | 41 |
40 public void close() throws LuanException { | 42 public void close() { |
41 for( LuanFunction fn : onClose ) { | 43 for( Reference<Runnable> ref : onClose ) { |
42 call(fn); | 44 Runnable r = ref.get(); |
45 if( r != null ) | |
46 r.run(); | |
43 } | 47 } |
44 onClose.clear(); | 48 onClose.clear(); |
45 } | 49 } |
46 | 50 |
47 public static LuanState newInstance() { | 51 public static LuanState newInstance() { |