PDA

View Full Version : Combining Lua and Free Pascal.



Dilan Rona
18-06-2019, 08:52 PM
I am sitting with two pascal files, one calling Lua from Pascal, and the other calling Pascal from Lua. I managed to get both working, but they are little more than proof of concept still. And I am having trouble getting multiple variables from a pascal function when calling it from Lua. What is the proper way to create a pascal library that i can use to call from Lua?


library libexample;

{$MODE OBJFPC}
{$H+}

uses
Classes,
crt,
lua53;

//{$R *.res}

var
L: plua_state;
{================================================= ===========}
function new_print(L: Plua_State): LongInt; cdecl;
{
Redefined print here as it causes problems when using freepascal with Lua.
Executing code from fpc, and using print in lua results in the cursor not
going back to the beginning.
}
var
vars:string;
i:integer;

begin
vars:='';

for i:=1 to lua_gettop(L) do begin
if i = 1 then vars:=vars + lua_tostring(L, i) else vars:=vars + ' ' + lua_tostring(L, i);
end;

writeln(vars);
new_print:=0;
end;
{================================================= ===========}
function example(L: plua_state):LongInt; cdecl;
var
a, b:string;

begin
a := lua_tostring(L, 1);
b := lua_tostring(L, 2);
writeln('I am executing from Free pascal now.');
writeln('received value: ', a, ' ', b);
lua_setglobal(L, 'example');
lua_pushinteger(L, 5); // Currently, only the last value "echo" is sent, instead of both.
lua_pushstring(L, 'echo');
example := 1;
end;
{================================================= ===========}
function luaopen_libexample(L: plua_state):LongInt; cdecl;
begin
lua_register(L, pchar('new_print'), @new_print);
lua_register(L, pchar('example'), @example);
luaopen_libexample :=0;
end;

exports
luaopen_libexample;
{================================================= ===========}
begin
writeln('Step 1. Loading the pascal module.');
writeln ('This portion is autoexecuted whenever the module is loaded.');
writeln;
end.


The above code is for a pascal library that can be called from lua, I had to redefine print as I noticed it does not work properly when calling print from lua, when having a pascal module loaded. Right now, I can send multiple variables from lua to pascal. But I also want to get multiple values from pascal to lua. And right now I can only get one.

Any help would be greatly appreciated.

Thanks in advance.

Dilan

Dilan Rona
23-06-2019, 10:24 PM
Found the answer I needed on another site. Was hoping to get help here, but looks like i was wrong.

AthenaOfDelphi
24-06-2019, 06:54 AM
Glad you found the answer. The unfortunate thing when you're doing things which aren't that common is that the pool of people who can possibly help is greatly reduced.

davido
05-07-2019, 12:00 PM
Can you share with us the answer you found on the other site please ? it really might help a lot of us here.

____________________________________________
VPN (https://downloader.vip/vpn/) Sai Mannat (https://flatwow.in/sai-mannat/) AnyDesk (https://anydesk.vip/)