From 75fb975410c3e30d1cef58c3b3b90a80d5eefd54 Mon Sep 17 00:00:00 2001 From: Ian Farmer Date: Tue, 4 Sep 2007 01:52:33 +0000 Subject: [PATCH] Fix a few VM issues which were causing subtle failures in the test cases. Includes a new VM method, newCall, which must be called before pushing a call onto the stack to make sure that base is set correctly. A couple of weird issues remain with autoload.lua. --- src/addon/java/lua/addon/luajava/LuaJava.java | 2 +- src/test/java/LuaJavaAppRunner.java | 4 +++ src/test/java/lua/LuaJTest.java | 6 ++++- src/test/res/autoload.lua | 11 +++++--- src/test/res/autoload.luac | Bin 605 -> 565 bytes src/test/res/calls-expected.out | 4 +++ src/test/res/calls.lua | 24 ++++++++++++++++++ src/test/res/calls.luac | Bin 0 -> 839 bytes 8 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 src/test/res/calls-expected.out create mode 100644 src/test/res/calls.lua create mode 100644 src/test/res/calls.luac diff --git a/src/addon/java/lua/addon/luajava/LuaJava.java b/src/addon/java/lua/addon/luajava/LuaJava.java index cc7631ea..fbf3f512 100644 --- a/src/addon/java/lua/addon/luajava/LuaJava.java +++ b/src/addon/java/lua/addon/luajava/LuaJava.java @@ -128,7 +128,7 @@ public final class LuaJava extends LFunction { LValue v = CoerceJavaToLua.coerce( o ); vm.push( v ); } catch (NoSuchFieldException nsfe) { - vm.setResult( new LMethod(m_instance,clazz,s) ); + vm.push( new LMethod(m_instance,clazz,s) ); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/src/test/java/LuaJavaAppRunner.java b/src/test/java/LuaJavaAppRunner.java index 519b36d4..13202b8f 100644 --- a/src/test/java/LuaJavaAppRunner.java +++ b/src/test/java/LuaJavaAppRunner.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.io.InputStream; import lua.StackState; +import lua.addon.luacompat.LuaCompat; import lua.addon.luajava.LuaJava; import lua.io.Closure; import lua.io.LoadState; @@ -20,6 +21,9 @@ public class LuaJavaAppRunner { public static void main( String[] args ) throws IOException { + // add LuaCompat bindings + LuaCompat.install(); + // add LuaJava bindings LuaJava.install(); diff --git a/src/test/java/lua/LuaJTest.java b/src/test/java/lua/LuaJTest.java index 66bd93c7..74571f59 100644 --- a/src/test/java/lua/LuaJTest.java +++ b/src/test/java/lua/LuaJTest.java @@ -51,7 +51,11 @@ public class LuaJTest extends TestCase { public void testBoolean() throws IOException, InterruptedException { runTest( "boolean" ); } - + + public void testCalls() throws IOException, InterruptedException { + runTest( "calls" ); + } + public void testCoercions() throws IOException, InterruptedException { runTest( "coercions" ); } diff --git a/src/test/res/autoload.lua b/src/test/res/autoload.lua index 723e53ed..4bd6047f 100644 --- a/src/test/res/autoload.lua +++ b/src/test/res/autoload.lua @@ -1,11 +1,14 @@ +-- Clear out builtin math package +math = nil + local function autoload(table, key) - local chunk = loadfile("/"..key..".luac") + local chunk = loadfile(key..".luac") table[key] = chunk() end -autoload_mt = { __index = autoload } - -setmetatable(_G, autoload_mt) +setmetatable(_G, { __index = autoload } ) +-- local result = math.sqrt(9.0) +-- print("x=", result) print("square root of 9.0 is ", math.sqrt(9.0)) print("math.pi=", math.pi); diff --git a/src/test/res/autoload.luac b/src/test/res/autoload.luac index e49e9fc43b3bd9d2f4592b648f1b368a77d22484..aa2a19647673355e1e97f1f7781686d160973f30 100644 GIT binary patch literal 565 zcmZ`%Jxc>Y5PcgrY6NW*ECe~kI@nqWXAx{{>;qv8YjVA`xH?@+7m;&+|}bBTZ>*4jEBW9J93$BgEQA1B^< z_dH5q_juBI0L9UF(n&9Qf!${^&C`j8J=Ytv!sSVnXPJ+(estTsiPCvg@y=(the&8w zv2D<)Rdbr&Rpir@vMaN68jw2cDqIpPiZu7Y4;JQ);fe!oLO^2`paE*EDOt6$ek#dQ zr{#6&0TF4yU8rgcHI+u?)!YoAku5PyUX_-)D!PMWGA#L2A=8(U5b>zrF0|JFpl|*c Pv=s4de}}TcDGy}7x(P}q literal 605 zcmZ`$Jxc>Y5Pf@Yg7K?TECe~kI@($YYY{@k#y$|%oTyoh1?KVaJg+JyZ#D8Fy zALG0`4lLrp+xd92v$Hc-FPVLX(pn$l41lqjOD2gOG$)xAzmlMKA?B^%*#I4Q-xj;& zfprdS#qJufT2NRSg{@ck&NoV_06Rc+jpP@iK(22{*NF zR9eeL>c9#jH%2!^k!;T6$FlM~mR?Jt)-(guk3_8n8zYDEcv=BJW~DwoR4$R7_h{rE z{1wX=l{w2{%$@@Y-IXnKxP5T})+DhHtWkkI(B_BB8HfS4PG%f^&@Fss^heojT;wu? f!tF7WYz5BvFWAd};Fk7*^WhuS@`}NoOMvnV^_Edm diff --git a/src/test/res/calls-expected.out b/src/test/res/calls-expected.out new file mode 100644 index 00000000..efcced8d --- /dev/null +++ b/src/test/res/calls-expected.out @@ -0,0 +1,4 @@ +15 +15 +1.2246467991473532E-16 +1.2246467991473532E-16 diff --git a/src/test/res/calls.lua b/src/test/res/calls.lua new file mode 100644 index 00000000..f9d8aa2f --- /dev/null +++ b/src/test/res/calls.lua @@ -0,0 +1,24 @@ +local function f(x) + -- tailcall to a builtin + return math.sin(x) +end + +local function factorial(i) + local function helper(sum, n) + if n <= 0 then + return sum + else + -- tail call to a nested Lua function + return helper(n + sum, n - 1) + end + end + return helper(0, i) +end + +local result1 = factorial(5) +print(result1) +print(factorial(5)) + +local result2 = f(math.pi) +print(result2) +print(f(math.pi)) diff --git a/src/test/res/calls.luac b/src/test/res/calls.luac new file mode 100644 index 0000000000000000000000000000000000000000..4cad7222601f3e69dc8ac3585df7a8cdea6a6347 GIT binary patch literal 839 zcmZ`%%TB^j6r5XH!S{>c1H#I+iT;4?LRW6wt4YHnG$}|*NnF-M{R;J4=)#?!z(@EA z&a~73(c8>DbI&<5_W||mv3~<&+ja%(0Nj=zhNIJP>~m2Jt6V2NA=)mG@Jq?x5|~e} zVPK|i!4r4}XyiFeSazO(mDIJS>~}nFVG`5u3J$!m{+ha_TWXj`lm0e!C4!^s+A9ph zC>X@B9n!uZ_h73$3}7*67(yzNlKL-+D|bRK;EvgQMmej0&P9?@FyPEX5^`OJdSox@ zBf%?Wb3RG6BWmDO7~108NM9;S={pkhL8=4DX?C^TRy}26i!c_PHO3=MP;-$qnX17g z&&+*Myy;CA%ygt*vl;omU=niCafHXOUWm)){-9aM+15 zQwfFbKcM%Jfe;Y6c+NltEj9GtvI&Xy=CDs*ph~`s8guhF;JUye`G3$}+P|CK>X=M# h)%9EPLlpR7R#R3l7j;JCFh0wE@WtFYc3D?W;|F{yM?nAp literal 0 HcmV?d00001