diff core/src/luan/modules/BasicLuan.java @ 413:8937263f59f6

add __pairs; add back HttpServicer.get_parameter_values;
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 29 Apr 2015 19:01:18 -0600
parents d55e873e1f0d
children 8fbb961aabd5
line wrap: on
line diff
--- a/core/src/luan/modules/BasicLuan.java	Wed Apr 29 16:28:18 2015 -0600
+++ b/core/src/luan/modules/BasicLuan.java	Wed Apr 29 19:01:18 2015 -0600
@@ -16,6 +16,7 @@
 import luan.LuanSource;
 import luan.LuanElement;
 import luan.LuanMethod;
+import luan.LuanMeta;
 import luan.impl.LuanCompiler;
 
 
@@ -49,8 +50,22 @@
 
 	public static LuanFunction pairs(LuanState luan,final LuanTable t) throws LuanException {
 		Utils.checkNotNull(luan,t);
+		Object obj = luan.getHandler("__pairs",t);
+		if( obj != null ) {
+			if( obj instanceof LuanFunction ) {
+				obj = Luan.first(luan.call((LuanFunction)obj,"__pairs",new Object[]{t}));
+				if( !(obj instanceof LuanFunction) )
+					throw luan.exception( "metamethod __pairs should return function but returned " + Luan.type(obj) );
+				return (LuanFunction)obj;
+			}
+			if( obj instanceof LuanMeta ) {
+				LuanMeta meta = (LuanMeta)obj;
+				return meta.__pairs(luan,t);
+			}
+			throw luan.exception( "invalid type of metamethod __pairs: " + Luan.type(obj) );
+		}
 		return new LuanFunction() {
-			Iterator<Map.Entry<Object,Object>> iter = t.iterator();
+			final Iterator<Map.Entry<Object,Object>> iter = t.iterator();
 
 			@Override public Object[] call(LuanState luan,Object[] args) {
 				if( !iter.hasNext() )