Getting Started with Lua

From MorphOS Library

About

Lua is a powerful, fast, lightweight, embeddable scripting language, which is well integrated into MorphOS. It even supports ARexx port communication on MorphOS.


Usage

The Lua interpreter is implemented as a system shared library (lua.library), which can be used internally from other programs with the C API of Lua.

Lua can also be used as a stand-alone language by executing scripts with the LuaX command on MorphOS.


MorphOS Specific Features

Modules

Standard Lua libraries are implemented as shared modules and, unlike the standard Lua distribution, they have to be loaded separately with the require() function if needed. The only module available immediately is package.

The MOSSYS:Libs/Lua/ directory contains all available modules. These are included in MorphOS 3.10:

For example, a require("io") line is needed before you can use the io.write() fuction.


ARexx Communication

The ipc module contains the following functions to communicate with programs via their ARexx ports:

  • ipc.address(port) - Sets the default ARexx port name for sending commands. address("COMMAND") executes commands as DOS shell commands (although Lua's os.execute() does the same).
  • ipc.checkport(port) - Checks if an ARexx port is available.
  • ipc.rx(command) - Sends an ARexx command to a port set with ipc.address. ARexx's RESULT is returned as the result of the function. Lua variable rc is also set to the ARexx's RC result.
  • ipc.waitforport(name[, interval]) - Waits for a public port to appear. The script is aborted if the port isn't found after the specified interval of time (the default interval is 5 seconds).

Note: address() and rx() functions are registered in the global Lua namespace and should be called without the ipc. prefix.

Example:

-- Let's load the module we need:
require("ipc")

-- If OWB's port is found, let's open the official MorphOS page and cycle the fullscreen mode:
if ipc.checkport("OWB.1") then
   address("OWB.1")
   rx("OPEN NAME http://www.morphos-team.net/")
   rx("FULLSCREEN")
end

If the previous example is saved to a file called test.lua, it can be executed from the shell with the LuaX test.lua command.


Documentation

It's easy to find all kinds of documentation and solutions for your problems for Lua from the Internet, but here are some recommendations.

Official documentation:

Tutorials by Lua users:

MorphOS autodocs and examples:

  • Up-to-date autodocs are included in the MorphOS SDK
  • The outdated Lua archive contains some MorphOS specific documentation, but DO NOT INSTALL any binaries from it!


Debugging on MorphOS

  • Lua Explorador is a MUI based source level debugger for Lua programs.


Simple Example

A simple example with strings:

require("io")
require("string")

local firstvar="MorphOS"
local secondvar="Rulez"

io.write(firstvar)
io.write(" "..string.upper(secondvar).."!\n")

When ran from the shell:

Ram Disk:> luax example.lua
MorphOS RULEZ!
Ram Disk:>

Source Code Examples