diff src/luan/modules/parsers/LuanToString.java @ 1607:fa066aaa068c

nginx caching
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 30 Apr 2021 20:23:28 -0600
parents 8fbcc4747091
children 8852f34a646a
line wrap: on
line diff
--- a/src/luan/modules/parsers/LuanToString.java	Tue Apr 20 18:06:50 2021 -0600
+++ b/src/luan/modules/parsers/LuanToString.java	Fri Apr 30 20:23:28 2021 -0600
@@ -7,6 +7,7 @@
 import java.util.Collections;
 import luan.Luan;
 import luan.LuanTable;
+import luan.LuanFunction;
 import luan.LuanException;
 import luan.LuanRuntimeException;
 
@@ -62,6 +63,7 @@
 	}
 
 	public final Settings settingsInit = new Settings();
+	public Luan luan = null;
 	private final LuanTable subOptions;
 
 	public LuanToString(LuanTable options,LuanTable subOptions) throws LuanException {
@@ -119,6 +121,46 @@
 	}
 
 	private void toString(LuanTable tbl,StringBuilder sb,int indented,Settings settings) throws LuanException {
+		if( tbl.getMetatable()!=null ) {
+			if( settings.strict )
+				throw new LuanException("can't handle metatables when strict");
+			if( luan==null )
+				throw new LuanException("can't handle metatables when luan isn't set");
+		}
+		LuanFunction pairs = luan.getHandlerFunction("__pairs",tbl);
+		if( pairs != null ) {
+			sb.append( '{' );
+			boolean first = true;
+			for( Object obj : tbl.iterable(luan) ) {
+				Map.Entry entry = (Map.Entry)obj;
+				if( settings.compressed ) {
+					if( first )
+						first = false;
+					else
+						sb.append( ',' );
+				} else if( settings.inline ) {
+					if( first ) {
+						first = false;
+						sb.append( ' ' );
+					} else
+						sb.append( ", " );
+				} else {
+					first = false;
+					indent(sb,indented+1);
+				}
+				toString(entry,sb,indented+1,settings);
+			}
+			if( !first ) {
+				if( settings.compressed ) {
+				} else if( settings.inline ) {
+					sb.append( ' ' );
+				} else {
+					indent(sb,indented);
+				}
+			}
+			sb.append( '}' );
+			return;
+		}
 		List list = tbl.asList();
 		Map map = tbl.rawMap();
 		sb.append( '{' );