changeset 796:6b8ea0a9b7c9

remove LuanMeta
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 05 Sep 2016 19:52:49 -0600
parents dd36eae6aa04
children 86e477945676
files src/luan/LuanMeta.java src/luan/LuanPropertyMeta.java src/luan/LuanTable.java src/luan/modules/Table.luan src/luan/modules/TableLuan.java src/luan/modules/http/HttpServicer.java src/luan/modules/lucene/Lucene.luan src/luan/modules/lucene/LuceneIndex.java
diffstat 8 files changed, 49 insertions(+), 171 deletions(-) [+]
line wrap: on
line diff
diff -r dd36eae6aa04 -r 6b8ea0a9b7c9 src/luan/LuanMeta.java
--- a/src/luan/LuanMeta.java	Mon Sep 05 16:32:55 2016 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,69 +0,0 @@
-package luan;
-
-import java.util.Map;
-import java.util.Iterator;
-import java.util.Set;
-import java.util.HashSet;
-
-
-public abstract class LuanMeta {
-
-	public abstract Object __index(LuanState luan,LuanTable tbl,Object key) throws LuanException;
-
-	protected abstract Iterator keys(LuanTable tbl);
-
-	public LuanFunction __pairs(final LuanState luan,final LuanTable tbl) {
-		return new LuanFunction() {
-			final Iterator<Map.Entry<Object,Object>> iter1 = tbl.rawIterator();
-			final Iterator<Object> iter2 = keys(tbl);
-			final Set<Object> set = new HashSet<Object>();
-
-			@Override public Object[] call(LuanState luan,Object[] args) throws LuanException {
-				if( iter1.hasNext() ) {
-					Map.Entry<Object,Object> entry = iter1.next();
-					Object key = entry.getKey();
-					set.add(key);
-					return new Object[]{key,entry.getValue()};
-				}
-				while( iter2.hasNext() ) {
-					Object key = iter2.next();
-					if( set.add(key) ) {
-						Object value = __index(luan,tbl,key);
-						return new Object[]{key,value};
-					}
-				}
-				return LuanFunction.NOTHING;
-			}
-		};
-	}
-
-	public boolean canNewindex() {
-		return false;
-	}
-
-	public void __new_index(LuanState luan,LuanTable tbl,Object key,Object value) throws LuanException {
-		throw new UnsupportedOperationException();
-	}
-
-	protected abstract String type(LuanTable tbl);
-
-	public String __to_string(LuanState luan,LuanTable tbl) throws LuanException {
-		return type(tbl) + "-" + tbl.rawToString();
-	}
-
-	public LuanTable newMetatable() {
-		LuanTable mt = new LuanTable();
-		mt.rawPut( "__index", this );
-		mt.rawPut( "__pairs", this );
-		mt.rawPut( "__to_string", this );
-		if( canNewindex() )
-			mt.rawPut( "__new_index", this );
-		return mt;
-	}
-
-	public LuanTable newTable() {
-		LuanTable tbl = new LuanTable();
-		tbl.setMetatable( newMetatable() );
-		return tbl;
-	}
-}
diff -r dd36eae6aa04 -r 6b8ea0a9b7c9 src/luan/LuanPropertyMeta.java
--- a/src/luan/LuanPropertyMeta.java	Mon Sep 05 16:32:55 2016 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,75 +0,0 @@
-package luan;
-
-import java.util.Map;
-import java.util.Iterator;
-
-
-public final class LuanPropertyMeta extends LuanMeta {
-	public static final LuanPropertyMeta INSTANCE = new LuanPropertyMeta();
-
-	private LuanPropertyMeta() {}
-
-	public LuanTable getters(LuanTable tbl) {
-		return (LuanTable)tbl.getMetatable().rawGet("get");
-	}
-
-	public LuanTable setters(LuanTable tbl) {
-		return (LuanTable)tbl.getMetatable().rawGet("set");
-	}
-
-	protected String type(LuanTable tbl) {
-		return (String)tbl.getMetatable().rawGet("type");
-	}
-
-	@Override public Object __index(LuanState luan,LuanTable tbl,Object key) throws LuanException {
-		Object obj = getters(tbl).rawGet(key);
-		if( obj == null )
-			return null;
-		if( !(obj instanceof LuanFunction) )
-			throw new LuanException("get for '"+key+"' isn't a function");
-		LuanFunction fn = (LuanFunction)obj;
-		return fn.call(luan);
-	}
-
-	@Override protected Iterator keys(final LuanTable tbl) {
-		return new Iterator() {
-			final Iterator<Map.Entry<Object,Object>> iter = getters(tbl).rawIterator();
-
-			@Override public boolean hasNext() {
-				return iter.hasNext();
-			}
-			@Override public Object next() {
-				return iter.next().getKey();
-			}
-			@Override public void remove() {
-				throw new UnsupportedOperationException();
-			}
-		};
-	}
-
-
-	@Override public boolean canNewindex() {
-		return true;
-	}
-
-	@Override public void __new_index(LuanState luan,LuanTable tbl,Object key,Object value) throws LuanException {
-		Object obj = setters(tbl).rawGet(key);
-		if( obj == null ) {
-			tbl.rawPut(key,value);
-			return;
-		}
-		if( !(obj instanceof LuanFunction) )
-			throw new LuanException("set for '"+key+"' isn't a function");
-		LuanFunction fn = (LuanFunction)obj;
-		fn.call(luan,new Object[]{value});
-	}
-
-	@Override public LuanTable newMetatable() {
-		LuanTable mt = super.newMetatable();
-		mt.rawPut( "get", new LuanTable() );
-		mt.rawPut( "set", new LuanTable() );
-		mt.rawPut( "type", "property" );
-		return mt;
-	}
-
-}
diff -r dd36eae6aa04 -r 6b8ea0a9b7c9 src/luan/LuanTable.java
--- a/src/luan/LuanTable.java	Mon Sep 05 16:32:55 2016 -0600
+++ b/src/luan/LuanTable.java	Mon Sep 05 19:52:49 2016 -0600
@@ -117,10 +117,6 @@
 		Object h = getHandler("__to_string");
 		if( h == null )
 			return rawToString();
