changeset 1542:d4407e8de707

disallow duplicates in table constructor
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 20 Sep 2020 15:53:58 -0600
parents dc23c96f5021
children 1db694d98003
files src/luan/LuanTable.java src/luan/impl/LuanImpl.java src/luan/impl/LuanParser.java
diffstat 3 files changed, 16 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
diff -r dc23c96f5021 -r d4407e8de707 src/luan/LuanTable.java
--- a/src/luan/LuanTable.java	Sun Sep 13 15:50:52 2020 -0600
+++ b/src/luan/LuanTable.java	Sun Sep 20 15:53:58 2020 -0600
@@ -202,13 +202,13 @@
 		throw new LuanException("invalid type "+Luan.type(h)+" for metamethod __new_index");
 	}
 
-	public void rawPut(Object key,Object val) throws LuanException {
+	public Object rawPut(Object key,Object val) throws LuanException {
 		if( security != null )
 			Luan.checkSecurity(luan,"table",security,"put",key,val);
-		rawPut2(key,val);
+		return rawPut2(key,val);
 	}
 
-	private void rawPut2(Object key,Object val) {
+	private Object rawPut2(Object key,Object val) {
 		check();
 		Integer iT = Luan.asInteger(key);
 		if( iT != null ) {
@@ -219,13 +219,13 @@
 						list.add(val);
 						mapToList();
 					}
-					return;
+					return null;
 				} else if( i>=0 && i<list.size() ) {
-					list.set(i,val);
+					Object rtn = list.set(i,val);
 					if( val == null ) {
 						listToMap(i);
 					}
-					return;
+					return rtn;
 				}
 			}
 		}
@@ -235,11 +235,13 @@
 		}
 		if( val == null ) {
 			if( map!=null )
-				map.remove(key);
+				return map.remove(key);
+			else
+				return null;
 		} else {
 			if( map==null )
 				map = newMap();
-			map.put(key,val);
+			return map.put(key,val);
 		}
 	}
 
diff -r dc23c96f5021 -r d4407e8de707 src/luan/impl/LuanImpl.java
--- a/src/luan/impl/LuanImpl.java	Sun Sep 13 15:50:52 2020 -0600
+++ b/src/luan/impl/LuanImpl.java	Sun Sep 20 15:53:58 2020 -0600
@@ -236,11 +236,14 @@
 				TableField tblFld = (TableField)fld;
 				Object key = tblFld.key;
 				Object value = tblFld.value;
-				if( key != null && value != null )
-					table.rawPut(key,value);
+				if( key != null && value != null ) {
+					if( table.rawPut(key,value) != null )
+						throw new LuanException("duplicate key in table constructor: "+key);
+				}
 			} else {
 				i++;
-				table.rawPut(i,fld);
+				if( table.rawPut(i,fld) != null )
+					throw new LuanException("duplicate key in table constructor: "+i);
 			}
 		}
 		return table;
diff -r dc23c96f5021 -r d4407e8de707 src/luan/impl/LuanParser.java
--- a/src/luan/impl/LuanParser.java	Sun Sep 13 15:50:52 2020 -0600
+++ b/src/luan/impl/LuanParser.java	Sun Sep 20 15:53:58 2020 -0600
@@ -1157,14 +1157,6 @@
 		tblExp.add( "LuanImpl.table(luan," );
 		Expr lastExp = tblExp;
 		List<Expr> builder = new ArrayList<Expr>();
-/*
-		Spaces();
-		Field(builder);
-		while( FieldSep() ) {
-			Spaces();
-			Field(builder);
-		}
-*/
 		do {
 			Spaces();  lastExp.addNewLines();
 			Expr exp = Field();