Copyright © 2009-2010 Luaj.org. Freely available under the terms of the Luaj license.
introduction · examples · concepts · libraries · building · downloads · release notes
This is an alpha release for a luaj 2.0. The most recent stable release is 1.0.3
From the main distribution directory line type:
java -cp lib/luaj-jse-2.0-alpha1.jar lua examples/lua/hello.lua
You should see the following output:
hello, world
From the main distribution directory line type:
java -cp lib/luaj-jse-1.0.jar luac examples/lua/hello.lua java -cp lib/luaj-jse-1.0.jar lua luac.out
The compiled output should run and produce the same result.
The following pattern is used within Java SE
import org.luaj.vm2.*;
import org.luaj.vm2.lib.*;
import org.luaj.vm2.compiler.LuaC;
String script = "examples/lua/hello.lua";
LuaC.install();
LuaValue _G = JsePlatform.standardGlobals();
_G.get("dofile").call( LuaValue.valueOf(script) );
A simple example may be found in
examples/jse/SampleJseMain.java
You must include the library lib/luaj-jse-2.0-alpha1.jar in your class path.
The following pattern is used within MIDlets:
import org.luaj.vm2.*;
import org.luaj.vm2.lib.*;
import org.luaj.vm2.compiler.LuaC;
String script = "examples/lua/hello.lua";
LuaC.install();
LuaValue _G = JmePlatform.standardGlobals();
_G.get("dofile").call( LuaValue.valueOf(script) );
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
examples/jme/SampleMIDlet.java
You must include the library lib/luaj-jme-2.0-alpha1.jar in your midlet jar. They can be obfuscated if desired.
To include the Java it, include the following sometime before lua source files are loaded:
org.luaj.vm2.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-jse-2.0-alpha1.jar in your class path.
A working example may be found in
examples/jse/ScriptEngineSample.java
A code generator that compiles lua to java bytecode base on the bcel library is now included.
To use it at runtime, the tool "lua" has an option "-j" to compile into java bytecode.
java -cp luaj-jse-2.0-alpha1.jar;lib/bcel-5.2.jar lua -j examples/lua/hello.lua
To compile lua files into Java in advance, the tool "luajc" is provided:
cp examples/lua/hello.lua . java -cp luaj-jse-2.0-alpha1.jar;lib/bcel-5.2.jar luajc hello.lua ls -l hello.classYou should see a class file of 906 bytes or so. A version of the bcel library must be in your class path for code generation to work. The ant script contains a target for fetching bcel v 5.2:
ant bcel-lib
Files compiled into java in this way can be run via Java class loading, without the bcel libraries (generated class files must be in your class path):
java -cp luaj-jse-2.0-alpha1.jar;. lua -l helloThe current bytecode generator produces a separate class file for each prototype, and generated class files do not work properly with module() or setfenv().
The following libraries are loaded by default in Java ME and Java SE platforms:
base coroutine math package string table
The following libraries are optional, but preconfigured for some platforms and tools:
io os debug luajava
The Java ME platform has an optional, partial implementation of the io in
src/jme/org/luaj/vm2/lib/jme/JmeIoLib.java
src/core/org/luaj/lib/OsLib.javaA slightly more complete version for Java SE is in:
src/jse/org/luaj/vm2/lib/jse/JseOsLib.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:
org.luaj.vm2.lib.DebugLib.install(vm);or install from lua using:
require 'org.luaj.vm2.lib.DebugLib'The lua command line utility includes the debug library by default.
The following lua script will open a swiing frame on Java SE:
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-jse-2.0-alpha1.jar lua src/test/res/swingapp.lua
The Java ME platform does not include this library, and it cannot be made to work because of the lack of a reflection API in Java SE.
Other targets exist for creating distribution file an measuring code coverage of unit tests.
A large array of test scripts may be found in
test/lua/*.lua
A large set of JUnit tests are invoked by the JUnit 3 suite:
test/junit/org/luaj/vm2/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
Known Issues
|