-		if( h instanceof LuanMeta ) {
-			LuanMeta meta = (LuanMeta)h;
-			return meta.__to_string(luan,this);
-		}
 		LuanFunction fn = Luan.checkFunction(h);
 		return Luan.checkString( Luan.first( fn.call(luan,new Object[]{this}) ) );
 	}
@@ -140,10 +136,6 @@
 			LuanFunction fn = (LuanFunction)h;
 			return Luan.first(fn.call(luan,new Object[]{this,key}));
 		}
-		if( h instanceof LuanMeta ) {
-			LuanMeta meta = (LuanMeta)h;
-			return meta.__index(luan,this,key);
-		}
 		return luan.index(h,key);
 	}
 
@@ -177,11 +169,6 @@
 			fn.call(luan,new Object[]{this,key,value});
 			return;
 		}
-		if( h instanceof LuanMeta ) {
-			LuanMeta meta = (LuanMeta)h;
-			meta.__new_index(luan,this,key,value);
-			return;
-		}
 		if( h instanceof LuanTable ) {
 			LuanTable tbl = (LuanTable)h;
 			tbl.put(luan,key,value);
@@ -354,10 +341,6 @@
 					throw new LuanException( "metamethod __pairs should return function but returned " + Luan.type(obj) );
 				return (LuanFunction)obj;
 			}
-			if( h instanceof LuanMeta ) {
-				LuanMeta meta = (LuanMeta)h;
-				return meta.__pairs(luan,this);
-			}
 			throw new LuanException( "invalid type of metamethod __pairs: " + Luan.type(h) );
 		}
 		return rawPairs();
diff -r dd36eae6aa04 -r 6b8ea0a9b7c9 src/luan/modules/Table.luan
--- a/src/luan/modules/Table.luan	Mon Sep 05 16:32:55 2016 -0600
+++ b/src/luan/modules/Table.luan	Mon Sep 05 19:52:49 2016 -0600
@@ -7,7 +7,6 @@
 M.concat = TableLuan.concat
 M.copy = TableLuan.copy
 M.insert = TableLuan.insert
-M.new_property_table = TableLuan.new_property_table
 M.pack = TableLuan.pack
 M.remove = TableLuan.remove
 M.sort = TableLuan.sort
diff -r dd36eae6aa04 -r 6b8ea0a9b7c9 src/luan/modules/TableLuan.java
--- a/src/luan/modules/TableLuan.java	Mon Sep 05 16:32:55 2016 -0600
+++ b/src/luan/modules/TableLuan.java	Mon Sep 05 19:52:49 2016 -0600
@@ -11,7 +11,6 @@
 import luan.LuanException;
 import luan.LuanRuntimeException;
 import luan.LuanMethod;
-import luan.LuanPropertyMeta;
 
 
 public final class TableLuan {
@@ -109,10 +108,6 @@
 		return list.rawSubList(from,to);
 	}
 
-	public static LuanTable new_property_table() {
-		return LuanPropertyMeta.INSTANCE.newTable();
-	}
-
 	public static void clear(LuanTable tbl) {
 		tbl.rawClear();
 	}
