comparison src/luan/modules/BasicLuan.java @ 1578:c922446f53aa

immutable threading
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 08 Feb 2021 14:16:19 -0700
parents 8fbcc4747091
children fa066aaa068c
comparison
equal deleted inserted replaced
1577:60e5c324adf9 1578:c922446f53aa
12 import goodjava.json.JsonToString; 12 import goodjava.json.JsonToString;
13 import luan.Luan; 13 import luan.Luan;
14 import luan.LuanTable; 14 import luan.LuanTable;
15 import luan.LuanFunction; 15 import luan.LuanFunction;
16 import luan.LuanException; 16 import luan.LuanException;
17 import luan.LuanCloner; 17 import luan.LuanMutable;
18 import luan.LuanCloneable;
19 import luan.LuanImmutabler;
20 import luan.modules.parsers.LuanToString; 18 import luan.modules.parsers.LuanToString;
21 19
22 20
23 public final class BasicLuan { 21 public final class BasicLuan {
24 22
49 public static LuanFunction pairs(Luan luan,final LuanTable t) throws LuanException { 47 public static LuanFunction pairs(Luan luan,final LuanTable t) throws LuanException {
50 Utils.checkNotNull(t); 48 Utils.checkNotNull(t);
51 return t.pairs(luan); 49 return t.pairs(luan);
52 } 50 }
53 51
54 private static class Ipairs extends LuanFunction implements LuanCloneable, Cloneable { 52 private static final class Ipairs extends LuanFunction implements LuanMutable {
55 List<Object> list; 53 List<Object> list;
56 int i = 0; 54 int i = 0;
57 final int size; 55 final int size;
58 56
59 Ipairs(LuanTable t) { 57 Ipairs(LuanTable t) {
66 return LuanFunction.NOTHING; 64 return LuanFunction.NOTHING;
67 Object val = list.get(i++); 65 Object val = list.get(i++);
68 return new Object[]{i,val}; 66 return new Object[]{i,val};
69 } 67 }
70 68
71 @Override public final Ipairs shallowClone() { 69 @Override public boolean isImmutable() {
72 try { 70 return false;
73 return (Ipairs)clone(); 71 }
74 } catch(CloneNotSupportedException e) { 72
75 throw new RuntimeException(e); 73 @Override public void makeImmutable() {
76 } 74 throw new RuntimeException("ipairs cannot be made immutable");
77 }
78
79 @Override public final void deepenClone(LuanCloneable dc,LuanCloner cloner) {
80 Ipairs clone = (Ipairs)dc;
81 clone.list = (List)cloner.clone(list);
82 }
83
84 @Override public void makeImmutable(LuanImmutabler immutabler) throws LuanException {
85 throw new LuanException("ipairs cannot be made immutable");
86 } 75 }
87 } 76 }
88 77
89 public static LuanFunction ipairs(LuanTable t) throws LuanException { 78 public static LuanFunction ipairs(LuanTable t) throws LuanException {
90 Utils.checkNotNull(t); 79 Utils.checkNotNull(t);
98 return null; 87 return null;
99 Object obj = metatable.rawGet("__metatable"); 88 Object obj = metatable.rawGet("__metatable");
100 return obj!=null ? obj : metatable; 89 return obj!=null ? obj : metatable;
101 } 90 }
102 91
103 public static void set_metatable(LuanTable table,LuanTable metatable) throws LuanException { 92 public static void set_metatable(Luan luan,LuanTable table,LuanTable metatable) throws LuanException {
104 Utils.checkNotNull(table); 93 Utils.checkNotNull(table);
105 if( table.getHandler("__metatable") != null ) 94 if( table.getHandler(luan,"__metatable") != null )
106 throw new LuanException("cannot change a protected metatable"); 95 throw new LuanException("cannot change a protected metatable");
107 table.setMetatable(metatable); 96 table.setMetatable(metatable);
108 } 97 }
109 98
110 public static boolean raw_equal(Object v1,Object v2) { 99 public static boolean raw_equal(Object v1,Object v2) {
173 return rtn; 162 return rtn;
174 } 163 }
175 }; 164 };
176 } 165 }
177 166
178 private static class Values extends LuanFunction implements LuanCloneable, Cloneable { 167 private static final class Values extends LuanFunction implements LuanMutable {
179 Object[] args; 168 Object[] args;
180 int i = 0; 169 int i = 0;
181 170
182 Values(Object[] args) { 171 Values(Object[] args) {
183 this.args = args; 172 this.args = args;
188 return LuanFunction.NOTHING; 177 return LuanFunction.NOTHING;
189 Object val = args[i++]; 178 Object val = args[i++];
190 return new Object[]{i,val}; 179 return new Object[]{i,val};
191 } 180 }
192 181
193 @Override public final Values shallowClone() { 182 @Override public boolean isImmutable() {
194 try { 183 return false;
195 return (Values)clone(); 184 }
196 } catch(CloneNotSupportedException e) { 185
197 throw new RuntimeException(e); 186 @Override public void makeImmutable() {
198 } 187 throw new RuntimeException("values cannot be made immutable");
199 }
200
201 @Override public final void deepenClone(LuanCloneable dc,LuanCloner cloner) {
202 Values clone = (Values)dc;
203 clone.args = (Object[])cloner.clone(args);
204 }
205
206 @Override public void makeImmutable(LuanImmutabler immutabler) throws LuanException {
207 throw new LuanException("values cannot be made immutable");
208 } 188 }
209 } 189 }
210 190
211 public static LuanFunction values(final Object... args) throws LuanException { 191 public static LuanFunction values(final Object... args) throws LuanException {
212 return new Values(args); 192 return new Values(args);
246 Utils.checkEmpty(options); 226 Utils.checkEmpty(options);
247 } 227 }
248 return jts.toString(Luan.toJava(obj)); 228 return jts.toString(Luan.toJava(obj));
249 } 229 }
250 230
231 public static Object get_local_cloned(Luan luan,Object obj,Object key) {
232 return luan.getLocalCloned(obj,key);
233 }
234
235 public static void set_local_cloned(Luan luan,Object obj,Object key,Object value) {
236 luan.setLocalCloned(obj,key,value);
237 }
238
239 public static Object get_local_only(Luan luan,Object obj,Object key) {
240 return luan.getLocalOnly(obj,key);
241 }
242
243 public static void set_local_only(Luan luan,Object obj,Object key,Object value) {
244 luan.setLocalOnly(obj,key,value);
245 }
246
247 public static boolean is_immutable(LuanMutable m) {
248 return m.isImmutable();
249 }
250
251 private void BasicLuan() {} // never 251 private void BasicLuan() {} // never
252 } 252 }