Mercurial Hosting > luan
comparison src/luan/LuanTable.java @ 1561:e1a13e707bf3
start immutable
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 05 Nov 2020 20:24:09 -0700 |
parents | d4407e8de707 |
children | b89212fd04b5 |
comparison
equal
deleted
inserted
replaced
1560:33a53c43e2f7 | 1561:e1a13e707bf3 |
---|---|
19 private List list = null; | 19 private List list = null; |
20 private LuanTable metatable = null; | 20 private LuanTable metatable = null; |
21 public LuanClosure closure; | 21 public LuanClosure closure; |
22 private LuanCloner cloner; | 22 private LuanCloner cloner; |
23 private String security = null; | 23 private String security = null; |
24 private boolean immutable = false; | |
24 | 25 |
25 public LuanTable(Luan luan) { | 26 public LuanTable(Luan luan) { |
26 this.luan = luan; | 27 this.luan = luan; |
27 } | 28 } |
28 | 29 |
29 public LuanTable(Luan luan,List list){ | 30 public LuanTable(Luan luan,List list) { |
30 this.luan = luan; | 31 this.luan = luan; |
31 list(); | 32 list(); |
32 int n = list.size(); | 33 int n = list.size(); |
33 for( int i=0; i<n; i++ ) { | 34 for( int i=0; i<n; i++ ) { |
34 Object val = list.get(i); | 35 Object val = list.get(i); |
46 if( key != null && value != null ) | 47 if( key != null && value != null ) |
47 rawPut2(key,value); | 48 rawPut2(key,value); |
48 } | 49 } |
49 } | 50 } |
50 | 51 |
51 public LuanTable(Luan luan,Set set){ | 52 public LuanTable(Luan luan,Set set) { |
52 this.luan = luan; | 53 this.luan = luan; |
53 for( Object el : set ) { | 54 for( Object el : set ) { |
54 if( el != null ) | 55 if( el != null ) |
55 rawPut2(el,Boolean.TRUE); | 56 rawPut2(el,Boolean.TRUE); |
56 } | 57 } |
64 this.list = new ArrayList<Object>(tbl.list); | 65 this.list = new ArrayList<Object>(tbl.list); |
65 this.metatable = tbl.metatable; | 66 this.metatable = tbl.metatable; |
66 } | 67 } |
67 | 68 |
68 @Override public LuanTable shallowClone() { | 69 @Override public LuanTable shallowClone() { |
70 if(immutable) throw new RuntimeException(); | |
69 return new LuanTable(luan); | 71 return new LuanTable(luan); |
70 } | 72 } |
71 | 73 |
72 @Override public void deepenClone(LuanCloneable dc,LuanCloner cloner) { | 74 @Override public void deepenClone(LuanCloneable dc,LuanCloner cloner) { |
73 check(); | 75 check(); |
99 return luan; | 101 return luan; |
100 } | 102 } |
101 | 103 |
102 private void completeClone(LuanTable clone,LuanCloner cloner) { | 104 private void completeClone(LuanTable clone,LuanCloner cloner) { |
103 clone.luan = (Luan)cloner.clone(luan); | 105 clone.luan = (Luan)cloner.clone(luan); |
104 if( map != null ) { | 106 clone.map = cloner.clone(map); |
105 Map newMap = newMap(); | 107 clone.list = (List)cloner.clone(list); |
106 for( Object stupid : map.entrySet() ) { | |
107 Map.Entry entry = (Map.Entry)stupid; | |
108 newMap.put( cloner.get(entry.getKey()), cloner.get(entry.getValue()) ); | |
109 } | |
110 clone.map = newMap; | |
111 } | |
112 if( list != null ) { | |
113 List newList = new ArrayList<Object>(); | |
114 for( Object obj : list ) { | |
115 newList.add(cloner.get(obj)); | |
116 } | |
117 clone.list = newList; | |
118 } | |
119 clone.metatable = (LuanTable)cloner.clone(metatable); | 108 clone.metatable = (LuanTable)cloner.clone(metatable); |
120 clone.closure = (LuanClosure)cloner.clone(closure); | 109 clone.closure = (LuanClosure)cloner.clone(closure); |
110 } | |
111 | |
112 @Override public void makeImmutable(LuanImmutabler immutabler) throws LuanException { | |
113 check(); | |
114 immutabler.makeImmutable(luan); | |
115 immutabler.makeImmutable(map); | |
116 immutabler.makeImmutable(list); | |
117 immutabler.makeImmutable(metatable); | |
118 immutabler.makeImmutable(closure); | |
119 immutable = true; | |
121 } | 120 } |
122 | 121 |
123 public boolean isList() { | 122 public boolean isList() { |
124 return map==null || map.isEmpty(); | 123 return map==null || map.isEmpty(); |
125 } | 124 } |
201 } | 200 } |
202 throw new LuanException("invalid type "+Luan.type(h)+" for metamethod __new_index"); | 201 throw new LuanException("invalid type "+Luan.type(h)+" for metamethod __new_index"); |
203 } | 202 } |
204 | 203 |
205 public Object rawPut(Object key,Object val) throws LuanException { | 204 public Object rawPut(Object key,Object val) throws LuanException { |
205 if( immutable ) | |
206 throw new LuanException("table is immutable"); | |
206 if( security != null ) | 207 if( security != null ) |
207 Luan.checkSecurity(luan,"table",security,"put",key,val); | 208 Luan.checkSecurity(luan,"table",security,"put",key,val); |
208 return rawPut2(key,val); | 209 return rawPut2(key,val); |
209 } | 210 } |
210 | 211 |
477 public Object getHandler(String op) throws LuanException { | 478 public Object getHandler(String op) throws LuanException { |
478 check(); | 479 check(); |
479 return metatable==null ? null : metatable.get(op); | 480 return metatable==null ? null : metatable.get(op); |
480 } | 481 } |
481 | 482 |
482 private Map<Object,Object> newMap() { | 483 private static Map<Object,Object> newMap() { |
483 return new LinkedHashMap<Object,Object>(); | 484 return new LinkedHashMap<Object,Object>(); |
484 } | 485 } |
485 | 486 |
486 public boolean isSet() throws LuanException { | 487 public boolean isSet() throws LuanException { |
487 for( Map.Entry<Object,Object> entry : iterable() ) { | 488 for( Map.Entry<Object,Object> entry : iterable() ) { |
533 if( list != null ) | 534 if( list != null ) |
534 n += list.size(); | 535 n += list.size(); |
535 return n; | 536 return n; |
536 } | 537 } |
537 | 538 |
538 public Object remove(Object key) { | 539 public Object remove(Object key) throws LuanException { |
540 if( immutable ) | |
541 throw new LuanException("table is immutable"); | |
539 Object old = rawGet(key); | 542 Object old = rawGet(key); |
540 rawPut2(key,null); | 543 rawPut2(key,null); |
541 return old; | 544 return old; |
542 } | 545 } |
543 | 546 |