changeset 415:ce8e19567911

make LuanPropertyMeta a singleton
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 30 Apr 2015 06:04:17 -0600
parents d2ab2240cc65
children 91af5337b9ae
files core/src/luan/LuanMeta.java core/src/luan/LuanPropertyMeta.java core/src/luan/modules/TableLuan.java lucene/src/luan/modules/lucene/FieldTable.java web/src/luan/modules/web/HttpServicer.java
diffstat 5 files changed, 40 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/LuanMeta.java	Wed Apr 29 19:06:18 2015 -0600
+++ b/core/src/luan/LuanMeta.java	Thu Apr 30 06:04:17 2015 -0600
@@ -10,12 +10,12 @@
 
 	public abstract Object __index(LuanState luan,LuanTable tbl,Object key) throws LuanException;
 
-	protected abstract Iterator keys();
+	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.iterator();
-			final Iterator<Object> iter2 = keys();
+			final Iterator<Object> iter2 = keys(tbl);
 			final Set<Object> set = new HashSet<Object>();
 
 			@Override public Object[] call(LuanState luan,Object[] args) throws LuanException {
--- a/core/src/luan/LuanPropertyMeta.java	Wed Apr 29 19:06:18 2015 -0600
+++ b/core/src/luan/LuanPropertyMeta.java	Thu Apr 30 06:04:17 2015 -0600
@@ -4,38 +4,21 @@
 import java.util.Iterator;
 
 
-public final class LuanPropertyMeta extends LuanMeta implements DeepCloneable<LuanPropertyMeta> {
-	private LuanTable getters;
-	private LuanTable setters;
+public final class LuanPropertyMeta extends LuanMeta {
+	public static final LuanPropertyMeta INSTANCE = new LuanPropertyMeta();
 
 	private LuanPropertyMeta() {}
 
-	public static LuanPropertyMeta newInstance() {
-		LuanPropertyMeta meta = new LuanPropertyMeta();
-		meta.getters = new LuanTable();
-		meta.setters = new LuanTable();
-		return meta;
-	}
-
-	public LuanTable getters() {
-		return getters;
+	public LuanTable getters(LuanTable tbl) {
+		return (LuanTable)tbl.getMetatable().get("get");
 	}
 
-	public LuanTable setters() {
-		return setters;
-	}
-
-	@Override public LuanPropertyMeta shallowClone() {
-		return new LuanPropertyMeta();
-	}
-
-	@Override public void deepenClone(LuanPropertyMeta clone,DeepCloner cloner) {
-		clone.getters = cloner.get(getters);
-		clone.setters = cloner.get(setters);
+	public LuanTable setters(LuanTable tbl) {
+		return (LuanTable)tbl.getMetatable().get("set");
 	}
 
 	@Override public Object __index(LuanState luan,LuanTable tbl,Object key) throws LuanException {
-		Object obj = getters.get(key);
+		Object obj = getters(tbl).get(key);
 		if( obj == null )
 			return null;
 		if( !(obj instanceof LuanFunction) )
@@ -44,9 +27,9 @@
 		return luan.call(fn);
 	}
 
-	@Override protected Iterator keys() {
+	@Override protected Iterator keys(final LuanTable tbl) {
 		return new Iterator() {
-			final Iterator<Map.Entry<Object,Object>> iter = getters.iterator();
+			final Iterator<Map.Entry<Object,Object>> iter = getters(tbl).iterator();
 
 			@Override public boolean hasNext() {
 				return iter.hasNext();
@@ -66,7 +49,7 @@
 	}
 
 	@Override public void __newindex(LuanState luan,LuanTable tbl,Object key,Object value) throws LuanException {
-		Object obj = setters.get(key);
+		Object obj = setters(tbl).get(key);
 		if( obj == null )
 			throw luan.exception("can't set property '"+key+"'");
 		if( !(obj instanceof LuanFunction) )
@@ -77,8 +60,8 @@
 
 	@Override public LuanTable newMetatable() {
 		LuanTable mt = super.newMetatable();
-		mt.put( "get", getters );
-		mt.put( "set", setters );
+		mt.put( "get", new LuanTable() );
+		mt.put( "set", new LuanTable() );
 		return mt;
 	}
 
--- a/core/src/luan/modules/TableLuan.java	Wed Apr 29 19:06:18 2015 -0600
+++ b/core/src/luan/modules/TableLuan.java	Thu Apr 30 06:04:17 2015 -0600
@@ -116,6 +116,6 @@
 	}
 
 	public static LuanTable new_property_table() {
-		return LuanPropertyMeta.newInstance().newTable();
+		return LuanPropertyMeta.INSTANCE.newTable();
 	}
 }
--- a/lucene/src/luan/modules/lucene/FieldTable.java	Wed Apr 29 19:06:18 2015 -0600
+++ b/lucene/src/luan/modules/lucene/FieldTable.java	Thu Apr 30 06:04:17 2015 -0600
@@ -53,7 +53,7 @@
 		return map.get(key);
 	}
 
-	@Override public final Iterator keys() {
+	@Override public final Iterator keys(LuanTable tbl) {
 		return map.keySet().iterator();
 	}
 /*
--- a/web/src/luan/modules/web/HttpServicer.java	Wed Apr 29 19:06:18 2015 -0600
+++ b/web/src/luan/modules/web/HttpServicer.java	Thu Apr 30 06:04:17 2015 -0600
@@ -119,8 +119,8 @@
 	}
 
 	private LuanTable requestTable() throws NoSuchMethodException {
-		LuanPropertyMeta meta = LuanPropertyMeta.newInstance();
-		LuanTable tbl = meta.newTable();
+		LuanTable tbl = LuanPropertyMeta.INSTANCE.newTable();
+		LuanTable getters = LuanPropertyMeta.INSTANCE.getters(tbl);
 		tbl.put("java",request);
 		LuanTable parameters = new NameMeta() {
 
@@ -128,7 +128,7 @@
 				return request.getParameter(name);
 			}
 
-			@Override protected Iterator keys() {
+			@Override protected Iterator keys(LuanTable tbl) {
 				return new EnumerationIterator(request.getParameterNames());
 			}
 /*
@@ -145,7 +145,7 @@
 				return request.getHeader(name);
 			}
 
-			@Override protected Iterator keys() {
+			@Override protected Iterator keys(LuanTable tbl) {
 				return new EnumerationIterator(request.getHeaderNames());
 			}
 /*
@@ -155,22 +155,22 @@
 */
 		}.newTable();
 		tbl.put( "headers", headers );
-		meta.getters().put( "method", new LuanJavaFunction(
+		getters.put( "method", new LuanJavaFunction(
 			HttpServletRequest.class.getMethod( "getMethod" ), request
 		) );
-		meta.getters().put( "path", new LuanJavaFunction(
+		getters.put( "path", new LuanJavaFunction(
 			HttpServletRequest.class.getMethod( "getRequestURI" ), request
 		) );
-		meta.getters().put( "server_name", new LuanJavaFunction(
+		getters.put( "server_name", new LuanJavaFunction(
 			HttpServletRequest.class.getMethod( "getServerName" ), request
 		) );
-		meta.getters().put( "url", new LuanJavaFunction(
+		getters.put( "url", new LuanJavaFunction(
 			HttpServicer.class.getMethod( "getURL" ), this
 		) );
-		meta.getters().put( "query_string", new LuanJavaFunction(
+		getters.put( "query_string", new LuanJavaFunction(
 			HttpServicer.class.getMethod( "getQueryString" ), this
 		) );
-		meta.getters().put( "remote_address", new LuanJavaFunction(
+		getters.put( "remote_address", new LuanJavaFunction(
 			HttpServletRequest.class.getMethod( "getRemoteAddr" ), request
 		) );
 		LuanTable cookies = new LuanMeta() {
@@ -182,7 +182,7 @@
 				return getCookieValue(request,name);
 			}
 
-			@Override protected Iterator<Object> keys() {
+			@Override protected Iterator<Object> keys(LuanTable tbl) {
 				return new Iterator<Object>() {
 					final Cookie[] cookies = request.getCookies();
 					int i = 0;
@@ -222,11 +222,10 @@
 					if( filename == null ) {
 						value = new String(part.getBytes());
 					} else {
-						LuanPropertyMeta partMeta = LuanPropertyMeta.newInstance();
-						LuanTable partTbl = partMeta.newTable();
+						LuanTable partTbl = LuanPropertyMeta.INSTANCE.newTable();
 						partTbl.put("filename",filename);
 						partTbl.put("content_type",part.getContentType());
-						partMeta.getters().put( "content", new LuanFunction() {
+						LuanPropertyMeta.INSTANCE.getters(partTbl).put( "content", new LuanFunction() {
 							@Override public Object call(LuanState luan,Object[] args) throws LuanException {
 								try {
 									InputStream in = part.getInputStream();
@@ -271,8 +270,9 @@
 	}
 
 	private LuanTable responseTable() throws NoSuchMethodException {
-		LuanPropertyMeta meta = LuanPropertyMeta.newInstance();
-		LuanTable tbl = meta.newTable();
+		LuanTable tbl = LuanPropertyMeta.INSTANCE.newTable();
+		LuanTable getters = LuanPropertyMeta.INSTANCE.getters(tbl);
+		LuanTable setters = LuanPropertyMeta.INSTANCE.setters(tbl);
 		tbl.put("java",response);
 		add( tbl, "send_redirect", String.class );
 		add( tbl, "send_error", Integer.TYPE, String.class );
@@ -282,7 +282,7 @@
 				return response.getHeader(name);
 			}
 
-			@Override protected Iterator keys() {
+			@Override protected Iterator keys(LuanTable tbl) {
 				return response.getHeaderNames().iterator();
 			}
 
@@ -312,25 +312,25 @@
 */
 		}.newTable();
 		tbl.put( "headers", headers );
-		meta.getters().put( "content_type", new LuanJavaFunction(
+		getters.put( "content_type", new LuanJavaFunction(
 			HttpServletResponse.class.getMethod( "getContentType" ), response
 		) );
-		meta.setters().put( "content_type", new LuanJavaFunction(
+		setters.put( "content_type", new LuanJavaFunction(
 			HttpServletResponse.class.getMethod( "setContentType", String.class ), response
 		) );
-		meta.getters().put( "character_encoding", new LuanJavaFunction(
+		getters.put( "character_encoding", new LuanJavaFunction(
 			HttpServletResponse.class.getMethod( "getCharacterEncoding" ), response
 		) );
-		meta.setters().put( "character_encoding", new LuanJavaFunction(
+		setters.put( "character_encoding", new LuanJavaFunction(
 			HttpServletResponse.class.getMethod( "setCharacterEncoding", String.class ), response
 		) );
 		add( tbl, "text_writer" );
 		add( tbl, "set_cookie", String.class, String.class, Boolean.TYPE, String.class );
 		add( tbl, "remove_cookie", String.class, String.class );
-		meta.getters().put( "status", new LuanJavaFunction(
+		getters.put( "status", new LuanJavaFunction(
 			HttpServletResponse.class.getMethod( "getStatus" ), response
 		) );
-		meta.setters().put( "status", new LuanJavaFunction(
+		setters.put( "status", new LuanJavaFunction(
 			HttpServletResponse.class.getMethod( "setStatus", Integer.TYPE ), response
 		) );
 		return tbl;
@@ -344,7 +344,7 @@
 				return request.getSession().getAttribute(name);
 			}
 
-			@Override protected Iterator keys() {
+			@Override protected Iterator keys(LuanTable tbl) {
 				return new EnumerationIterator(request.getSession().getAttributeNames());
 			}