We’re crazy! We’re out of our minds! We’re going to run Lua code, from inside our Lazarus code! 2 Languages at once!
Lua is a programming language. Lazarus’ Free Pascal is a programming language too. But did you know we can use Lua from Free Pascal? Interesting, right? One language inside the other.
Lua was designed to be small, compact and user friendly. So embedding Lua inside another program is a no sweat task. We can have the taste of another language in our Free Pascal programs with a small footprint (~260kb on *nix, ~225kb on Windows).
This can be especially useful if you want to let the user input some code and run it on the fly. This can help you create your own small IDE that can read code from a project file of your chosen syntax and run them! Or run some code that’s only available on Lua and not FPC. Or anything else… sky is the limit.
Let’s start with a simple tutorial…
Simple Tutorial
Start Lazarus.
Create a new Program project (Project - New Project - Program - OK).
Now click File - Save All (because we’ll need to keep some dependent files inside our project folder).
Common tasks:
1. Download lua53.pas from here and keep it inside your project folder. If that file is not there anymore (due to future changes) get the file from this archive. This file will let us use the lua53 unit inside our uses clause and act like a bridge between Lua and Free Pascal.
2. Download Lua dynamic libraries. Go to http://luabinaries.sourceforge.net/ and scroll down to the History heading. Click on the latest 5.3.x release. For me it was https://sourceforge.net/projects/luabinaries/files/5.3.5/.
- If you are using Windows, click the Windows Libraries, then Dynamic. Check if your Lazarus is 32bit or 64bit. Then download the archive and Microsoft Visual C++ Redistributable for that architecture. If you scroll down a bit you will be able to see which file is for which redistributable. e.g. I had 2015 redistributable installed and 32bit Lazarus installed, so I downloaded lua-5.3.5_Win32_dll14_lib.zip, extracted it, then copied the lua53.dll into my project folder.
- If you are using Linux platform, click Linux Libraries, then download the one that is closest to your kernel version. (For me it was lua-5.3.5_Linux44_64_lib.tar.gz.) Then get the libluaXX.so file from the archive and put that into project folder.
- If you are using Mac OS, click Other Libraries (Note: but I have not tested this on Mac)
Download the archive appropriate for you and extract it somewhere. Get either lua53.dll for Windows or liblua53.so for other platforms from there. Put the file inside the project folder. This file has the stuff that will run our Lua code.
For Linux (and also I assume Mac) though, you’ll have to do something extra.
A. Open a terminal on the project folder, then run sudo cp liblua53.so /usr/lib/
B. Or, run the executable with LD_LIBRARY_PATH=. ./proj_example1
End of Common tasks. Now let’s get back to tutorial…
Copy-paste this on your pascal unit:
1 | program Project1; |
So after all these, my project folder looks like this:
Now we are ready to run our program. Hit Run - Run (or F9).
It should print the message from Lua as expected:
Troubleshooting
On Windows, if you get a boring “The Application was Unable to Start Correctly (0xc000007b)” message when you run your program from explorer or file manager, then maybe you should try dll file from 32bit architecture. 64bit dlls cause this issue sometimes. You can also use Dependency Walker to find out where the issue is. Just remember, the files will be for the target architecture you are building for.
Tutorial
Start Lazarus.
Create a new Application project (Project - New Project - Application - OK).
Now click File - Save All (because we’ll need to keep some dependent files inside our project folder).
Don’t forget to follow the things under “Common tasks“ heading above.
Place 2 TMemos and a TButton. Change the Lines property of Memo1 to something like:
1 | print ("I'm Lua, but I'm running inside".." Lazarus!!") |
This way we’ll have a simple code to execute when we run our program. We should also empty the Lines property of Memo2 to let our output fill it in later. Add TLabels as you wish.
Set the Caption property of Button1 to something like “Execute!“ or anything you like. Your form should now look something like this:
Double click the Button1 and enter:
1 | procedure TForm1.Button1Click(Sender: TObject); |
Now put these functions before the TForm1.Button1Click procedure that you just edited:
1 | function print_func(L : Plua_State) : Integer; cdecl; |
Note: If you put them before the procedure you won’t need forward declaration. If you want to put them anywhere you want (before or after) you can just copy and paste just the whole function line before the implementation line, something like this:
1 | ... |
Why do we use cdecl?
You will also notice there is a word “cdecl“ at the end of the function lines. These are used when the function is meant to communicate with a C library. In our case, we are communicating with Lua which is written in C. So we use this feature.
Now add lua53 under uses clause:
1 | uses |
Now click Run - Run (or F9). It will show up on screen.
Now click the Execute button and it should show a message on the Memo to the right.
You can paste any Lua code and it should work. I tried a code example from tutorialspoint.com (love their work by the way) which is something like this:
1 | -- Variable definition: |
Which should output like the screenshot below:
Have fun with this amazing trick!
Ref:
- Basics: http://lua-users.org/wiki/LuaInFreePascal
- Lots of examples: https://github.com/lainz/lainzcodestudio
- Lua4Lazarus bridge unit project: https://github.com/malcome/Lua4Lazarus
- Example Lua code: https://www.tutorialspoint.com/lua/lua_variables.htm
- Some more examples of Lua code usage: https://github.com/mvdhoning/dlua
- cdecl: https://freepascal.org/docs-html/ref/refsu71.html
Downloads
You can download the source code for the tutorial project and executable/EXE files from the links below: