VerySimple.Lua 2.0 – a cross-platform Lua 5.3.0 wrapper for Delphi XE5-10.1 Berlin

Wednesday, February 18th, 2015 | Dennis D. Spreen | Delphi Programming, Delphi XE7, iOS, Lua, Mac OS X

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.”

Download current version at GitHub

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

Example 2 – Extend Lua with own functions

helloworld.lua

Output:

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.

Tags: , ,

67 Comments to VerySimple.Lua 2.0 – a cross-platform Lua 5.3.0 wrapper for Delphi XE5-10.1 Berlin

Me
February 18, 2015

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.

Dennis
February 18, 2015

😉 download and try the included examples!

Stefan Glienke
February 18, 2015

Any reason why only XE5-XE7 apart from “I did not test it on any other versions”?

[…] Just read a n article about Lua: […]

Dennis
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

Shane Holmes
February 19, 2015

Thank you!

Roland Chastain
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. 🙂

Dennis
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… 😉

[…] Head over and download the full source code for the Lua scripting interpreter for Delphi XE7 Firemon… […]

Batusai777
February 20, 2015

Keep the hardwork, Samples are very Clear, thanks for contribuiting to the Delphi Community, and thanks for the Effort 🙂

Bogdan
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.

Bogdan
March 6, 2015

Sorry I didn’t realize the license is MIT. The comments in the code still mention GPL.

Maicon Wagner
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

Flegont
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);

Dennis
May 15, 2015

hmm, it runs with my XE5 Version without any error
Embarcadero® Delphi® XE5 Version 19.0.14356.6604

Sudan
May 16, 2015

It is not work on Delphi XE5 Prosessional,me too.

Dennis
May 16, 2015

ok, I’ll reinstall Delphi XE pro (+ latest update) and take a look

Flegont
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

Dennis
May 16, 2015

Fixed XE5 and moved from GoogleCode to GitHub (needs some Git polish)!

Sudan
May 17, 2015

Thanks , it now fine works on Delphi XE5 Pro.

Chris
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.”

david
July 6, 2015

very good.
Can add android lua.so,dll file?

Tony
August 26, 2015

Great work!
But where to find the Lua.so file ? thanks.

dennis
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”.

Tony
September 28, 2015

Good job! Can you tell me how to access the delphi custom class object from Lua? thanks

Carlos
October 23, 2015

How could I use TclientDataSet using this scripting ?

maxwell
December 16, 2015

hello, when i try to register more then 32 functions (via RegisterFunctions(Self)) my program going to crash, wtf is it?

maxwell
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 ?

maxwell
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.

Alex
December 23, 2015

To reduce the size of the compiled file, remove unnecessary unite FMx.Dialogs on line = 173 in VerySimple.Lua (-4500KB).

maxwell
December 24, 2015

Hey, Alex. Can you help me? Why i cant register more than 32 functions via RegisterFunctions(Self)?

dennis
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.

John Whitham
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.

John Whitham
March 30, 2016

Okay, worked out how to do it. Thanks for your hard work, very useful.

Cugreev Eugene
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…

cjk
June 22, 2016

Thanks for your hard work!

but is there a way to pass and TFileStream as an argument override DoFile method?

cjk
June 24, 2016

How to use decoda debugging delphi program?

cjk
June 24, 2016

Dear dennis, Could you please provide multiple lua file and using the “module” “require” function called example?

thanks!

Mark K
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.

Paolo
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);

Denis Bisson
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.

Constantine
December 27, 2016

OMG, Thank you, kind Sir!
You do fantastic job with this lib!

wiman4
August 29, 2017

thank you very much, 32 functions problem resulved!! you seems to forget updating your github version?

Zhaoqm
November 28, 2017

VerySimple.Lua can add support for linux?

Bruce
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?

bonrolle hersteller
March 2, 2019

Klasse Website. Vielen Dank.

Lars
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

Flegont
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?

Dennis D. Spreen
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…

Flegont
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

Dennis D. Spreen
June 27, 2019

Every 5.3.x-Version is just a bugfix version without adding/changing of features so they should work right away

Johan Avén
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?

array81
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.

Dennis
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);

see https://www.lua.org/pil/25.2.html

Dennis
June 13, 2020

lua_pop(L, 2); // don’t forget to pop both values at the end!

John Whitham
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!

Dennis
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

Bruce
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?

Dennis
October 27, 2021

@Bruce: Please send me an example script, thanks

Dennis
November 19, 2021

@Bruce: don’t use math.log10(x) anymore, it’s been replaced by math.log(x, base)

Pierre Chassang
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

Pierre Chassang
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.

Dennis
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.

Luca Pretti
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.

Dennis
April 11, 2024

@Luca: Send me an example, I’ll take a look then

Luca Pretti
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

I'm an avid programmer working on a variety of platforms in a variety of languages with a wide technical interest.

Search

QR Code

Categories