Tutorial; Creating a Lua 4 sandbox environment - HWRM/KarosGraveyard GitHub Wiki

Lua 4 Sandbox

It is possible to install and compile the Lua 4.0.1 binary on your system.

This means we can write Lua 4 scripts and test them using Lua 4 directly, without needing to run them in Homeworld! This can save a LOT of time debugging your scripts!

Prerequesites

I have no idea how to compile Lua correctly on Windows/Mac! As such, you will need some type of linux environment to compile in.

For Windows 10+ users, you can make use of WSL.

Other viable alternatives are emulation, or dual booting.

Compilation natively on other platforms is possible; I just haven't figured it out (see INSTALL manual file).

Downloading Lua 4

Lua 4.0.1 Download

You can find all versions of Lua here.

Extract & Compile

Make sure to extract the contents into your linux environment (if emulating or using WSL).

Then, in a command line, change directory into the extracted directory, and run make.

* Installation
  ------------
  Building Lua on a Unix system should be very easy:

	1. Edit "config" to suit your platform, if at all necessary.
	2. Do "make".
	3. If you want to install Lua in an "official" place in your system,
	   then do "make install". The official place and the way to install
	   files are defined in "config". You may have to be root to do this.

This will generate two binaries called lua and luac, under bin (in the extracted directory). If you ran make install, the binaries will be located according to config:

# Locations for "make install". You may need to be root do "make install".
INSTALL_ROOT= /usr/local
INSTALL_BIN= $(INSTALL_ROOT)/bin
INSTALL_INC= $(INSTALL_ROOT)/include
INSTALL_LIB= $(INSTALL_ROOT)/lib
INSTALL_MAN= $(INSTALL_ROOT)/man/man1

So /usr/local/bin by default.

Aliasing the binary for use (optional)

💡 I would strongly recommend adding the location of the lua binary to your command line environment, such as aliasing it in a config file or adding it to PATH for Windows, etc.

For example, I have an alias in my .zshconfig file (which is syntaxically almost identical to a .bashrc file or etc.):

alias lua4="~/lua-4.0.1/bin/lua"

My lua is compiled at the location /lua-4.0.1/bin/lua, and I just define an alias for this so I can use it easily:

lua4 my-script.lua

Writing & testing scripts

Lua is run via the command line like so:

<path to Lua binary> <path to script>
/usr/local/bin/lua my-script.lua

This is made MUCH easier if you have some type of alias for invoking the lua binary (see the above section):

lua4 my-script.lua

Example

-- my-script.lua

local x = 10;
local xPlus = function (y)
  return %x + y;
end

print("x is " .. x);
print("x plus 2: " .. xPlus(2));
print("x plus -4: " .. xPlus(-4);

Run it (using an alias):

lua4 my-script.lua

Output:

x is 10
x plus 2: 12
x plus -4: 6