changeset 1166:7ef40e1923b7

add back Thread.global allow metatables to have metatables
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 08 Feb 2018 02:22:51 -0700
parents 668f29bc52ea
children 7e6f28c769a1
files conv.txt src/luan/Luan.java src/luan/LuanState.java src/luan/LuanTable.java src/luan/impl/LuanImpl.java src/luan/modules/BasicLuan.java src/luan/modules/TableLuan.java src/luan/modules/Thread.luan src/luan/modules/ThreadLuan.java src/luan/modules/http/Implementation.luan src/luan/modules/http/impl/Http.luan
diffstat 11 files changed, 88 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/conv.txt	Wed Feb 07 23:16:12 2018 -0700
+++ b/conv.txt	Thu Feb 08 02:22:51 2018 -0700
@@ -7,5 +7,4 @@
 "request.parameter"
 "request.cookie"
 "set_cookie"
-"Thread.global"
 "assert"
--- a/src/luan/Luan.java	Wed Feb 07 23:16:12 2018 -0700
+++ b/src/luan/Luan.java	Thu Feb 08 02:22:51 2018 -0700
@@ -114,39 +114,6 @@
 		throw new LuanException("attempt to call a " + Luan.type(obj) + " value" );
 	}
 
-	public static boolean isLessThan(LuanState luan,Object o1,Object o2) throws LuanException {
-		if( o1 instanceof Number && o2 instanceof Number ) {
-			Number n1 = (Number)o1;
-			Number n2 = (Number)o2;
-			return n1.doubleValue() < n2.doubleValue();
-		}
-		if( o1 instanceof String && o2 instanceof String ) {
-			String s1 = (String)o1;
-			String s2 = (String)o2;
-			return s1.compareTo(s2) < 0;
-		}
-		LuanFunction fn = getBinHandler("__lt",o1,o2);
-		if( fn != null )
-			return checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) );
-		throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
-	}
-
-	public static LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException {
-		if( o1 instanceof LuanTable ) {
-			LuanFunction f1 = getHandlerFunction(op,(LuanTable)o1);
-			if( f1 != null )
-				return f1;
-		}
-		return o2 instanceof LuanTable ? getHandlerFunction(op,(LuanTable)o2) : null;
-	}
-
-	public static LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException {
-		Object f = t.getHandler(op);
-		if( f == null )
-			return null;
-		return checkFunction(f);
-	}
-
 	public static LuanFunction load(String text,String sourceName,LuanTable env)
 		throws LuanException
 	{
--- a/src/luan/LuanState.java	Wed Feb 07 23:16:12 2018 -0700
+++ b/src/luan/LuanState.java	Thu Feb 08 02:22:51 2018 -0700
@@ -96,4 +96,38 @@
 		throw new LuanException( "attempt to perform arithmetic on '"+context()+"' (a " + Luan.type(obj) + " value)" );
 	}
 */
+
+
+	public boolean isLessThan(Object o1,Object o2) throws LuanException {
+		if( o1 instanceof Number && o2 instanceof Number ) {
+			Number n1 = (Number)o1;
+			Number n2 = (Number)o2;
+			return n1.doubleValue() < n2.doubleValue();
+		}
+		if( o1 instanceof String && o2 instanceof String ) {
+			String s1 = (String)o1;
+			String s2 = (String)o2;
+			return s1.compareTo(s2) < 0;
+		}
+		LuanFunction fn = getBinHandler("__lt",o1,o2);
+		if( fn != null )
+			return Luan.checkBoolean( Luan.first(fn.call(this,new Object[]{o1,o2})) );
+		throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
+	}
+
+	public LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException {
+		if( o1 instanceof LuanTable ) {
+			LuanFunction f1 = getHandlerFunction(op,(LuanTable)o1);
+			if( f1 != null )
+				return f1;
+		}
+		return o2 instanceof LuanTable ? getHandlerFunction(op,(LuanTable)o2) : null;
+	}
+
+	public LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException {
+		Object f = t.getHandler(this,op);
+		if( f == null )
+			return null;
+		return Luan.checkFunction(f);
+	}
 }
