Mercurial Hosting > luan
changeset 1157:50879022f49d
failed attempt to remove Http.per_session_pages
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 05 Feb 2018 01:25:42 -0700 |
parents | 3839ecc130ea |
children | 267fdf5e9fbd |
files | src/luan/modules/Thread.luan src/luan/modules/ThreadLuan.java |
diffstat | 2 files changed, 81 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
diff -r 3839ecc130ea -r 50879022f49d src/luan/modules/Thread.luan --- a/src/luan/modules/Thread.luan Sun Feb 04 23:18:51 2018 -0700 +++ b/src/luan/modules/Thread.luan Mon Feb 05 01:25:42 2018 -0700 @@ -37,4 +37,19 @@ return map.put(key,value) end +--[[ +function Thread.global_callable(name,timeout,fns) + local callable = ThreadLuan.globalCallable(name,timeout,fns) + local mt = {} + function mt.__index(_,key) + return function(...) + return callable.call(key,args) + end + end + local tbl = {} + set_metatable(tbl,mt) + return tbl +end +]] + return Thread
diff -r 3839ecc130ea -r 50879022f49d src/luan/modules/ThreadLuan.java --- a/src/luan/modules/ThreadLuan.java Sun Feb 04 23:18:51 2018 -0700 +++ b/src/luan/modules/ThreadLuan.java Mon Feb 05 01:25:42 2018 -0700 @@ -1,7 +1,9 @@ package luan.modules; import java.io.Closeable; +import java.util.Iterator; import java.util.Map; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.concurrent.Executor; import java.util.concurrent.Executors; @@ -111,4 +113,68 @@ public static void sleep(long millis) throws InterruptedException { Thread.sleep(millis); } + + +/* + private static boolean isPrimitive(Object v) { + if( v instanceof Object[] ) { + Object[] a = (Object[])v; + for( Object obj : a ) { + if( !isPrimitive(obj) ) + return false; + } + return true; + } else { + return v==null || v instanceof String || v instanceof Boolean || v instanceof Number; + } + } + + public static final class Callable { + private long expires; + private final LuanState luan = new LuanState(); + private final LuanTable fns; + + Callable(LuanState luan,LuanTable fns) { + LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); + this.fns = (LuanTable)cloner.get(fns); + } + + public synchronized Object call(String fnName,Object... args) throws LuanException { + if( !isPrimitive(args) ) + throw new LuanException("can't pass non-primitive type to global_callable"); + Object f = fns.get(luan,fnName); + if( f == null ) + throw new LuanException("function '"+fnName+"' not found in global_callable"); + if( !(f instanceof LuanFunction) ) + throw new LuanException("value of '"+fnName+"' not a function in global_callable"); + LuanFunction fn = (LuanFunction)f; + Object rtn = fn.call(luan,args); + if( !isPrimitive(rtn) ) + throw new LuanException("can't return non-primitive type from global_callable"); + return rtn; + } + } + + private static Map<String,Callable> callableMap = new HashMap<String,Callable>(); + + private static void sweep() { + long now = System.currentTimeMillis(); + for( Iterator<Callable> iter = callableMap.values().iterator(); iter.hasNext(); ) { + Callable callable = iter.next(); + if( callable.expires < now ) + iter.remove(); + } + } + + public static synchronized Callable globalCallable(LuanState luan,String name,long timeout,LuanTable fns) { + Callable callable = callableMap.get(name); + if( callable == null ) { + sweep(); + callable = new Callable(luan,fns); + callableMap.put(name,callable); + } + callable.expires = System.currentTimeMillis() + timeout; + return callable; + } +*/ }