Lua

From FxCodeBaseWiki

Jump to: navigation, search

Contents

Lua

What is Lua?

Lua is an extremely fast, simple and powerful scripting language designed, implemented, and maintained at PUC-Rio in Brazil. The strengths of Lua have been well-proven by its usage in many time-critical applications, including real-time strategies such as World of Warcraft. Nowadays, Lua is in top 20 of the most popular programming languages in the world.

Lua official website: http://www.lua.org

What does Lua Look Like?

Syntax

In terms of syntax, Lua is close to the Pascal family of programming languages (Pascal, ADA, Delphi). There is no syntax sugars, such as prefix/postfix operators or assignments as part of expressions, as in C++.

Compare the factorial function implemented in C++, Pascal, and Lua.

C++:

 int fact(int value)
 {
     if (value == 0)
        return 1;
     else
        return fact(value - 1) * value;
 }

Pascal:

 function fact(n: integer): longint;
 begin
     if (n = 0) then
         fact := 1
     else
         fact := n * fact(n - 1);
 end;

Lua:

 function fact(n)
    if n == 0 then
       return 1;
    else
       return fact(n - 1) * n;
    end
 end

Concepts

In terms of programming concepts, the language closest to Lua is JavaScript:

  • Lua has no types. A variable can take any value.
  • Lua does not require explicit variable declaration. If the value has not be assigned before, it is just a nil value.
  • The interface of Lua classes (known as tables) is dynamically created simply by assigning the reference to the function.

Tables

The concept of tables is the most complicated concept for Lua newbies. Tables are everything in Lua - they are classes, hash tables (dictionaries), and even arrays.

Table as a hash table.

You can store and then retrieve a value inside the table using any key:

 -- create a table
 a = {};
 -- put value 123 into the table with key "onetwothree"
 a["onetwothree"] = 123;
 -- retrieve the value by the key
 print(a["onetwothree"]);

If the key is a string, you can use the dot operator (.) to assign or retrieve the values. The example below does exactly the same as the previous example:

 -- create a table
 a = {};
 -- put value 123 into the table with key "onetwothree"
 a.onetwothree = 123;
 -- retrieve the value by the key
 print(a.onetwothree);

You can mix the syntax as well. For example, this code is also correct and does the same:

 -- create a table
 a = {};
 -- put value 123 into the table with key "onetwothree"
 a.onetwothree = 123;
 -- retrieve the value by the key
 print(a["onetwothree"]);

Table as an array.

If you use an integer number as a key and values are assigned starting from key 1, sequentially, without gaps in the order, Lua (and LuaJIT) optimizer recognizes that part of the values assigned as an array. An alternative (and really faster) method of access is used in that case:

 local array, i;
 -- create a table
 array = {};
 -- fill the table
 for i = 1, 10, 1 do 
    array[i] = i * 20;
 end
 -- now keys 1 to 10 are stored as an array
 -- but we are still able to add additional properties to the dictionary part of the table
 array.first_item = 1;
 array.last_item = 10;
 -- this (access to the array part)...
 b = a[5];
 -- ...is faster than this (access to the dictionary part)
 c = a.first_item;

Table as a class.

A reference to a function can be also stored into the table.

 -- create a table
 math = {};
 -- create an inline function and store the reference into the table 
 math.abs = function (x) 
              if x < 0 then
                 return -x;
              else
                 return x;
              end;
            end
 ...
 b = math.abs(-10);

Looks exactly like a static method of the class, doesn't it? Now, what if we want to create instance methods? The instance methods must receive a reference to the table, mustn't they? Let's see.

 -- create a table
 mytable = {};
 -- create a property
 mytable.x = 0;
 -- create a method
 mytable.inc = function (_this) 
                   _this.x = _this.x + 1;
               end

Now we can use it as:

  mytable.inc(mytable);

But other languages (except plain C) do not require passing this reference explicitly! Why Lua does? Well, for such case Lua has a "syntax sugar", the colon operator (:), so the above call can be written as:

  mytable:inc();

Where is Documentation?

  • Standard Lua documentation is included into both online and offline versions of the Indicore SDK documentation.
  • You can always refer to the Lua official website: http://www.lua.org.

Which Version of Lua is Used?

Indicore uses Lua 5.1.5 specification and LuaJIT 2.0 implementation of the virtual machine.

Is Indicore Compatible with 3rd-Party Lua Extensions?

Yes. However, as stated above, we use LuaJIT 2.0 instead of the original Lua virtual machine, which is literally API/ABI compatible with the original Lua VM implementation except an ability to save or load Lua byte-code. LuaJIT 2.0 has its own compiler which is not compatible with the original Lua VM.

So, just check whether the extension is compatible with LuaJIT.

This Article in Other Languages

Language: English  • Español • Français • Русский • 中文 • ‪中文(繁體)‬
Personal tools