From 932960c84684f134827b4fac8640055125763111 Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Tue, 23 Oct 2007 00:34:04 +0000 Subject: [PATCH] Add tests for pcall, add error() builtin, fix assert(), error levels. --- .../java/lua/addon/luacompat/LuaCompat.java | 44 ++++++------------ src/main/java/lua/Builtin.java | 14 ++++++ src/main/java/lua/VM.java | 10 +++- src/main/java/lua/debug/DebugStackState.java | 18 ++++--- src/main/java/lua/value/LThread.java | 7 ++- src/test/java/lua/LuaJTest.java | 11 ++--- src/test/res/compile.sh | 1 - src/test/res/coroutines.luac | Bin 0 -> 3549 bytes src/test/res/mathlib.luac | Bin 0 -> 367 bytes src/test/res/pcalls.lua | 36 ++++++++++++++ src/test/res/pcalls.luac | Bin 0 -> 2083 bytes src/test/res/strlib.luac | Bin 0 -> 2536 bytes 12 files changed, 92 insertions(+), 49 deletions(-) create mode 100644 src/test/res/coroutines.luac create mode 100644 src/test/res/mathlib.luac create mode 100644 src/test/res/pcalls.lua create mode 100644 src/test/res/pcalls.luac create mode 100644 src/test/res/strlib.luac diff --git a/src/addon/java/lua/addon/luacompat/LuaCompat.java b/src/addon/java/lua/addon/luacompat/LuaCompat.java index 9195ad62..5eaf2b26 100644 --- a/src/addon/java/lua/addon/luacompat/LuaCompat.java +++ b/src/addon/java/lua/addon/luacompat/LuaCompat.java @@ -66,7 +66,6 @@ public class LuaCompat extends LFunction { } public static final String[] GLOBAL_NAMES = { - "assert", "loadfile", "tonumber", "rawget", @@ -127,22 +126,21 @@ public class LuaCompat extends LFunction { }; private static final int GLOBALS_BASE = 0; - private static final int ASSERT = GLOBALS_BASE + 0; - private static final int LOADFILE = GLOBALS_BASE + 1; - private static final int TONUMBER = GLOBALS_BASE + 2; - private static final int RAWGET = GLOBALS_BASE + 3; - private static final int RAWSET = GLOBALS_BASE + 4; - private static final int SETFENV = GLOBALS_BASE + 5; - private static final int SELECT = GLOBALS_BASE + 6; - private static final int COLLECTGARBAGE = GLOBALS_BASE + 7; - private static final int DOFILE = GLOBALS_BASE + 8; - private static final int LOADSTRING = GLOBALS_BASE + 9; - private static final int LOAD = GLOBALS_BASE + 10; - private static final int TOSTRING = GLOBALS_BASE + 11; - private static final int UNPACK = GLOBALS_BASE + 12; - private static final int NEXT = GLOBALS_BASE + 13; - private static final int MODULE = GLOBALS_BASE + 14; - private static final int REQUIRE = GLOBALS_BASE + 15; + private static final int LOADFILE = GLOBALS_BASE + 0; + private static final int TONUMBER = GLOBALS_BASE + 1; + private static final int RAWGET = GLOBALS_BASE + 2; + private static final int RAWSET = GLOBALS_BASE + 3; + private static final int SETFENV = GLOBALS_BASE + 4; + private static final int SELECT = GLOBALS_BASE + 5; + private static final int COLLECTGARBAGE = GLOBALS_BASE + 6; + private static final int DOFILE = GLOBALS_BASE + 7; + private static final int LOADSTRING = GLOBALS_BASE + 8; + private static final int LOAD = GLOBALS_BASE + 9; + private static final int TOSTRING = GLOBALS_BASE + 10; + private static final int UNPACK = GLOBALS_BASE + 11; + private static final int NEXT = GLOBALS_BASE + 12; + private static final int MODULE = GLOBALS_BASE + 13; + private static final int REQUIRE = GLOBALS_BASE + 14; private static final int MATH_BASE = 20; @@ -191,18 +189,6 @@ public class LuaCompat extends LFunction { public boolean luaStackCall( VM vm ) { switch ( id ) { - case ASSERT: { - if ( !vm.getArgAsBoolean(0) ) { - String message; - if ( vm.getArgCount() > 1 ) { - message = vm.getArgAsString(1); - } else { - message = "assertion failed!"; - } - throw new RuntimeException(message); - } - vm.setResult(); - } break; case LOADFILE: loadfile(vm, vm.getArgAsString(0)); break; diff --git a/src/main/java/lua/Builtin.java b/src/main/java/lua/Builtin.java index 84ed1335..17c15d7a 100644 --- a/src/main/java/lua/Builtin.java +++ b/src/main/java/lua/Builtin.java @@ -24,6 +24,8 @@ final class Builtin extends JavaFunction { "type", "pcall", "ipairs", + "error", + "assert", }; private static final int PRINT = 0; private static final int PAIRS = 1; @@ -32,6 +34,8 @@ final class Builtin extends JavaFunction { private static final int TYPE = 4; private static final int PCALL = 5; private static final int IPAIRS = 6; + private static final int ERROR = 7; + private static final int ASSERT = 8; private static PrintStream stdout = System.out; @@ -90,6 +94,16 @@ final class Builtin extends JavaFunction { return 2; } } + case ERROR: { + vm.error(vm.tostring(1), vm.gettop()>1? vm.tointeger(2): 1); + } + case ASSERT: { + if ( ! vm.toboolean(1) ) { + vm.error( vm.gettop()>1? vm.tostring(2): "assertion failed!", 0 ); + } else { + return vm.gettop(); + } + } default: luaUnsupportedOperation(); return 0; diff --git a/src/main/java/lua/VM.java b/src/main/java/lua/VM.java index d3675080..8e8bee52 100644 --- a/src/main/java/lua/VM.java +++ b/src/main/java/lua/VM.java @@ -357,6 +357,14 @@ public interface VM { */ public void error(); + /** + * Raises an error with the default level. + * + * In the java implementation this throws a RuntimeException, possibly filling + * line number information first. + */ + public void error(String message); + /** * Raises an error. The message is pushed onto the stack and used as the error message. * It also adds at the beginning of the message the file name and the line number where @@ -365,7 +373,7 @@ public interface VM { * In the java implementation this throws a RuntimeException, possibly filling * line number information first. */ - public void error(String message); + public void error(String message, int level); /** * Controls the garbage collector. [-0, +0, e] diff --git a/src/main/java/lua/debug/DebugStackState.java b/src/main/java/lua/debug/DebugStackState.java index 11830710..5e3d665a 100644 --- a/src/main/java/lua/debug/DebugStackState.java +++ b/src/main/java/lua/debug/DebugStackState.java @@ -94,11 +94,16 @@ public class DebugStackState extends StackState implements DebugRequestListener return source+":"+line+"("+func+")"; } - // override and fill in line number info - public void error(String message) { - super.error( getFileLine(cc)+": "+message ); + public void error(String message, int level) { + super.error( level<=0? message: getFileLine(cc+1-level)+": "+message ); } + + // use line numbers by default + public void error(String message) { + error(message, 1); + } + private void printLuaTrace() { System.out.println( "Lua location: "+getFileLine(cc) ); @@ -112,11 +117,10 @@ public class DebugStackState extends StackState implements DebugRequestListener super.exec(); } catch (AbortException e) { // ignored. Client aborts the debugging session. - } catch ( Exception t ) { - t.printStackTrace(); - printLuaTrace(); - System.out.flush(); } + // let other exceptions be processed + // the same as the base class to minimize differences + // between the debug and non-debug behavior } diff --git a/src/main/java/lua/value/LThread.java b/src/main/java/lua/value/LThread.java index f7b0bfa4..781754dd 100644 --- a/src/main/java/lua/value/LThread.java +++ b/src/main/java/lua/value/LThread.java @@ -54,10 +54,9 @@ public class LThread extends LValue { public void resumeFrom(VM vm, int nargs) { if ( status == STATUS_DEAD ) { - vm.error("cannot resume dead coroutine"); -// vm.settop(0); -// vm.pushboolean(false); -// vm.pushstring("cannot resume dead coroutine"); + vm.settop(0); + vm.pushboolean(false); + vm.pushstring("cannot resume dead coroutine"); return; } diff --git a/src/test/java/lua/LuaJTest.java b/src/test/java/lua/LuaJTest.java index e2a6d570..2b7edbd2 100644 --- a/src/test/java/lua/LuaJTest.java +++ b/src/test/java/lua/LuaJTest.java @@ -16,12 +16,6 @@ import lua.value.LValue; public class LuaJTest extends TestCase { - /* - public void testCoroutines() throws IOException, InterruptedException { - runTest( "coroutines" ); - } - - /*/ public void testTest1() throws IOException, InterruptedException { runTest( "test1" ); } @@ -82,6 +76,10 @@ public class LuaJTest extends TestCase { runTest( "metatables" ); } + public void testPcalls() throws IOException, InterruptedException { + runTest( "pcalls" ); + } + public void testSelect() throws IOException, InterruptedException { runTest( "select" ); } @@ -109,7 +107,6 @@ public class LuaJTest extends TestCase { public void testUpvalues2() throws IOException, InterruptedException { runTest( "upvalues2" ); } - //*/ private void runTest( String testName ) throws IOException, InterruptedException { diff --git a/src/test/res/compile.sh b/src/test/res/compile.sh index 436f0cf8..eb930f15 100644 --- a/src/test/res/compile.sh +++ b/src/test/res/compile.sh @@ -1,6 +1,5 @@ #!/bin/bash TESTS=`echo *.lua` -TESTS="test3.lua" for x in $TESTS do echo compiling $x diff --git a/src/test/res/coroutines.luac b/src/test/res/coroutines.luac new file mode 100644 index 0000000000000000000000000000000000000000..7f11dd3093562c565f3695793fdbe0e9310edc65 GIT binary patch literal 3549 zcmbtWTW=dh6h1Rv$4x1BkpeYUfN)m>go}{4WH!Pf1jgitf z_yrbE{E&4e^TbcckLXX}`(}1*XXAAUjP&i9bD7b*Ik#qNE3g)cp&vQDXuk8NSa}}b zik`*eL7H?2>9E^Px25yRGER|t{X(rA< zbq^_lcfkfaCHtc%`cJHHjzpb+PGDQrO4jsV)PQYWiyFiB1h$VSdlh^`gU6SwOpqpj zU`FRMu#3*+g=zP09rO-yPlKl!VT`A{#p^Y9`H~cS5lh;=?f#%O81}c5Za+Ch`tVkG z(LDDSla2GV(;wc4Ex+IC9@58dT*vb@KSf3JH!U_eq!oFb8Rz6N{Em!RdUIaFn&OUj z&gIOK9b%pEgRibYzA>5#ZwNE22o~W0x@QsGToKI3B3^-v*jNENDmaVu^%b}Q-6Gxq zAHW;W7|(O!)0{g16$xDGyff!`uE6!(DakRM?cR>Ooohkd*u?WAKVAC~v65pWU zS%f!3cv0M^NTv{8uTx%^d7V;dDQGLO(YykeUXOM(7VEChrPrcmvHkq#Z(m~lEN{=Z zH3lnhNxnMf>Gy9gx$A8ATS?a^X`Y0$+|MT^Jq_O##_r7=Z-JJ9@xKULuqnek(5=F| z(3jyo=q|$huqnd_&|QQNVN-_up2NbAk3qiV6n&)*aso^Y9;kUn&HKtUL-RyK8KZAh zp^IbK#>(}MI{HR+gxWue^=-(11L_m&`vT^8w>H|nPp4I<#s7vd=yG`ytWl1Zj zVPz=f{;th_p196;;#moYM0mbWmVmWk5v-m?dsze`5paS8ioqy;$eS9RR-S zg;OMb*Y1NsTHsiOgRbvNGPy9m#$=C-mUWievLOE7p95Zq5dwWLZ{FN|Y4BKW`G;Ep z#aP+AY0E~-!2^7=1m8jy@Ezuh*acsNfvgB##sE633TU7O_|{JD;0)(`3%e_Isp}?z z^Svc>UdEj+Q2_tuc(?iA7XIA=We#ovw{kECLmmeD6=VcM8^TNa{R%XoTZVne%kTqq Q7lG&D*^k`+2X8#bKOV-cbpQYW literal 0 HcmV?d00001 diff --git a/src/test/res/mathlib.luac b/src/test/res/mathlib.luac new file mode 100644 index 0000000000000000000000000000000000000000..43d7c928acdeda2bf1769d3f51dbb2f458be90d4 GIT binary patch literal 367 zcmZ`!yAHxI47{`kAtXi?Kn#2U-w+0vSlC$#2ob5YrH?%w;JaGB47*jj&|13KclIUD z@>m2XC?c9*20%Z9{1KYVHY@_BE-04&rT}QnN0OQ-2g&`i7l^pZOaz~E6OD3>GmuD$X@|2{n!A~eZvFA??xeXE Vulz_2z+-&~@=zaSw;^z=i64pOB1-@O literal 0 HcmV?d00001 diff --git a/src/test/res/pcalls.lua b/src/test/res/pcalls.lua new file mode 100644 index 00000000..12aa0385 --- /dev/null +++ b/src/test/res/pcalls.lua @@ -0,0 +1,36 @@ + +-- sample lua function that returns values in reverse order +local function lc(a,b,c) + return c,b,a +end + +-- sample lua function that throws a lua error +local function le(a,b,c) + error( 'sample error', 0 ) +end + +-- function that does a plain call to the underlying function +local function cp(f,a,b,c) + global = f(a,b,c) + return global +end + +-- function that does a tail call to the underlying function +local function ct(f,a,b,c) + return f(a,b,c) +end + +-- lua calls +print( 'lc(22,33,44)', lc(22,33,44) ) +print( 'pcall(lc,22,33,44)', pcall(lc,22,33,44) ) +print( 'pcall(le,22,33,44)', pcall(le,22,33,44) ) +print( 'cp(lc,22,33,44)', cp(lc,22,33,44) ) +print( 'pcall(cp,lc,22,33,44)', pcall(cp,lc,22,33,44) ) +print( 'pcall(cp,le,22,33,44)', pcall(cp,le,22,33,44) ) +print( 'ct(lc,22,33,44)', ct(lc,22,33,44) ) +print( 'pcall(ct,lc,22,33,44)', pcall(ct,lc,22,33,44) ) +print( 'pcall(ct,le,22,33,44)', pcall(ct,le,22,33,44) ) + +print( "assert(true,'a','b','c')", assert( true, 'a', 'b', 'c' ) ) +print( "pcall(assert,true,'a','b','c')", pcall(assert, true, 'a', 'b', 'c' ) ) +print( "pcall(assert,false,'a','b','c')", pcall(assert, false, 'a', 'b', 'c' ) ) diff --git a/src/test/res/pcalls.luac b/src/test/res/pcalls.luac new file mode 100644 index 0000000000000000000000000000000000000000..4063726f0e3448e719f9e0d7058c570743183d9d GIT binary patch literal 2083 zcmbVN+iuf95It*e+JMSM;v%4Fh@etZ$>O1{c(!@qi5EV>F)2baG;)$Y+m-kW9{G^C zQvU++#D{QZH@+m~VzJW6&g`C@8PCpa4nNGok5F3cHVy#z<0y>d$(wi2TfdD}2us=H#er?vs-lPFivDf|G9MC`bIL zt#$&Z@Wlw|i~chD%WX_<8B;S{pSR>W4sDT_k(V1Ds_#^-8jtx{!|Ni&699UTKjY-fw<+{Jig7eJJ@wAc zyz}!DxLn|P>sul97Xse<`Vluw%?#qmtBzQEQcI{)Ms>2jRn%9lcPD49I&@apQtr2gH zk=`ojB8(@ip2(3QDQ{U}Pv$xM2rLet4fPvvs&?-Yy86 zohql2t1733W^*c9F{E3h-6UoyrPY{^_=qr8)NfUIk2k=yF88Xqq>rn(3T?<$pwMhw zqREh_U2aBhLcUXMKHC9ll3jQ~&Mq91vkM(^n&61MCg_s43)00+&?B!2UXiy8ugPhG hWAd6pYSty<$dvx9P)DXl3N;!VQmOJkfJ9Mc`~@|7*{=Wq literal 0 HcmV?d00001 diff --git a/src/test/res/strlib.luac b/src/test/res/strlib.luac new file mode 100644 index 0000000000000000000000000000000000000000..bb8dd7508b14feb1a6916b21b42469a99083c68b GIT binary patch literal 2536 zcmah~O-~d-5PiMdu!zR^88sk`x)W#DKm@#aqSpjYB*qX=BFQWWY_fdFE{Wb|4sbB) zMUI^O1^fZytOn2i0QE2E&8x5GBfv@zvw7Xs^{TqMtE+nlX14q%P+IFgoC9E5?agMR zKHA*!Im*65&o0FtV#)y1L4+^@I#8HHpa!Ws#uHFvDsK_LzgJB{2FV^&{9uJjkQJSE0p}^y5l!rIb-7!P95t4=rbg|(a~zfamNy8 zn$>0=Lu)c_vkHBQg46FbTJ3e`_vcTJ-)c2hlbG(TJ4*{i3uvi)zs{dumo}M!GKxY2 zgC<_*s^4B%N!)4a(@uE5nwj;PIJ94_Lthq#vx%Y18w>Q1P1ZfXTA_{XAeKFj(ol{` z1<%AusK0K%NaCzCGMm0Ac5Sbx*(zdxW`Fqa%SS~-neaUk;uS72KXINH{AQDvFZl-8 zxgr)&dcba*h?JAqL$RvloGdD;t5pjuh<Qhh5Qd7jLB4H=wyI#YeFNZt z!M>R<3XE-3fFHbqAE}G=L86}yYigGkJE2nV!dmesck_$BQeJ1-=atuTYl~;lPb=5; zp4}gL_`H(X&wKNe6nlgj^D0@^|4hcSdF9DUIbpquRGwrutqe_3W{sJ|zMrHm-dzo9zTVe!>D4!hQ;1L}4=$40QmtmN49vt!+ zZc)y|ZSooJP`05=?licxk&AG+(}{Gn!5+v)ZB)}ytY%Xo?j?gBxnPp_DRcZ^OR-F| zms8F_j_~wSkzDhMNnQtUlqDi7bjR5M5=xemhjdwD4*5fsqqIRbZi9riLAGmyY}1Ak zxje}JY>+#Y2f2wh$kUsL^W<%~K+cAX