diff src/luan/LuanTable.java @ 1607:fa066aaa068c

nginx caching
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 30 Apr 2021 20:23:28 -0600
parents c922446f53aa
children 92beba8bf1c8
line wrap: on
line diff
--- a/src/luan/LuanTable.java	Tue Apr 20 18:06:50 2021 -0600
+++ b/src/luan/LuanTable.java	Fri Apr 30 20:23:28 2021 -0600
@@ -105,10 +105,9 @@
 	}
 
 	public String toStringLuan(Luan luan) throws LuanException {
-		Object h = getHandler(luan,"__to_string");
-		if( h == null )
+		LuanFunction fn = luan.getHandlerFunction("__to_string",this);
+		if( fn == null )
 			return rawToString();
-		LuanFunction fn = Luan.checkFunction(h);
 		return Luan.checkString( Luan.first( fn.call(luan,this) ) );
 	}
 
@@ -268,9 +267,8 @@
 	}
 
 	public int length(Luan luan) throws LuanException {
-		Object h = getHandler(luan,"__len");
-		if( h != null ) {
-			LuanFunction fn = Luan.checkFunction(h);
+		LuanFunction fn = luan.getHandlerFunction("__len",this);
+		if( fn != null ) {
 			return (Integer)Luan.first(fn.call(luan,this));
 		}
 		return rawLength();
@@ -299,9 +297,10 @@
 	}
 
 	public Iterator<Map.Entry> iterator(final Luan luan) throws LuanException {
-		if( getHandler(luan,"__pairs") == null )
+		LuanFunction h = luan.getHandlerFunction("__pairs",this);
+		if( h == null )
 			return rawIterator();
-		final LuanFunction fn = pairs(luan);
+		final LuanFunction fn = pairs(luan,h);
 		return new Iterator<Map.Entry>() {
 			private Map.Entry<Object,Object> next = getNext();
 
@@ -336,18 +335,16 @@
 	}
 
 	public LuanFunction pairs(Luan luan) throws LuanException {
-		Object h = getHandler(luan,"__pairs");
-		if( h != null ) {
-			if( h instanceof LuanFunction ) {
-				LuanFunction fn = (LuanFunction)h;
-				Object obj = Luan.first(fn.call(luan,this));
-				if( !(obj instanceof LuanFunction) )
-					throw new LuanException( "metamethod __pairs should return function but returned " + Luan.type(obj) );
-				return (LuanFunction)obj;
-			}
-			throw new LuanException( "invalid type of metamethod __pairs: " + Luan.type(h) );
-		}
-		return rawPairs();
+		return pairs( luan, luan.getHandlerFunction("__pairs",this) );
+	}
+
+	private LuanFunction pairs(Luan luan,LuanFunction fn) throws LuanException {
+		if( fn==null )
+			return rawPairs();
+		Object obj = Luan.first(fn.call(luan,this));
+		if( !(obj instanceof LuanFunction) )
+			throw new LuanException( "metamethod __pairs should return function but returned " + Luan.type(obj) );
+		return (LuanFunction)obj;
 	}
 
 	private LuanFunction rawPairs() {