Getting Started with LuaJ

James Roseborough, Ian Farmer, Version 2.0-alpha1

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

1 - Introduction

Goals of Luaj

Luaj is a lua interpreter based on the 5.1.x line of lua with the following goals in mind:

Differences with 1.0

In addition to the basic goals of luaj, version 2.0 is aimed at improving on the 1.0 vm in the following aspects.

2 - Simple Examples

Run a script in Java SE

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

Compile a script to lua bytecode

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.

Compile a script to java bytecode

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.



Run a script in a Java Application

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.

Run a script in a MIDlet

The for MIDlets the JmePlatform is used instead:

	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.

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.

Including the lua bytecode compiler

By default, the compiler is not included so as to minimize footprint. Without a compiler, files can still be executed, but they must be compiled elsewhere beforehand. The "luac" utility is provided in the jse jar for this purpose, or a standard lua compiler can be used.

To include the lua-to-lua-bytecode compiler, 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.

Including the lua-to-Java-bytecode compiler

To compile from lua to Java bytecode for all lua loaded at runtime, use the LuaJC class:

	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.

Run a script using JSR-233 Dynamic Scripting

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

3 - Concepts

Globals

The old notion of platform has been replaced with creation of globals. Two classes are provided to encapsulate common combinations of libraries.

JsePlatform

This class can be used as a factory for globals in a typical Java SE application. All standard libraries are included, as well as the luajava library. The default search path is the current directory, and the math operations include all those supported by Java SE.

JmePlatform

This class can be used to set up the basic environment for a Java ME application. The default search path is limited to the jar resources, and the math operations are limited to those supported by Java ME. All libraries are included except luajava, and the os, io, and math libraries are limited to those functions that can be supported on that platform.

4 - Libraries

Standard Libraries

Libraries are coded to closely match the behavior specified in See standard lua documentation for details on the library API's

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 

Optional Libraries

I/O Library

The Java SE platform contains the io library by default.

The Java ME platform has an optional, partial implementation of the io in

	src/jme/org/luaj/vm2/lib/jme/JmeIoLib.java

OS Library

A basic os library implementation for either Java ME or Java SE is provided ins
	src/core/org/luaj/lib/OsLib.java
A slightly more complete version for Java SE is in:
	src/jse/org/luaj/vm2/lib/jse/JseOsLib.java
Time is a represented as number of milliseconds since the epoch, and most time and date formatting, locales, and other features are not implemented.

Debug Library

The following library is optional:
	debug
Install from Java using:
	_G.load( new DebugLib() );
or change startup code to use:
	LuaValue _G = JsePlatform.debugGlobals();
To install from lua use:
	require 'org.luaj.vm2.lib.DebugLib'
The lua command line utility includes the debug library by default.

The Luajava Library

The Java SE platform includes the luajava library, which simplifies binding to Java classes and methods. It is patterned after the original luajava project.

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.

5 - Building and Testing

Building the jars

An ant file is included in the root directory which builds the libraries by default.

Other targets exist for creating distribution file an measuring code coverage of unit tests.

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.

6 - Downloads

Downloads and Project Pages

Downloads for version 1.0.3 are currently hosted on SourceForge. Downloads of built packages for 2.0 are not yet available. Sources are available via sourceforge.net
	SourceForge Luaj Project Page
	SourceForge Luaj Download Area

and LuaForge:

	LuaForge Luaj Project Page
	LuaForge Luaj Project Area

7 - Release Notes

Main Changes by Version

  2.0-alpha1
  • All basic bunit tests pass.
  • Initial port of all libraries has been done.
  • Compile-to-Java based on bcel 5.2.

Known Issues

  • module() and setfenv() don't work with compiled code
  • debug code may not be removed by obfuscators
  • not all luajava bugfixes have been ported over