comparison src/luan/LuanTable.java @ 1542:d4407e8de707

disallow duplicates in table constructor
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 20 Sep 2020 15:53:58 -0600
parents 2e8a5df45d56
children e1a13e707bf3
comparison
equal deleted inserted replaced
1541:dc23c96f5021 1542:d4407e8de707
200 return; 200 return;
201 } 201 }
202 throw new LuanException("invalid type "+Luan.type(h)+" for metamethod __new_index"); 202 throw new LuanException("invalid type "+Luan.type(h)+" for metamethod __new_index");
203 } 203 }
204 204
205 public void rawPut(Object key,Object val) throws LuanException { 205 public Object rawPut(Object key,Object val) throws LuanException {
206 if( security != null ) 206 if( security != null )
207 Luan.checkSecurity(luan,"table",security,"put",key,val); 207 Luan.checkSecurity(luan,"table",security,"put",key,val);
208 rawPut2(key,val); 208 return rawPut2(key,val);
209 } 209 }
210 210
211 private void rawPut2(Object key,Object val) { 211 private Object rawPut2(Object key,Object val) {
212 check(); 212 check();
213 Integer iT = Luan.asInteger(key); 213 Integer iT = Luan.asInteger(key);
214 if( iT != null ) { 214 if( iT != null ) {
215 int i = iT - 1; 215 int i = iT - 1;
216 if( list != null || i == 0 ) { 216 if( list != null || i == 0 ) {
217 if( i == list().size() ) { 217 if( i == list().size() ) {
218 if( val != null ) { 218 if( val != null ) {
219 list.add(val); 219 list.add(val);
220 mapToList(); 220 mapToList();
221 } 221 }
222 return; 222 return null;
223 } else if( i>=0 && i<list.size() ) { 223 } else if( i>=0 && i<list.size() ) {
224 list.set(i,val); 224 Object rtn = list.set(i,val);
225 if( val == null ) { 225 if( val == null ) {
226 listToMap(i); 226 listToMap(i);
227 } 227 }
228 return; 228 return rtn;
229 } 229 }
230 } 230 }
231 } 231 }
232 if( key instanceof Number && !(key instanceof Double) ) { 232 if( key instanceof Number && !(key instanceof Double) ) {
233 Number n = (Number)key; 233 Number n = (Number)key;
234 key = Double.valueOf(n.doubleValue()); 234 key = Double.valueOf(n.doubleValue());
235 } 235 }
236 if( val == null ) { 236 if( val == null ) {
237 if( map!=null ) 237 if( map!=null )
238 map.remove(key); 238 return map.remove(key);
239 else
240 return null;
239 } else { 241 } else {
240 if( map==null ) 242 if( map==null )
241 map = newMap(); 243 map = newMap();
242 map.put(key,val); 244 return map.put(key,val);
243 } 245 }
244 } 246 }
245 247
246 private void mapToList() { 248 private void mapToList() {
247 if( map != null ) { 249 if( map != null ) {