Results 1 to 4 of 4

Thread: Combining Lua and Free Pascal.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Combining Lua and Free Pascal.

    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?

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

  2. #2
    Found the answer I needed on another site. Was hoping to get help here, but looks like i was wrong.

  3. #3
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,232
    Blog Entries
    2
    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.
    :: AthenaOfDelphi :: My Blog :: My Software ::

  4. #4
    Can you share with us the answer you found on the other site please ? it really might help a lot of us here.

    ____________________________________________
    VPN Sai Mannat AnyDesk
    Last edited by davido; 13-07-2019 at 05:58 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Comodo SSL