Bien débuter avec le langage script Lua

From MorphOS Library

Revision as of 22:03, 14 February 2016 by Papiosaur (talk | contribs) (Usage)

A propos de Lua

Lua est un langage script puissant, rapide et léger intégré dans MorphOS. Il supporte également les ports de communication ARexx sur MorphOS.

Usage

L'interpréteur Lua est implémenté comme une library partagée (lua.library), qui peut être utilisée en interne avec l'C API de Lua.

Lua peut également être utilisé comme un langage autonome qui peut exécuter des scripts avec la commande LuaX de 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.9:

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:

  • 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