Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
1263:382c444a6c77 | 1264:d41997776788 |
---|---|
1 package luan; | 1 package luan; |
2 | 2 |
3 import java.io.Closeable; | 3 import java.io.Closeable; |
4 import java.io.IOException; | 4 import java.io.IOException; |
5 import java.lang.ref.Reference; | |
6 import java.lang.ref.WeakReference; | |
7 import java.util.List; | |
8 import java.util.ArrayList; | |
9 import java.util.Map; | 5 import java.util.Map; |
10 import java.util.HashMap; | 6 import java.util.HashMap; |
11 import org.slf4j.Logger; | 7 import org.slf4j.Logger; |
12 import org.slf4j.LoggerFactory; | 8 import org.slf4j.LoggerFactory; |
13 import luan.impl.LuanCompiler; | 9 import luan.impl.LuanCompiler; |
18 public final class LuanState implements LuanCloneable { | 14 public final class LuanState implements LuanCloneable { |
19 private static final Logger logger = LoggerFactory.getLogger(LuanState.class); | 15 private static final Logger logger = LoggerFactory.getLogger(LuanState.class); |
20 | 16 |
21 public LuanJavaOk javaOk; | 17 public LuanJavaOk javaOk; |
22 private Map registry; | 18 private Map registry; |
23 private final List<Reference<Closeable>> onClose = new ArrayList<Reference<Closeable>>(); | |
24 public boolean isLocked = false; | 19 public boolean isLocked = false; |
20 | |
21 public interface OnClose extends Closeable { | |
22 public void onClose(Closeable c); | |
23 } | |
24 public OnClose onClose; | |
25 | 25 |
26 public LuanState() { | 26 public LuanState() { |
27 javaOk = new LuanJavaOk(); | 27 javaOk = new LuanJavaOk(); |
28 registry = new HashMap(); | 28 registry = new HashMap(); |
29 } | 29 } |
45 public Map registry() { | 45 public Map registry() { |
46 return registry; | 46 return registry; |
47 } | 47 } |
48 | 48 |
49 public void onClose(Closeable c) { | 49 public void onClose(Closeable c) { |
50 onClose.add(new WeakReference<Closeable>(c)); | 50 if( onClose != null ) |
51 } | 51 onClose.onClose(c); |
52 | |
53 public void close() { | |
54 for( Reference<Closeable> ref : onClose ) { | |
55 Closeable c = ref.get(); | |
56 if( c != null ) { | |
57 try { | |
58 c.close(); | |
59 } catch(IOException e) { | |
60 logger.error(c.toString(),e); | |
61 } | |
62 } | |
63 } | |
64 onClose.clear(); | |
65 } | |
66 | |
67 protected void finalize() throws Throwable { | |
68 close(); | |
69 } | 52 } |
70 | 53 |
71 public final Object eval(String cmd,Object... args) throws LuanException { | 54 public final Object eval(String cmd,Object... args) throws LuanException { |
72 return Luan.load(cmd,"eval").call(this,args); | 55 return Luan.load(cmd,"eval").call(this,args); |
73 } | 56 } |