VerySimple.Lua 2.0 – a cross-platform Lua 5.3.0 wrapper for Delphi XE5-10.1 Berlin
VerySimple.Lua is a Lua 5.3 binding for Delphi XE5- 10.1 Berlin which automatically creates OOP callback functions for Win32, Win64, Mac OS X, iOS and Android.
“Lua is a powerful, fast, lightweight, embeddable scripting language, it has been used in many industrial applications (e.g., Adobe’s Photoshop Lightroom), with an emphasis on embedded systems (e.g., the Ginga middleware for digital TV in Brazil) and games (e.g., World of Warcraft). Lua is currently the leading scripting language in games and it has a deserved reputation for performance. To claim to be “as fast as Lua” is an aspiration of other scripting languages.”
What’s new in v2.0?
- Lua 5.3.0 support (all header translations done from scratch)
- cross-platform – Windows 32 bit, Windows 64 bit, Mac OS X, iOS and Android
- improved autoregister function – with the help of TRtti it registers only valid methods
- simplified callback with upvalues instead of pointer list
- package support
- examples included (VCL and Firemonkey)
Example 1 – Simple usage
1 2 3 4 5 6 7 8 9 10 |
uses VerySimple.Lua; begin Lua := TVerySimpleLua.Create; Lua.DoFile('example1.lua'); Lua.Free; end; |
Example 2 – Extend Lua with own functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
uses VerySimple.Lua, VerySimple.Lua.Lib; type TMyLua = class(TVerySimpleLua) published function HelloWorld(LuaState: TLuaState): Integer; end; function TMyLua.HelloWorld: Integer; var ArgCount: Integer; I: integer; begin writeln('Delphi: Hello World'); ArgCount := Lua_GetTop(LuaState); writeln('Arguments: ', ArgCount); for I := 1 to ArgCount do writeln('Arg', I, ': ', Lua_ToInteger(LuaState, I)); // Push return values Lua_PushInteger(LuaState, 101); Lua_PushInteger(LuaState, 102); Result := 2; end; var MyLua: TMyLua; begin MyLua := TMyLua.Create; MyLua.DoFile('helloworld.lua'); MyLua.Free; end; |
helloworld.lua
1 2 3 4 5 6 7 |
print("LuaDelphi Test"); p1,p2 = HelloWorld(1,5,15) print "Results:"; print (p1); print (p2); |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
LuaDelphi Test Delphi: Hello World Arguments: 3 Arg1: 1 Arg2: 5 Arg3: 15 Results: 101 102 |
Dynamic library vs. static library
VerySimple.Lua uses the lua dynamic library if compiled for Windows, Mac OS X or Android and uses the static library if compiled for iOS (all libraries included).
Unit tests
<excuse type=”lame”>This binding is based on my now 6 year old Lua 5.1 delphi binding, and I didn’t had time for writing (updated) unit tests</excuse>.
Automatic get/setters for properties, etc.
As the name implies: it is just a very simple Lua <-> delphi binding. Feel free to add those functions by yourself.
67 Comments to VerySimple.Lua 2.0 – a cross-platform Lua 5.3.0 wrapper for Delphi XE5-10.1 Berlin
It would be great if, rather than telling us it is great and giving us sample code, to actually show us an example of how it is used. Can’t glean much from what has been posted and not interesting enough without a real world example to want to go and download and try it out. Just saying.
February 18, 2015
😉 download and try the included examples!
February 18, 2015
Any reason why only XE5-XE7 apart from “I did not test it on any other versions”?
February 19, 2015
[…] Just read a n article about Lua: […]
February 19, 2015
@stefan: I did not test it on any other versions. Should work with XE3 + XE4 as well, and with some modifications (MarshaledAString) on older versions as well
February 19, 2015
Thank you!
February 20, 2015
Hello! It looks great. I am so disappointed that it isn’t compatible with my Delphi version (XE2). I tried to make some modifications but there were too many to do and finally I gave up. I have to buy XE7. 🙂
February 20, 2015
It would be possible to rewrite the lib for usage with older Delphi versions (2010+), but as a professional developer I think the delphi maintenance is a great deal – with a fixed price/year you’re always up to date with the latest version – and why not use those new functions they offer? As this is not a commercial but an open source library, you’re free to fork and rewrite it for other versions – but I don’t want to spend my time for this task… 😉
February 20, 2015
[…] Head over and download the full source code for the Lua scripting interpreter for Delphi XE7 Firemon… […]
February 20, 2015
Keep the hardwork, Samples are very Clear, thanks for contribuiting to the Delphi Community, and thanks for the Effort 🙂
March 3, 2015
Any chance you could release it under a different license, compatible with commercial/proprietary applications? I would like to use it in a product that includes commercial third-party libraries that I purchased but I cannot distribute in source code. MPL would be great, or even a commercial license – I wouldn’t mind paying for it.
March 6, 2015
Sorry I didn’t realize the license is MIT. The comments in the code still mention GPL.
March 23, 2015
Hello, congratulations on your project, however it is happening one error when compiling.
There is the overloaded version of ‘RegisterFunction’ that can be called with arguments These
May 14, 2015
It is not work on Delphi XE5
Source:
uses …, VerySimple.Lua, VerySimple.Lua.Lib;
…
procedure TForm1.Test(Sender: TObject);
var
Lua: TVerySimpleLua;
begin
Lua := TVerySimpleLua.Create;
Lua.DoFile(‘example1.lua’);
Lua.Free;
end;
Error code:
[dcc32 Error] VerySimple.Lua.pas(395): E2250 There is no overloaded version of ‘RegisterFunction’ that can be called with these arguments
Error in line:
RegisterFunction(L, AObject.ClassType, AObject.ClassType.MethodAddress(Func), FuncName);
May 15, 2015
hmm, it runs with my XE5 Version without any error
Embarcadero® Delphi® XE5 Version 19.0.14356.6604
May 16, 2015
It is not work on Delphi XE5 Prosessional,me too.
May 16, 2015
ok, I’ll reinstall Delphi XE pro (+ latest update) and take a look
May 16, 2015
It fine works on Delphi XE7, XE8. I’m a little surprised by the size of the compiled file. “Hello World!” – Example1.exe – 6,174,208 bytes
May 16, 2015
Fixed XE5 and moved from GoogleCode to GitHub (needs some Git polish)!
May 17, 2015
Thanks , it now fine works on Delphi XE5 Pro.
May 18, 2015
@Flegont: About exe sizes, see second chart
May 27, 2015
My son pointed me to LUA in my quest to find a decent embedded language. I am looking forward to trying this wrapper.
By the way, your write-up on the curse almost brought tears to my eyes. Every programmer with enough gray hair and even a shred of humility can surely relate. I always tell our green programmers, “never assume a snippet of code or the tiniest of utilities that you produce will not escape into the wild.”
July 6, 2015
very good.
Can add android lua.so,dll file?
August 26, 2015
Great work!
But where to find the Lua.so file ? thanks.
August 26, 2015
Added the missing iOS/Android lib files (lost during google code transfer). You may easily create your own, see previous blog posts about “How to create LibLua for iOS/Android/Mac”.
September 28, 2015
Good job! Can you tell me how to access the delphi custom class object from Lua? thanks
October 23, 2015
How could I use TclientDataSet using this scripting ?
December 16, 2015
hello, when i try to register more then 32 functions (via RegisterFunctions(Self)) my program going to crash, wtf is it?
December 16, 2015
also catching “PANIC: unprotected error in call to Lua API (not enough memory)” when try to register some functions, and using 200-lines lua script. wtf ?
December 17, 2015
third question: is this component thread safe ? can i use TVerySimpleLua in this construction:
type
TMyThread = class(TThread)
private
FLua: TVerySimpleLua;
…
end;
…
constructor TMyThread.Create;
begin
FLua := TVerySimpleLua.Create;
FLua.DoFile(…); // executing one script file in every thread !!!
…
end;
——————–
will it conflict or not ?
thank you.
December 23, 2015
To reduce the size of the compiled file, remove unnecessary unite FMx.Dialogs on line = 173 in VerySimple.Lua (-4500KB).
December 24, 2015
Hey, Alex. Can you help me? Why i cant register more than 32 functions via RegisterFunctions(Self)?
January 19, 2016
it seems that the Lua stack isn’t properly expanded as needed (stuck at
20 by default), so you may just add this line at first in the
PushFunction in the VerySimple.Lua.pas (Line 427):
class procedure TVerySimpleLua.PushFunction(L: lua_State; Data, Code:
Pointer);
begin
if lua_checkstack(L, 3) 1 then // assure enough stack space
Exit;
// prepare Closure value (CallBack Object Pointer)
…
I will later update every stack push with stack space checking.
March 30, 2016
Works very nicely in general, but is there a way to pass and array as an argument? I have a need to rewrite code I used previously with luawrap.
March 30, 2016
Okay, worked out how to do it. Thanks for your hard work, very useful.
May 23, 2016
About an bug in old XE5, same thing on v19.0.13476.4176
re: [dcc32 Error] VerySimple.Lua.pas(395): E2250 There is no overloaded version of ‘RegisterFunction’ that can be called with these arguments
Error going exactly from function overloading, and can be fixed very easy.
Orig : RegisterFunction(L, AObject.ClassType, AObject.ClassType.MethodAddress(LMethod.Name), LMethod.Name);
Add @ sign before L to show complier, it’s realy pointer type.
Show complier real type of AObject.ClassType using TClass(…)
Fixed : RegisterFunction(@L, TClass(AObject.ClassType), AObject.ClassType.MethodAddress(LMethod.Name), LMethod.Name);
Works well…
June 22, 2016
Thanks for your hard work!
but is there a way to pass and TFileStream as an argument override DoFile method?
June 24, 2016
How to use decoda debugging delphi program?
June 24, 2016
Dear dennis, Could you please provide multiple lua file and using the “module” “require” function called example?
thanks!
September 1, 2016
I am attempting to use the very simple lua code but when I try sending a string parameter to lua and return it. it’s only getting the first letter of the string.
September 21, 2016
Fix for XE5 in VeriSimple.lua.pas
Original : RegisterFunction(L, AObject.ClassType, AObject.ClassType.MethodAddress(LMethod.Name), LMethod.Name);
Fixed : RegisterFunction(Pointer(L), (AObject.ClassType), AObject.ClassType.MethodAddress(LMethod.Name), LMethod.Name);
November 7, 2016
Sir, whoever you are, thank you so much for this! In my own application through the years I’ve develop a kind of custom script language with minimal functions and capacity but poeple are always asking for more and it becomes more difficult to improve my stuff since I’m not really good enough to re-write a kind of c-langage or something… But now, after having tried your examples, I am dreaming of integrated instead LUA script in my stuff. All the mechanic of functions, variables, with the loop, the if statement, etc. which is mainly the hard part, could be handle by the LUA interpretor and then, for the actual things related with our applications, then, LUA script would call my routines. Wow! I can’t wait to integrate that. Thank yo so much!
I though it was difficult to do that!
Denis.
December 27, 2016
OMG, Thank you, kind Sir!
You do fantastic job with this lib!
August 29, 2017
thank you very much, 32 functions problem resulved!! you seems to forget updating your github version?
November 28, 2017
VerySimple.Lua can add support for linux?
February 6, 2018
I found in a previous article, you have a Lua for Delphi 2010. I would like to use this but it uses Lua 5.1. Do you know if it would work to just change to Lua 5.3?
March 2, 2019
Klasse Website. Vielen Dank.
April 4, 2019
Dunno what I am doing wrong, but trying your libs within a 64bit delphi application (even the examples) does not work at my side.
What I did:
(1) changed the destination platform to 64-Bit
(2) created new mylua.dcu
(3) created new VerySimple.Lua.Lib.dcu
(4) created new VerySimple.Lua.dcu
ever since I tried I am getting
an
ELuaLibraryLoadError:
Failed to load Lua library “..\..\DLL\Win64\LUA5.3.0.DLL”
I am using Delphi XE5 so do you have a suggestions what I am doing wrong? (It works out perfectly when creating 32-Bit DCU’s and setting DLL and application to 32Bit).
Thanks in advance
June 25, 2019
I use your libs witin 32bit Delphi application.
Everything works great on 32/64 platforms. Win XP, 7, 8, 10.
But once, on Win 10 64 I got an error “Failed to load Lua library Lua5.3.0.dll”
Then I downloaded Lua5.3.5.dll in the Internet and renamed it to Lua5.3.0.dll and replaced the old one.
Everything began to work again.
What was the problem?
June 25, 2019
@Flegont: It does work with Lua 5.3.5 – see my new post on how to build it on windows or obtain the official (debug) DLLs (see link in same post). I can’t help you though why it stopped working with lua.5.3.0.dll – it still runs on Win10/64 with that version…
June 25, 2019
Thank you, your clarification is sufficient. Error Lua5.3.0.dll on Win 10 64bit was observed only on one user (and was easily fixed, as I said earlier). In all other cases known to me, Lua5.3.0.dll works correct with Win 10 64bit
June 27, 2019
Every 5.3.x-Version is just a bugfix version without adding/changing of features so they should work right away
July 11, 2019
I’ve been much struggeling with anonymous lua functions. I’ve found a rather slow and ugly way to call them from Delphi by adding a static function to the global scope. But that’s not very efficiant.
I was hoping for something like …
lua_Function lua_toluafunction (lua_State *L, int index);
void lua_pushluafunction (lua_State *L, lua_Function f);
Is anything like that possible?
June 13, 2020
Hi, could you explain me how to get it more results from LUA script?
I see example 6. In my case I need push some double values to lua script and get some double value as results. With one result it’s ok but I don’t understand how get more that one result.
June 13, 2020
@array81: just take a look at the example 6:
lua_call(L, 2, 1); // call function with 2 parameters and 1 result
if your function returns two results then
lua_call(L, 2, 2); // call function with 2 parameters and 2 results
now get the result from the stack with_
int ret1 = lua_tonumber(L, -2);
int ret2 = lua_tonumber(L, -1);
June 13, 2020
lua_pop(L, 2); // don’t forget to pop both values at the end!
June 21, 2020
Firstly thank you for these useful components. I have used this, together with verysimple.xml, to create an extensible spreadsheet where users can code their own functions and cell data is stored in xml nodes.
Now though I want to extend Lua to call a function in my GUI – to prompt for user input – and have spent many hours trying and failing to work out how to do this. Any help would be very gratefully received!
June 21, 2020
@John: you could make a package (see “example 5 create a package” with two ways creating a package) which holds your functions
October 27, 2021
If I use math.log10() in my lua function, I get an error. But, the cmd window closes immediately so I can’t find the reason. If I call the function directly from the lua file, it does give the correct answer.
Can you give me an idea how to fix this problem?
October 27, 2021
@Bruce: Please send me an example script, thanks
November 19, 2021
@Bruce: don’t use math.log10(x) anymore, it’s been replaced by math.log(x, base)
April 21, 2023
Hello,
I am testing VerySimpleLua with Delphi 10.2 / Windows 10.
Trying create text file and write into it “Hello World”, I get Erreur E/S 6.
Could you help me please.
Regards
Pierre
May 22, 2023
Hi,
As a total newbie in LUA, here is my problem:
I would like send an integer value from Delphi to a lua script variable called “TOTAL_TANK” then launch a lua function with no argument called “config_tanks()”.
How to do that please.
June 3, 2023
Why not calling a Lua function with the total_tank var, e.g. “config_tanks(TOTAL_TANK)” and then set the global var inside that lua function? You may take a look at example 6 on how to call lua functions and adjust the paramcount there.
April 3, 2024
Is there a way to catch (or verify programmatically) syntactic errors in the LUA script?
Currently in a console program I get “PANIC: unprotected error in call to Lua API (attempt to call a nil value)”.
With the same (erroneous) script a VCL application just crashes.
April 11, 2024
@Luca: Send me an example, I’ll take a look then
May 17, 2024
I solved my previous problem. Thank you.
Now I am facing a strange problem running my program on a panel PC with Windows 10 Enterprise 2016 LTSB.
Is there any dependency for the Lua DDLs in your installation package?
Leave a comment
About Dennis D. Spreen
Search
Recent Posts
- How to compile Lua 5.4.0 for Android as a dynamic library using Android Studio 4
- Please make inline vars usable for production – fix RSP-28892
- How to compile Lua 5.4.0 as a Mac OS X dynamic library
- How to compile Lua 5.4.0 for Linux as a shared library
- How to compile Lua 5.4.0 for Windows
- Daily Wage – a Spigot/Bukkit plugin that pays out a daily wage
- How to compile Lua 5.3.5 for Windows
- Better Collada exporter for Blender with custom properties
- MOS6502-delphi – a MOS 6502 CPU emulator for Delphi
- Pass a multidimensional array as a parameter (with a hidden caveat)
Categories
Tags
Archives
- May 2020
- March 2020
- June 2019
- March 2017
- August 2016
- July 2016
- June 2016
- January 2016
- September 2015
- February 2015
- January 2015
- October 2014
- September 2014
- August 2014
- May 2014
- March 2014
- February 2014
- November 2011
- June 2011
- February 2011
- March 2010
- September 2009
- August 2009
- July 2009
- May 2009
- March 2009
- February 2009
- January 2009
- November 2008
- October 2008
- February 2008
- June 2007
Delphi Feeds
- 2025 Focus & priorities December 30, 2024
- Vidéos rediffusées le 30 décembre 2024 en TV de rattrapage December 29, 2024
- Vidéos rediffusées le 29 décembre 2024 en TV de rattrapage December 28, 2024
- New CastleTransformManipulate unit to move/rotate/scale 3D objects in your applications, wireframe “shading” toggle for IndexedFaceSet, Steam achievements fixed with Delphi, slides from my presentation in Bielsko-Biała December 27, 2024
- Vidéos rediffusées le 28 décembre 2024 en TV de rattrapage December 27, 2024
- I Got Complacent—and Here’s What I Learned December 27, 2024
- Building a Simple yet Robust Job Queue System Using Postgres December 27, 2024
- Vidéos rediffusées le 27 décembre 2024 en TV de rattrapage December 26, 2024
- Merry Christmas Delphi Developers 2024 December 25, 2024
- Delphi and AI [3]: The meaning of Christmas December 25, 2024
February 18, 2015