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-2.0-alpha1.jar luac examples/lua/hello.lua java -cp lib/luaj-jse-2.0-alpha1.jar lua luac.out
The compiled output "luac.out" is lua bytecode and should run and produce the same result.
Luaj can compile to lua bytecode if the bcel library is on the class path. From the main distribution directory line type:
ant bcel-lib java -cp lib/luaj-jse-2.0-alpha1.jar;lib/bcel-5.2.jar luajc -s examples/lua -d . hello.lua java -cp lib/luaj-jse-2.0-alpha1.jar;. lua -l hello
The output hello.class is Java bytecode, should run and produce the same result. There is no runtime dependency on the bcel library, but the compiled classes must be in the class path.
The following pattern is used within Java SE
import org.luaj.vm2.*;
import org.luaj.vm2.lib.*;
String script = "examples/lua/hello.lua";
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 for MIDlets the JmePlatform is used instead:
import org.luaj.vm2.*;
import org.luaj.vm2.lib.*;
String script = "examples/lua/hello.lua";
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.
An ant script to build and run the midlet is in
build-midlet.xml
You must install the wireless toolkit and define WTK_HOME for this script to work.
To exclude the lua-to-lua-bytecode compiler, do not call standardGlobals() or debugGlobals() but instead initialize globals with including only those libraries that are needed and omitting the line:
org.luaj.vm2.compiler.LuaC.install();
To compile from lua to Java bytecode for all lua loaded at runtime, install the LuaJC compiler after globals have been created using:
org.luaj.vm2.jse.luajc.LuaJC.install();
This will compile all lua bytecode into Java bytecode, regardless of if they are loaded as lua source or lua binary files.
The bcel library must be on the class path for this to work.
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
The following libraries are loaded in py both JsePlatform.standardGlobals() and JmePlatform.standardGlobals():
base coroutine io math os package string table
The JsePlatform.standardGlobals() globals also include:
luajava
The JsePlatform.debugGlobals() and JsePlatform.debugGlobals() functions produce globals that include:
debug
The JmePlatform.standardGlobals() instantiated the io library io in
src/jme/org/luaj/vm2/lib/jme/JmeIoLib.javaThe JsePlatform.standardGlobals() includes support for random access and is in
src/jse/org/luaj/vm2/lib/jse/JseIoLib.java
The basic os library implementation us used by JmePlatform and is in:
src/core/org/luaj/lib/OsLib.javaA richer version for use by JsePlatform is :
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.
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.
The lua connand line tool includes luajava.
Other targets exist for creating distribution file an measuring code coverage of unit tests.
The main luaj JUnit tests are organized into a JUnit 3 suite:
test/junit/org/luaj/vm2/AllTests.lua
Unit test scripts can be found in these locations
test/lua/*.lua test/junit/org/luaj/vm2/compiler/lua5.1-tests.zip test/junit/org/luaj/vm2/compiler/regressions.zip test/junit/org/luaj/vm2/vm1/luajvm1-tests.zip
A build script for running unit tests and producing code coverage statistics is in
build-coverage.xmlIt relies on the cobertura code coverage library.
SourceForge Luaj Project Page SourceForge Luaj Download Areaand LuaForge:
LuaForge Luaj Project Page LuaForge Luaj Project Area
Known Issues
|