Mercurial Hosting > luan
diff 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 |
line wrap: on
line diff
--- a/core/src/luan/modules/BasicLuan.java Tue Mar 03 06:00:59 2015 +0000 +++ b/core/src/luan/modules/BasicLuan.java Thu Mar 19 00:01:57 2015 +0000 @@ -15,6 +15,7 @@ import luan.LuanException; import luan.LuanSource; import luan.LuanElement; +import luan.LuanMethod; import luan.impl.LuanCompiler; @@ -200,4 +201,56 @@ }; } + private LuanFunction fn(Object obj) { + return obj instanceof LuanFunction ? (LuanFunction)obj : null; + } + + public static void try_(LuanState luan,LuanTable blocks) throws LuanException { + Utils.checkNotNull(luan,blocks); + Object obj = blocks.get(1); + if( obj == null ) + throw luan.exception("missing 'try' value"); + if( !(obj instanceof LuanFunction) ) + throw luan.exception("bad 'try' value (function expected, got "+Luan.type(obj)+")"); + LuanFunction tryFn = (LuanFunction)obj; + LuanFunction catchFn = null; + obj = blocks.get("catch"); + if( obj != null ) { + if( !(obj instanceof LuanFunction) ) + throw luan.exception("bad 'catch' value (function expected, got "+Luan.type(obj)+")"); + catchFn = (LuanFunction)obj; + } + LuanFunction finallyFn = null; + obj = blocks.get("finally"); + if( obj != null ) { + if( !(obj instanceof LuanFunction) ) + throw luan.exception("bad 'finally' value (function expected, got "+Luan.type(obj)+")"); + finallyFn = (LuanFunction)obj; + } + try { + luan.call(tryFn); + } catch(LuanException e) { + if( catchFn == null ) + throw e; + luan.call(catchFn,new Object[]{e}); + } finally { + if( finallyFn != null ) + luan.call(finallyFn); + } + } + + @LuanMethod public static Object[] pcall(LuanState luan,LuanFunction f,Object... args) { + try { + Object[] r = Luan.array(luan.call(f,args)); + Object[] rtn = new Object[r.length+1]; + rtn[0] = true; + for( int i=0; i<r.length; i++ ) { + rtn[i+1] = r[i]; + } + return rtn; + } catch(LuanException e) { + return new Object[]{false,e}; + } + } + }