diff -r dd36eae6aa04 -r 6b8ea0a9b7c9 src/luan/modules/http/HttpServicer.java
--- a/src/luan/modules/http/HttpServicer.java	Mon Sep 05 16:32:55 2016 -0600
+++ b/src/luan/modules/http/HttpServicer.java	Mon Sep 05 19:52:49 2016 -0600
@@ -28,7 +28,7 @@
 import luan.LuanFunction;
 import luan.LuanException;
 import luan.LuanTable;
-import luan.LuanPropertyMeta;
+//import luan.LuanPropertyMeta;
 import luan.LuanCloner;
 import luan.modules.PackageLuan;
 import luan.modules.IoLuan;
@@ -126,6 +126,7 @@
 					if( filename == null ) {
 						value = new String(part.getBytes());
 					} else {
+/*
 						LuanTable partTbl = LuanPropertyMeta.INSTANCE.newTable();
 						partTbl.rawPut("filename",filename);
 						partTbl.rawPut("content_type",part.getContentType());
@@ -141,6 +142,28 @@
 								}
 							}
 						} );
+*/
+						LuanTable partTbl = new LuanTable();
+						partTbl.rawPut("filename",filename);
+						partTbl.rawPut("content_type",part.getContentType());
+						LuanTable mt = new LuanTable();
+						partTbl.setMetatable(mt);
+						mt.rawPut( "__index", new LuanFunction() {
+							@Override public Object call(LuanState luan,Object[] args) throws LuanException {
+								Object key = args[1];
+								if( "content".equals(key) ) {
+									try {
+										InputStream in = part.getInputStream();
+										byte[] content = Utils.readAll(in);
+										in.close();
+										return content;
+									} catch(IOException e) {
+										throw new RuntimeException(e);
+									}
+								}
+								return null;
+							}
+						} );
 						value = partTbl;
 					}
 					LuanTable list = (LuanTable)parametersTbl.rawGet(name);
diff -r dd36eae6aa04 -r 6b8ea0a9b7c9 src/luan/modules/lucene/Lucene.luan
--- a/src/luan/modules/lucene/Lucene.luan	Mon Sep 05 16:32:55 2016 -0600
+++ b/src/luan/modules/lucene/Lucene.luan	Mon Sep 05 19:52:49 2016 -0600
@@ -3,6 +3,7 @@
 local error = Luan.error
 local ipairs = Luan.ipairs or error()
 local type = Luan.type or error()
+local set_metatable = Luan.set_metatable or error()
 local Html = require "luan:Html.luan"
 local Io = require "luan:Io.luan"
 local uri = Io.uri or error()
@@ -36,7 +37,17 @@
 	local index = {}
 	index.dir = index_dir
 	local java_index = LuceneIndex.new(index_dir,default_type,default_fields)
-	index.indexed_fields = java_index.indexedFieldsMeta.newTable()
+--	index.indexed_fields = java_index.indexedFieldsMeta.newTable()
+
+	index.indexed_fields = {}
+	local mt = {}
+	set_metatable(index.indexed_fields,mt)
+	function mt.__index(_,key)
+		return java_index.getIndexedFieldParser(key)
+	end
+	function mt.__new_index(_,key,value)
+		return java_index.setIndexedFieldParser(key,value)
+	end
 
 	-- index.indexed_only_fields[type][field] = fn(doc)
 	index.indexed_only_fields = java_index.indexed_only_fields
diff -r dd36eae6aa04 -r 6b8ea0a9b7c9 src/luan/modules/lucene/LuceneIndex.java
--- a/src/luan/modules/lucene/LuceneIndex.java	Mon Sep 05 16:32:55 2016 -0600
+++ b/src/luan/modules/lucene/LuceneIndex.java	Mon Sep 05 19:52:49 2016 -0600
@@ -71,7 +71,7 @@
 import luan.LuanTable;
 import luan.LuanFunction;
 import luan.LuanException;
-import luan.LuanMeta;
+//import luan.LuanMeta;
 import luan.LuanRuntimeException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -456,7 +456,7 @@
 	}
 
 
-
+/*
 	public final LuanMeta indexedFieldsMeta = new LuanMeta() {
 
 		@Override public boolean canNewindex() {
@@ -490,7 +490,18 @@
 		}
 
 	};
+*/
+	public FieldParser getIndexedFieldParser(String field) {
+		return mfp.fields.get(field);
+	}
 
+	public void setIndexedFieldParser(String field,FieldParser fp) {
+		if( fp==null ) {  // delete
+			mfp.fields.remove(field);
+			return;
+		}
+		mfp.fields.put( field, fp );
+	}
 
 
 	private IndexableField newField(String name,Object value,Field.Store store,Set<String> indexed)