Copyright © 2007-2009 Luaj.org. Freely available under the terms of the Luaj license.
examples · concepts · libraries · building · downloads · release notes
From the main distribution directory line type:
java -cp lib/luaj-j2se-1.0.jar lua src/test/res/test4.lua
You should see the following output:
40
From the main distribution directory line type:
java -cp lib/luaj-j2se-1.0.jar luac src/test/res/test4.lua java -cp lib/luaj-j2se-1.0.jar lua luac.out
The compiled output should run and produce the same result.
The following pattern is used within J2SE
import org.luaj.platform.*; import org.luaj.vm.*; String script = "main.lua"; Platform.setInstance( new J2sePlatform() ); LuaState vm = Platform.newLuaState(); org.luaj.compiler.LuaC.install(); vm.getglobal( "dofile" ); vm.pushstring( script ); vm.call( 1, 0 );
A simple example may be found in
src/sample/SampleJ2seMain.java
You must include the library lib/luaj-j2se-1.0.jar in your class path.
The following pattern is used within MIDlets:
import org.luaj.platform.*; import org.luaj.vm.*; String script = "main.lua"; Platform.setInstance( new J2meMidp20Cldc11Platform( midlet ) ); LuaState vm = Platform.newLuaState(); org.luaj.compiler.LuaC.install(); vm.getglobal( "dofile" ); vm.pushstring( script ); vm.call( 1, 0 );
The file must be a resource within within the midlet jar for dofile() to find it. Any files included via require() must also be part of the midlet resources.
A simple example may be found in
src/sample/SampleMIDlet.java
You must include the library lib/luaj-j2me-1.0.jar in your midlet jar. They can be obfuscated if desired.
To include it, include the following after the Platform is created, but before the script is executed:
org.luaj.compiler.LuaC.install();
To omit the compiler, omit this line from your startup code.
The standard use of JSR-233 scripting engines may be used:
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine e = mgr.getEngineByExtension(".lua");
e.put("x", 25);
e.eval("y = math.sqrt(x)");
System.out.println( "y="+e.get("y") );
All standard aspects of script engines including compiled statements should be supported.
You must include the library lib/luaj-j2se-1.0.jar in your class path.
A working example may be found in
src/script/ScriptEngineSample.java
The following libraries are loaded by default in J2ME and J2SE platforms:
base coroutine math package string table
The following libraries are optional, but preconfigured for some platforms and tools:
io os debug luajava
The J2ME platform has an optional, partial implementation of the io in
src/j2me/org/luaj/lib/j2me/Cldc10IoLib.javaTo install into your vm instance use (j2me only):
LuaState vm = Platform.newLuaState(); org.luaj.lib.j2me.Cldc10IoLib.install(vm._G);
See the sample midlet int src/sample/SampleMIDlet for an example.
src/core/org/luaj/lib/OsLib.javaA slightly more complete version for J2SE is in:
src/j2se/org/luaj/lib/j2se/J2seOsLib.javaTime is a represented as number of milliseconds since the epoch, and most time and date formatting, locales, and other features are not implemented.
debugInstall from Java using:
LuaState vm = Platform.newLuaState(); org.luaj.lib.DebugLib.install(vm);or install from lua using:
require 'org.luaj.lib.DebugLib'The lua command line utility includes the debug library by default.
The following lua script will open a swiing frame on J2SE:
jframe = luajava.bindClass( "javax.swing.JFrame" ) frame = luajava.newInstance( "javax.swing.JFrame", "Texts" ); frame:setDefaultCloseOperation(jframe.EXIT_ON_CLOSE) frame:setSize(300,400) frame:setVisible(true)
See a longer sample in src/test/res/swingapp.lua for details, or try running it using:
java -cp lib/luaj-j2se-1.0.jar lua src/test/res/swingapp.lua
The J2ME platform does not include this library, and it cannot be made to work because of the lack of a reflection API in J2SE.
Other targets exist for creating distribution file an measuring code coverage of unit tests.
A large array of test scripts may be found in
src/test/res/*.lua
A large set of JUnit tests are invoked by the JUnit 3 suite:
src/test/java/AllTests.lua
These tests are used for to produce code coverage statistics using build-coverage.xml.
SourceForge Luaj Project Page SourceForge Luaj Download Areaand LuaForge:
LuaForge Luaj Project Page LuaForge Luaj Project Area
|