comparison src/luan/LuanTable.java @ 1330:f41919741100

fix security
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 11 Feb 2019 01:38:55 -0700
parents 1b46c8e6c647
children 25746915a241
comparison
equal deleted inserted replaced
1329:5a39b006acd1 1330:f41919741100
16 public final class LuanTable implements LuanCloneable { 16 public final class LuanTable implements LuanCloneable {
17 private LuanState luan; 17 private LuanState luan;
18 private Map map = null; 18 private Map map = null;
19 private List list = null; 19 private List list = null;
20 private LuanTable metatable = null; 20 private LuanTable metatable = null;
21 public LuanJavaOk javaOk; 21 public LuanClosure closure;
22 private LuanCloner cloner; 22 private LuanCloner cloner;
23 private String security = null;
23 24
24 public LuanTable(LuanState luan) { 25 public LuanTable(LuanState luan) {
25 this.luan = luan; 26 this.luan = luan;
26 } 27 }
27 28
28 public LuanTable(LuanState luan,List list) { 29 public LuanTable(LuanState luan,List list){
29 int n = list.size(); 30 int n = list.size();
30 for( int i=0; i<n; i++ ) { 31 for( int i=0; i<n; i++ ) {
31 Object val = list.get(i); 32 Object val = list.get(i);
32 if( val != null ) 33 if( val != null )
33 rawPut(i+1,val); 34 rawPut2(i+1,val);
34 } 35 }
35 } 36 }
36 37
37 public LuanTable(LuanState luan,Map map) { 38 public LuanTable(LuanState luan,Map map) {
38 this.luan = luan; 39 this.luan = luan;
39 for( Object stupid : map.entrySet() ) { 40 for( Object stupid : map.entrySet() ) {
40 Map.Entry entry = (Map.Entry)stupid; 41 Map.Entry entry = (Map.Entry)stupid;
41 Object key = entry.getKey(); 42 Object key = entry.getKey();
42 Object value = entry.getValue(); 43 Object value = entry.getValue();
43 if( key != null && value != null ) 44 if( key != null && value != null )
44 rawPut(key,value); 45 rawPut2(key,value);
45 } 46 }
46 } 47 }
47 48
48 public LuanTable(LuanState luan,Set set) { 49 public LuanTable(LuanState luan,Set set){
49 this.luan = luan; 50 this.luan = luan;
50 for( Object el : set ) { 51 for( Object el : set ) {
51 if( el != null ) 52 if( el != null )
52 rawPut(el,Boolean.TRUE); 53 rawPut2(el,Boolean.TRUE);
53 } 54 }
54 } 55 }
55 56
56 public LuanTable(LuanTable tbl) { 57 public LuanTable(LuanTable tbl) {
57 this.luan = tbl.luan; 58 this.luan = tbl.luan;
67 } 68 }
68 69
69 @Override public void deepenClone(LuanCloneable dc,LuanCloner cloner) { 70 @Override public void deepenClone(LuanCloneable dc,LuanCloner cloner) {
70 check(); 71 check();
71 LuanTable clone = (LuanTable)dc; 72 LuanTable clone = (LuanTable)dc;
73 clone.security = security;
72 switch( cloner.type ) { 74 switch( cloner.type ) {
73 case COMPLETE: 75 case COMPLETE:
74 deepenClone(clone,cloner); 76 deepenClone(clone,cloner);
75 return; 77 return;
76 case INCREMENTAL: 78 case INCREMENTAL:
77 clone.cloner = cloner; 79 clone.cloner = cloner;
78 clone.map = map; 80 clone.map = map;
79 clone.list = list; 81 clone.list = list;
80 clone.metatable = metatable; 82 clone.metatable = metatable;
81 clone.javaOk = javaOk; 83 clone.closure = closure;
82 return; 84 return;
83 } 85 }
84 } 86 }
85 87
86 private void check() { 88 private void check() {
106 newList.add(cloner.get(obj)); 108 newList.add(cloner.get(obj));
107 } 109 }
108 clone.list = newList; 110 clone.list = newList;
109 } 111 }
110 clone.metatable = (LuanTable)cloner.clone(metatable); 112 clone.metatable = (LuanTable)cloner.clone(metatable);
111 clone.javaOk = (LuanJavaOk)cloner.clone(javaOk); 113 clone.closure = (LuanClosure)cloner.clone(closure);
112 } 114 }
113 115
114 public boolean isList() { 116 public boolean isList() {
115 return map==null || map.isEmpty(); 117 return map==null || map.isEmpty();
116 } 118 }
187 return; 189 return;
188 } 190 }
189 throw new LuanException("invalid type "+Luan.type(h)+" for metamethod __new_index"); 191 throw new LuanException("invalid type "+Luan.type(h)+" for metamethod __new_index");
190 } 192 }
191 193
192 public void rawPut(Object key,Object val) { 194 public void rawPut(Object key,Object val) throws LuanException {
195 if( security != null )
196 Luan.checkSecurity(luan,"table",security,"put",key,val);
197 rawPut2(key,val);
198 }
199
200 private void rawPut2(Object key,Object val) {
193 check(); 201 check();
194 Integer iT = Luan.asInteger(key); 202 Integer iT = Luan.asInteger(key);
195 if( iT != null ) { 203 if( iT != null ) {
196 int i = iT - 1; 204 int i = iT - 1;
197 if( list != null || i == 0 ) { 205 if( list != null || i == 0 ) {
435 public LuanTable getMetatable() { 443 public LuanTable getMetatable() {
436 check(); 444 check();
437 return metatable; 445 return metatable;
438 } 446 }
439 447
440 public void setMetatable(LuanTable metatable) { 448 public void setMetatable(LuanTable metatable) throws LuanException {
449 if( security != null )
450 Luan.checkSecurity(luan,"table",security,"set_metatable",metatable);
441 check(); 451 check();
442 this.metatable = metatable; 452 this.metatable = metatable;
443 } 453 }
444 454
445 public Object getHandler(String op) throws LuanException { 455 public Object getHandler(String op) throws LuanException {
514 524
515 public Object call(String fnName,Object... args) throws LuanException { 525 public Object call(String fnName,Object... args) throws LuanException {
516 LuanFunction fn = (LuanFunction)get(fnName); 526 LuanFunction fn = (LuanFunction)get(fnName);
517 return fn.call(luan,args); 527 return fn.call(luan,args);
518 } 528 }
529
530 public static void setSecurity(LuanTable tbl,String security) {
531 tbl.security = security;
532 }
519 } 533 }