Mercurial Hosting > luan
comparison core/src/luan/modules/BasicLuan.java @ 326:db37d6aee4db
remove try-catch statement;
add Luan.try() and Luan.pcall();
git-svn-id: https://luan-java.googlecode.com/svn/trunk@327 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 19 Mar 2015 00:01:57 +0000 |
parents | a74559240b4f |
children | 62b457c50594 |
comparison
equal
deleted
inserted
replaced
325:78a6a71afbfd | 326:db37d6aee4db |
---|---|
13 import luan.LuanFunction; | 13 import luan.LuanFunction; |
14 import luan.LuanJavaFunction; | 14 import luan.LuanJavaFunction; |
15 import luan.LuanException; | 15 import luan.LuanException; |
16 import luan.LuanSource; | 16 import luan.LuanSource; |
17 import luan.LuanElement; | 17 import luan.LuanElement; |
18 import luan.LuanMethod; | |
18 import luan.impl.LuanCompiler; | 19 import luan.impl.LuanCompiler; |
19 | 20 |
20 | 21 |
21 public final class BasicLuan { | 22 public final class BasicLuan { |
22 | 23 |
198 return new Object[]{i,args[i-1]}; | 199 return new Object[]{i,args[i-1]}; |
199 } | 200 } |
200 }; | 201 }; |
201 } | 202 } |
202 | 203 |
204 private LuanFunction fn(Object obj) { | |
205 return obj instanceof LuanFunction ? (LuanFunction)obj : null; | |
206 } | |
207 | |
208 public static void try_(LuanState luan,LuanTable blocks) throws LuanException { | |
209 Utils.checkNotNull(luan,blocks); | |
210 Object obj = blocks.get(1); | |
211 if( obj == null ) | |
212 throw luan.exception("missing 'try' value"); | |
213 if( !(obj instanceof LuanFunction) ) | |
214 throw luan.exception("bad 'try' value (function expected, got "+Luan.type(obj)+")"); | |
215 LuanFunction tryFn = (LuanFunction)obj; | |
216 LuanFunction catchFn = null; | |
217 obj = blocks.get("catch"); | |
218 if( obj != null ) { | |
219 if( !(obj instanceof LuanFunction) ) | |
220 throw luan.exception("bad 'catch' value (function expected, got "+Luan.type(obj)+")"); | |
221 catchFn = (LuanFunction)obj; | |
222 } | |
223 LuanFunction finallyFn = null; | |
224 obj = blocks.get("finally"); | |
225 if( obj != null ) { | |
226 if( !(obj instanceof LuanFunction) ) | |
227 throw luan.exception("bad 'finally' value (function expected, got "+Luan.type(obj)+")"); | |
228 finallyFn = (LuanFunction)obj; | |
229 } | |
230 try { | |
231 luan.call(tryFn); | |
232 } catch(LuanException e) { | |
233 if( catchFn == null ) | |
234 throw e; | |
235 luan.call(catchFn,new Object[]{e}); | |
236 } finally { | |
237 if( finallyFn != null ) | |
238 luan.call(finallyFn); | |
239 } | |
240 } | |
241 | |
242 @LuanMethod public static Object[] pcall(LuanState luan,LuanFunction f,Object... args) { | |
243 try { | |
244 Object[] r = Luan.array(luan.call(f,args)); | |
245 Object[] rtn = new Object[r.length+1]; | |
246 rtn[0] = true; | |
247 for( int i=0; i<r.length; i++ ) { | |
248 rtn[i+1] = r[i]; | |
249 } | |
250 return rtn; | |
251 } catch(LuanException e) { | |
252 return new Object[]{false,e}; | |
253 } | |
254 } | |
255 | |
203 } | 256 } |