Type coercion for luajava package, make luajava an "addon"
This commit is contained in:
49
src/test/java/LuaJavaAppRunner.java
Normal file
49
src/test/java/LuaJavaAppRunner.java
Normal file
@@ -0,0 +1,49 @@
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import lua.GlobalState;
|
||||
import lua.StackState;
|
||||
import lua.addon.luajava.LuaJava;
|
||||
import lua.io.Closure;
|
||||
import lua.io.LoadState;
|
||||
import lua.io.Proto;
|
||||
import lua.value.LString;
|
||||
|
||||
/**
|
||||
* Program to run a compiled lua chunk for test purposes,
|
||||
* but with the LuaJava add-ons added in
|
||||
*
|
||||
* @author jim_roseborough
|
||||
*/
|
||||
public class LuaJavaAppRunner {
|
||||
|
||||
public static void main( String[] args ) throws IOException {
|
||||
|
||||
// add LuaJava bindings
|
||||
LuaJava.install();
|
||||
|
||||
// get script name
|
||||
String script = (args.length>0? args[0]: "/swingapp.luac");
|
||||
System.out.println("loading '"+script+"'");
|
||||
|
||||
// new lua state
|
||||
StackState state = new StackState();
|
||||
|
||||
// push args onto stack
|
||||
for ( int i=1; i<args.length; i++ )
|
||||
state.push(new LString(args[i]));
|
||||
|
||||
// load the file
|
||||
InputStream is = LuaJavaAppRunner.class.getResourceAsStream( script );
|
||||
Proto p = LoadState.undump(state, is, script);
|
||||
|
||||
// create closure to execute
|
||||
Closure c = new Closure( state, p );
|
||||
state.push( c );
|
||||
for ( int i=0; i<args.length; i++ )
|
||||
state.push( new LString(args[i]) );
|
||||
state.docall(args.length, 0);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
@@ -19,7 +18,7 @@ public class LuacRunner {
|
||||
public static void main( String[] args ) throws IOException {
|
||||
|
||||
// get script name
|
||||
String script = (args.length>0? args[0]: "src/test/res/test1.luac");
|
||||
String script = (args.length>0? args[0]: "/test1.luac");
|
||||
System.out.println("loading '"+script+"'");
|
||||
|
||||
// new lua state
|
||||
@@ -30,7 +29,7 @@ public class LuacRunner {
|
||||
state.push(new LString(args[i]));
|
||||
|
||||
// load the file
|
||||
InputStream is = new FileInputStream( script );
|
||||
InputStream is = LuacRunner.class.getResourceAsStream( script );
|
||||
Proto p = LoadState.undump(state, is, script);
|
||||
|
||||
// create closure to execute
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#!/bin/bash
|
||||
LUA_HOME=/cygdrive/c/programs/lua5.1
|
||||
TESTS="test1 test2 test3 test4 test5"
|
||||
TESTS="test7"
|
||||
TESTS="test1 test2 test3 test4 test5 swingapp"
|
||||
TESTS="swingapp"
|
||||
for x in $TESTS
|
||||
do
|
||||
echo compiling $x
|
||||
|
||||
20
src/test/res/swingapp.lua
Normal file
20
src/test/res/swingapp.lua
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
frame = luajava.newInstance( "javax.swing.JFrame", "Texts" );
|
||||
pane = luajava.newInstance( "javax.swing.JPanel" );
|
||||
borderFactory = luajava.bindClass( "javax.swing.BorderFactory" )
|
||||
border = borderFactory:createEmptyBorder( 30, 30, 10, 30 )
|
||||
pane:setBorder( border )
|
||||
label = luajava.newInstance( "javax.swing.JLabel", "This is a Label" );
|
||||
|
||||
|
||||
layout = luajava.newInstance( "java.awt.GridLayout", 2, 2 );
|
||||
pane:setLayout( layout )
|
||||
pane:add( label )
|
||||
pane:setBounds( 20, 30, 10, 30 )
|
||||
|
||||
borderLayout = luajava.bindClass( "java.awt.BorderLayout" )
|
||||
frame:getContentPane():add(pane, borderLayout.CENTER )
|
||||
jframe = luajava.bindClass( "javax.swing.JFrame" )
|
||||
frame:setDefaultCloseOperation(jframe.EXIT_ON_CLOSE)
|
||||
frame:pack()
|
||||
frame:setVisible(true)
|
||||
BIN
src/test/res/swingapp.luac
Normal file
BIN
src/test/res/swingapp.luac
Normal file
Binary file not shown.
@@ -1,5 +1,84 @@
|
||||
for cycle = 1,100 do
|
||||
|
||||
a = 123 + 456
|
||||
print( a )
|
||||
|
||||
i = 777
|
||||
while i<780 do
|
||||
print(i)
|
||||
i = i+1
|
||||
end
|
||||
|
||||
a,b = 0,1
|
||||
while true do -- infinite loop
|
||||
print( b )
|
||||
a,b = b,a+b
|
||||
if a>10 then break end -- exit the loop if the condition is true
|
||||
end
|
||||
|
||||
for count = 336,330,-2 do print(count) end -- numerical iteration
|
||||
|
||||
function sum(a,b,c,d) -- "sum" method
|
||||
local d = d or 0
|
||||
return a+b+c+d -- return sum
|
||||
end
|
||||
print( sum( 1, 2, 3, 4 ) )
|
||||
print( sum( 5, 6, 7 ) )
|
||||
print( sum( 9, 10, 11, 12, 13, 14 ) )
|
||||
print( sum( sum(1,2,3,4), sum(5,6,7), sum(9,10,11,12,13,14), 15 ) )
|
||||
|
||||
|
||||
function f0() print( "f0:" ) end
|
||||
function f1(a) print( "f1:", a ) end
|
||||
function f2(a,b) print( "f2:", a, b ) end
|
||||
function f3(a,b,c) print( "f3:", a, b, c ) end
|
||||
function f4(a,b,c,d) print( "f4:", a, b, c, d ) end
|
||||
|
||||
f0() f0( "a1/1" ) f0( "a1/2", "a2/2" ) f0( "a1/3", "a2/3", "a3/3" ) f0( "a1/4", "a2/4", "a3/4", "a4/4" )
|
||||
f1() f1( "a1/1" ) f1( "a1/2", "a2/2" ) f1( "a1/3", "a2/3", "a3/3" ) f1( "a1/4", "a2/4", "a3/4", "a4/4" )
|
||||
f2() f2( "a1/1" ) f2( "a1/2", "a2/2" ) f2( "a1/3", "a2/3", "a3/3" ) f2( "a1/4", "a2/4", "a3/4", "a4/4" )
|
||||
f3() f3( "a1/1" ) f3( "a1/2", "a2/2" ) f3( "a1/3", "a2/3", "a3/3" ) f3( "a1/4", "a2/4", "a3/4", "a4/4" )
|
||||
f4() f4( "a1/1" ) f4( "a1/2", "a2/2" ) f4( "a1/3", "a2/3", "a3/3" ) f4( "a1/4", "a2/4", "a3/4", "a4/4" )
|
||||
|
||||
function g0(a,b,c,d) return end
|
||||
function g1(a,b,c,d) return d end
|
||||
function g2(a,b,c,d) return c, d end
|
||||
function g3(a,b,c,d) return b, c, d end
|
||||
function g4(a,b,c,d) return a, b, c, d end
|
||||
|
||||
z = g0("c0.1/4", "c0.2/4", "c0.3/4", "c0.4/4")
|
||||
print( "z0:", z )
|
||||
z = g2("c2.1/4", "c2.2/4", "c2.3/4", "c2.4/4")
|
||||
print( "z2:", z )
|
||||
z = g4("c4.1/4", "c4.2/4", "c4.3/4", "c4.4/4")
|
||||
print( "z4:", z )
|
||||
|
||||
a,b,c,d = g0( "c0.1/4", "c0.2/4", "c0.3/4", "c0.4/4" )
|
||||
print( "g0:", a, b, c, d, "(eol)" )
|
||||
a,b,c,d = g2( "b2.1/4", "b2.2/4", "b2.3/4", "b2.4/4" )
|
||||
print( "g2:", a, b, c, d, "(eol)" )
|
||||
a,b,c,d = g4( "b4.1/4", "b4.2/4", "b4.3/4", "b4.4/4" )
|
||||
print( "g4:", a, b, c, d, "(eol)" )
|
||||
|
||||
function func(a,b,c)
|
||||
return a, b, c
|
||||
end
|
||||
|
||||
print( func(11, 12, 13) )
|
||||
print( func(23, 22, 21) )
|
||||
print( func(func(32,33,34), func(45,46,47), func(58,59,50)) )
|
||||
|
||||
function p(a,...)
|
||||
print("a",a)
|
||||
-- print("...",...)
|
||||
-- print("...,a",...,a)
|
||||
-- print("a,...",a,...)
|
||||
end
|
||||
p()
|
||||
p("q")
|
||||
p("q","r")
|
||||
p("q","r","s")
|
||||
|
||||
|
||||
|
||||
end
|
||||
Binary file not shown.
BIN
src/test/res/test7.luac
Normal file
BIN
src/test/res/test7.luac
Normal file
Binary file not shown.
Reference in New Issue
Block a user