Mercurial Hosting > luan
comparison src/luan/modules/BasicLuan.java @ 1520:d9a5405a3102
try statement
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 21 Jun 2020 18:14:13 -0600 |
parents | 56fb5cd8228d |
children | 0dc3be25ad20 |
comparison
equal
deleted
inserted
replaced
1519:3ebf9781707c | 1520:d9a5405a3102 |
---|---|
191 | 191 |
192 private LuanFunction fn(Object obj) { | 192 private LuanFunction fn(Object obj) { |
193 return obj instanceof LuanFunction ? (LuanFunction)obj : null; | 193 return obj instanceof LuanFunction ? (LuanFunction)obj : null; |
194 } | 194 } |
195 | 195 |
196 public static Object try_(LuanTable blocks,Object... args) throws LuanException { | |
197 Utils.checkNotNull(blocks); | |
198 Object obj = blocks.get(1); | |
199 if( obj == null ) | |
200 throw new LuanException("missing 'try' value"); | |
201 if( !(obj instanceof LuanFunction) ) | |
202 throw new LuanException("bad 'try' value (function expected, got "+Luan.type(obj)+")"); | |
203 LuanFunction tryFn = (LuanFunction)obj; | |
204 LuanFunction catchFn = null; | |
205 obj = blocks.get("catch"); | |
206 if( obj != null ) { | |
207 if( !(obj instanceof LuanFunction) ) | |
208 throw new LuanException("bad 'catch' value (function expected, got "+Luan.type(obj)+")"); | |
209 catchFn = (LuanFunction)obj; | |
210 } | |
211 LuanFunction finallyFn = null; | |
212 obj = blocks.get("finally"); | |
213 if( obj != null ) { | |
214 if( !(obj instanceof LuanFunction) ) | |
215 throw new LuanException("bad 'finally' value (function expected, got "+Luan.type(obj)+")"); | |
216 finallyFn = (LuanFunction)obj; | |
217 } | |
218 try { | |
219 return tryFn.call(args); | |
220 } catch(LuanException e) { | |
221 if( catchFn == null ) | |
222 throw e; | |
223 return catchFn.call(e.table(blocks.luan())); | |
224 } finally { | |
225 if( finallyFn != null ) | |
226 finallyFn.call(); | |
227 } | |
228 } | |
229 | |
230 public static Object[] pcall(LuanFunction f,Object... args) { | |
231 try { | |
232 Object[] r = Luan.array(f.call(args)); | |
233 Object[] rtn = new Object[r.length+1]; | |
234 rtn[0] = true; | |
235 for( int i=0; i<r.length; i++ ) { | |
236 rtn[i+1] = r[i]; | |
237 } | |
238 return rtn; | |
239 } catch(LuanException e) { | |
240 return new Object[]{false,e.table(f.luan())}; | |
241 } | |
242 } | |
243 | |
244 public static String number_type(Number v) throws LuanException { | 196 public static String number_type(Number v) throws LuanException { |
245 Utils.checkNotNull(v); | 197 Utils.checkNotNull(v); |
246 return v.getClass().getSimpleName().toLowerCase(); | 198 return v.getClass().getSimpleName().toLowerCase(); |
247 } | 199 } |
248 | 200 |