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: , ,

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

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