comparison src/luan/modules/ThreadLuan.java @ 1333:25746915a241

merge Luan and LuanState
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Feb 2019 22:33:40 -0700
parents f41919741100
children e0cf0d108a77
comparison
equal deleted inserted replaced
1332:11b7e11f9ed5 1333:25746915a241
10 import java.util.concurrent.Executors; 10 import java.util.concurrent.Executors;
11 import java.util.concurrent.ScheduledThreadPoolExecutor; 11 import java.util.concurrent.ScheduledThreadPoolExecutor;
12 import java.util.concurrent.ScheduledFuture; 12 import java.util.concurrent.ScheduledFuture;
13 import java.util.concurrent.TimeUnit; 13 import java.util.concurrent.TimeUnit;
14 import luan.Luan; 14 import luan.Luan;
15 import luan.LuanState;
16 import luan.LuanFunction; 15 import luan.LuanFunction;
17 import luan.LuanTable; 16 import luan.LuanTable;
18 import luan.LuanException; 17 import luan.LuanException;
19 import luan.LuanCloner; 18 import luan.LuanCloner;
20 import luan.LuanCloneable; 19 import luan.LuanCloneable;
22 21
23 public final class ThreadLuan { 22 public final class ThreadLuan {
24 private static final Executor exec = Executors.newCachedThreadPool(); 23 private static final Executor exec = Executors.newCachedThreadPool();
25 public static final ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(1); 24 public static final ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(1);
26 25
27 public static void fork(LuanState luan,LuanFunction fn) { 26 public static void fork(Luan luan,LuanFunction fn) {
28 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); 27 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
29 final LuanState newLuan = (LuanState)cloner.clone(luan); 28 final Luan newLuan = (Luan)cloner.clone(luan);
30 final LuanFunction newFn = (LuanFunction)cloner.get(fn); 29 final LuanFunction newFn = (LuanFunction)cloner.get(fn);
31 exec.execute(new Runnable(){public void run() { 30 exec.execute(new Runnable(){public void run() {
32 try { 31 try {
33 newFn.call(newLuan); 32 newFn.call(newLuan);
34 } catch(LuanException e) { 33 } catch(LuanException e) {
35 e.printStackTrace(); 34 e.printStackTrace();
36 } 35 }
37 }}); 36 }});
38 } 37 }
39 /* 38 /*
40 public static LuanFunction synchronized_(final LuanState luan,final LuanFunction fn) throws LuanException { 39 public static LuanFunction synchronized_(final Luan luan,final LuanFunction fn) throws LuanException {
41 Utils.checkNotNull(fn); 40 Utils.checkNotNull(fn);
42 return new LuanFunction() { 41 return new LuanFunction() {
43 @Override public Object call(LuanState ingored,Object[] args) throws LuanException { 42 @Override public Object call(Luan ingored,Object[] args) throws LuanException {
44 synchronized(luan) { 43 synchronized(luan) {
45 return fn.call(luan,args); 44 return fn.call(luan,args);
46 } 45 }
47 } 46 }
48 }; 47 };
49 } 48 }
50 */ 49 */
51 public static void schedule(LuanState luan,long delay,LuanFunction fn,String repeating) 50 public static void schedule(Luan luan,long delay,LuanFunction fn,String repeating)
52 throws LuanException 51 throws LuanException
53 { 52 {
54 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); 53 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
55 final LuanState newLuan = (LuanState)cloner.clone(luan); 54 final Luan newLuan = (Luan)cloner.clone(luan);
56 final LuanFunction newFn = (LuanFunction)cloner.get(fn); 55 final LuanFunction newFn = (LuanFunction)cloner.get(fn);
57 Runnable r = new Runnable(){public void run() { 56 Runnable r = new Runnable(){public void run() {
58 try { 57 try {
59 newFn.call(newLuan); 58 newFn.call(newLuan);
60 } catch(LuanException e) { 59 } catch(LuanException e) {
128 Unsafe(String reason) { 127 Unsafe(String reason) {
129 this.reason = reason; 128 this.reason = reason;
130 } 129 }
131 } 130 }
132 131
133 private static Object makeSafe(LuanState luan,Object v) throws LuanException { 132 private static Object makeSafe(Luan luan,Object v) throws LuanException {
134 if( v instanceof LuanTable ) { 133 if( v instanceof LuanTable ) {
135 LuanTable tbl = (LuanTable)v; 134 LuanTable tbl = (LuanTable)v;
136 if( tbl.getMetatable() != null ) 135 if( tbl.getMetatable() != null )
137 return new Unsafe("table with metatable"); 136 return new Unsafe("table with metatable");
138 LuanTable rtn = new LuanTable(luan); 137 LuanTable rtn = new LuanTable(luan);
162 } 161 }
163 } 162 }
164 163
165 public static final class Callable { 164 public static final class Callable {
166 private long expires; 165 private long expires;
167 private final LuanState luan = new LuanState(); 166 private final Luan luan = new Luan();
168 private final LuanTable fns; 167 private final LuanTable fns;
169 168
170 Callable(LuanState luan,LuanTable fns) { 169 Callable(Luan luan,LuanTable fns) {
171 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); 170 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
172 this.fns = (LuanTable)cloner.get(fns); 171 this.fns = (LuanTable)cloner.get(fns);
173 } 172 }
174 173
175 public synchronized Object call(LuanState callerLuan,String fnName,Object... args) throws LuanException { 174 public synchronized Object call(Luan callerLuan,String fnName,Object... args) throws LuanException {
176 Object obj = makeSafe(luan,args); 175 Object obj = makeSafe(luan,args);
177 if( obj instanceof Unsafe ) 176 if( obj instanceof Unsafe )
178 throw new LuanException("can't pass "+((Unsafe)obj).reason+" to global_callable "+Arrays.asList(args)); 177 throw new LuanException("can't pass "+((Unsafe)obj).reason+" to global_callable "+Arrays.asList(args));
179 args = (Object[])obj; 178 args = (Object[])obj;
180 Object f = fns.get(fnName); 179 Object f = fns.get(fnName);
200 if( callable.expires < now ) 199 if( callable.expires < now )
201 iter.remove(); 200 iter.remove();
202 } 201 }
203 } 202 }
204 203
205 public static synchronized Callable globalCallable(LuanState luan,String name,LuanTable fns,long timeout) { 204 public static synchronized Callable globalCallable(Luan luan,String name,LuanTable fns,long timeout) {
206 Callable callable = callableMap.get(name); 205 Callable callable = callableMap.get(name);
207 if( callable == null ) { 206 if( callable == null ) {
208 sweep(); 207 sweep();
209 callable = new Callable(luan,fns); 208 callable = new Callable(luan,fns);
210 callableMap.put(name,callable); 209 callableMap.put(name,callable);