--- a/src/luan/LuanTable.java	Wed Feb 07 23:16:12 2018 -0700
+++ b/src/luan/LuanTable.java	Thu Feb 08 02:22:51 2018 -0700
@@ -114,7 +114,7 @@
 	}
 
 	public String toString(LuanState luan) throws LuanException {
-		Object h = getHandler("__to_string");
+		Object h = getHandler(luan,"__to_string");
 		if( h == null )
 			return rawToString();
 		LuanFunction fn = Luan.checkFunction(h);
@@ -129,7 +129,7 @@
 		Object value = rawGet(key);
 		if( value != null )
 			return value;
-		Object h = getHandler("__index");
+		Object h = getHandler(luan,"__index");
 		if( h==null )
 			return null;
 		if( h instanceof LuanFunction ) {
@@ -159,7 +159,7 @@
 	}
 
 	public void put(LuanState luan,Object key,Object value) throws LuanException {
-		Object h = getHandler("__new_index");
+		Object h = getHandler(luan,"__new_index");
 		if( h==null || rawGet(key)!=null ) {
 			rawPut(key,value);
 			return;
@@ -263,7 +263,7 @@
 	}
 
 	public int length(LuanState luan) throws LuanException {
-		Object h = getHandler("__len");
+		Object h = getHandler(luan,"__len");
 		if( h != null ) {
 			LuanFunction fn = Luan.checkFunction(h);
 			return (Integer)Luan.first(fn.call(luan,new Object[]{this}));
@@ -295,7 +295,7 @@
 	}
 
 	public Iterator<Map.Entry> iterator(final LuanState luan) throws LuanException {
-		if( getHandler("__pairs") == null )
+		if( getHandler(luan,"__pairs") == null )
 			return rawIterator();
 		final LuanFunction fn = pairs(luan);
 		return new Iterator<Map.Entry>() {
@@ -332,7 +332,7 @@
 	}
 
 	public LuanFunction pairs(LuanState luan) throws LuanException {
-		Object h = getHandler("__pairs");
+		Object h = getHandler(luan,"__pairs");
 		if( h != null ) {
 			if( h instanceof LuanFunction ) {
 				LuanFunction fn = (LuanFunction)h;
@@ -430,9 +430,9 @@
 		this.metatable = metatable;
 	}
 
-	public Object getHandler(String op) {
+	public Object getHandler(LuanState luan,String op) throws LuanException {
 		check();
-		return metatable==null ? null : metatable.rawGet(op);
+		return metatable==null ? null : metatable.get(luan,op);
 	}
 
 	private Map<Object,Object> newMap() {
--- a/src/luan/impl/LuanImpl.java	Wed Feb 07 23:16:12 2018 -0700
+++ b/src/luan/impl/LuanImpl.java	Thu Feb 08 02:22:51 2018 -0700
@@ -34,7 +34,7 @@
 		if( o instanceof Number )
 			return -((Number)o).doubleValue();
 		if( o instanceof LuanTable ) {
-			LuanFunction fn = Luan.getHandlerFunction("__unm",(LuanTable)o);
+			LuanFunction fn = luan.getHandlerFunction("__unm",(LuanTable)o);
 			if( fn != null ) {
 				return Luan.first(fn.call(luan,new Object[]{o}));
 			}
@@ -43,7 +43,7 @@
 	}
 
 	private static Object arithmetic(LuanState luan,String op,Object o1,Object o2) throws LuanException {
-		LuanFunction fn = Luan.getBinHandler(op,o1,o2);
+		LuanFunction fn = luan.getBinHandler(op,o1,o2);
 		if( fn != null )
 			return Luan.first(fn.call(luan,new Object[]{o1,o2}));
 		String type = !(o1 instanceof Number) ? Luan.type(o1) : Luan.type(o2);
@@ -90,7 +90,7 @@
 	}
 
 	public static Object concat(LuanState luan,Object o1,Object o2) throws LuanException {
-		LuanFunction fn = Luan.getBinHandler("__concat",o1,o2);
+		LuanFunction fn = luan.getBinHandler("__concat",o1,o2);
 		if( fn != null )
 			return Luan.first(fn.call(luan,new Object[]{o1,o2}));
 		String s1 = luan.toString(o1);
@@ -137,17 +137,17 @@
 			String s2 = (String)o2;
 			return s1.compareTo(s2) <= 0;
 		}
-		LuanFunction fn = Luan.getBinHandler("__le",o1,o2);
+		LuanFunction fn = luan.getBinHandler("__le",o1,o2);
 		if( fn != null )
 			return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) );
-		fn = Luan.getBinHandler("__lt",o1,o2);
+		fn = luan.getBinHandler("__lt",o1,o2);
 		if( fn != null )
 			return !Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o2,o1})) );
 		throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
 	}
 
 	public static boolean lt(LuanState luan,Object o1,Object o2) throws LuanException {
-		return Luan.isLessThan(luan,o1,o2);
+		return luan.isLessThan(o1,o2);
 	}
 
 	public static boolean cnd(Object o) throws LuanException {
--- a/src/luan/modules/BasicLuan.java	Wed Feb 07 23:16:12 2018 -0700
+++ b/src/luan/modules/BasicLuan.java	Thu Feb 08 02:22:51 2018 -0700
@@ -71,9 +71,9 @@
 		return obj!=null ? obj : metatable;
 	}
 
-	public static void set_metatable(LuanTable table,LuanTable metatable) throws LuanException {
+	public static void set_metatable(LuanState luan,LuanTable table,LuanTable metatable) throws LuanException {
 		Utils.checkNotNull(table);
-		if( table.getHandler("__metatable") != null )
+		if( table.getHandler(luan,"__metatable") != null )
 			throw new LuanException("cannot change a protected metatable");
 		table.setMetatable(metatable);
 	}
--- a/src/luan/modules/TableLuan.java	Wed Feb 07 23:16:12 2018 -0700
+++ b/src/luan/modules/TableLuan.java	Thu Feb 08 02:22:51 2018 -0700
@@ -56,7 +56,7 @@
 			lt = new LessThan() {
 				public boolean isLessThan(Object o1,Object o2) {
 					try {
-						return Luan.isLessThan(luan,o1,o2);
+						return luan.isLessThan(o1,o2);
 					} catch(LuanException e) {
 						throw new LuanRuntimeException(e);
 					}
--- a/src/luan/modules/Thread.luan	Wed Feb 07 23:16:12 2018 -0700
+++ b/src/luan/modules/Thread.luan	Thu Feb 08 02:22:51 2018 -0700
@@ -53,4 +53,35 @@
 
 Thread.remove_global_callable = ThreadLuan.removeGlobalCallable
 
+
+local map = {}
+local fns = {}
+
+function fns.get(key)
+	return map[key]
+end
+
+function fns.put(key,value)
+	map[key] = value
+end
+
+Thread.global = {}  -- shared across threads
+local global_mt = {}
+set_metatable(Thread.global,global_mt)
+
+function global_mt.__index(_,key)
+	return global_mt.fns.get(key)
+end
+
+function global_mt.__new_index(_,key,value)
+	global_mt.fns.put(key,value)
+end
+
+function Thread.set_global_timeout(timeout)
+	global_mt.fns = Thread.global_callable("Thread.global",timeout,fns)
+end
+
+Thread.set_global_timeout(60000)  -- one minute
+
+
 return Thread
--- a/src/luan/modules/ThreadLuan.java	Wed Feb 07 23:16:12 2018 -0700
+++ b/src/luan/modules/ThreadLuan.java	Thu Feb 08 02:22:51 2018 -0700
@@ -1,6 +1,7 @@
 package luan.modules;
 
 import java.io.Closeable;
+import java.util.Arrays;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.HashMap;
@@ -140,7 +141,7 @@
 
 		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");
+				throw new LuanException("can't pass non-primitive type to global_callable "+Arrays.asList(args));
 			Object f = fns.get(luan,fnName);
 			if( f == null )
 				throw new LuanException("function '"+fnName+"' not found in global_callable");
--- a/src/luan/modules/http/Implementation.luan	Wed Feb 07 23:16:12 2018 -0700
+++ b/src/luan/modules/http/Implementation.luan	Thu Feb 08 02:22:51 2018 -0700
@@ -1,4 +1,4 @@
 return {
---	luan = "luan:http/jetty/"
-	luan = "luan:http/impl/"
+	luan = "luan:http/jetty/"
+--	luan = "luan:http/impl/"
 }
--- a/src/luan/modules/http/impl/Http.luan	Wed Feb 07 23:16:12 2018 -0700
+++ b/src/luan/modules/http/impl/Http.luan	Thu Feb 08 02:22:51 2018 -0700
@@ -11,6 +11,7 @@
 local clear = Table.clear or error()
 local Package = require "luan:Package.luan"
 local String = require "luan:String.luan"
+local lower = String.lower or error()
 local matches = String.matches or error()
 local HttpServicer = require "java:luan.modules.http.impl.HttpServicer"
 local IoLuan = require "java:luan.modules.IoLuan"
@@ -137,6 +138,7 @@
 	java.status = Status.getStatus(response.status)
 	for name, value in pairs(response.headers) do
 		type(name)=="string" or "header name must be string"
+		name = lower(name)
 		value = JavaLuan.toJava(value)
 		java.headers.put(name,value)
 	end