Difference between revisions of "Bien débuter avec le langage script Lua"

From MorphOS Library

(ARexx Communication)
(Modules)
Line 10: Line 10:
 
== Caractéristiques spécifiques MorphOS ==
 
== Caractéristiques spécifiques MorphOS ==
 
=== Modules ===
 
=== 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.
+
Les librairies standard Lua sont mis en œuvre sous forme de modules partagés et, contrairement à la distribution standard de Lua, ils doivent être chargés séparément avec la fonction '''require ()''' si nécessaire.
 +
 
 
The only module available immediately is ''package''.
 
The only module available immediately is ''package''.
  
The ''MOSSYS:Libs/Lua/'' directory contains all available modules. These are included in MorphOS 3.9:
+
Le répertoire ''MOSSYS:Libs/Lua/'' contient tous les modules disponibles. Ils sont inclus dans MorphOS 3.9:
 
* [http://www.lua.org/manual/5.1/manual.html#5.1 base]
 
* [http://www.lua.org/manual/5.1/manual.html#5.1 base]
 
* [http://www.lua.org/manual/5.1/manual.html#5.2 coroutine]
 
* [http://www.lua.org/manual/5.1/manual.html#5.2 coroutine]
Line 26: Line 27:
 
* [http://www.lua.org/manual/5.1/manual.html#5.5 table]
 
* [http://www.lua.org/manual/5.1/manual.html#5.5 table]
  
For example, a '''require("io")''' line is needed before you can use the ''io.write()'' fuction.
+
Par exemple, une ligne '''require("io")''' est nécessaire avant d'utiliser la fonction ''io.write()''.
 
 
  
 
=== Communication ARexx ===
 
=== Communication ARexx ===

Revision as of 22:14, 14 February 2016

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.

Caractéristiques spécifiques MorphOS

Modules

Les librairies standard Lua sont mis en œuvre sous forme de modules partagés et, contrairement à la distribution standard de Lua, ils doivent être chargés séparément avec la fonction require () si nécessaire.

The only module available immediately is package.

Le répertoire MOSSYS:Libs/Lua/ contient tous les modules disponibles. Ils sont inclus dans MorphOS 3.9:

Par exemple, une ligne require("io") est nécessaire avant d'utiliser la fonction io.write().

Communication ARexx

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