Results 1 to 5 of 5

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,245
    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.

  5. #5
    Sorry for the late reply. I ended giving up on the project due to the lack of support overall, and because I simply couldnt do with freepascal at the time I could do with Borland Pascal. Main reason being the lack of a crt unit after a fresh install of Free Pascal.

    This is the test pascal file that is working as of today, reorganized the register functions to above the main.lua file being loaded.

    Code:
    program lua_test;
    
    uses
            crt,
            lua,
            lualib,
            lauxlib;
    
    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 cls(L: Plua_State): LongInt; cdecl;
    begin
            clrscr();
            cls:=0;
    end;
    {--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--}
    procedure SendtoLua(); cdecl;
    begin
            writeln('===================');
            lua_getglobal(L, 'luatest');
            lua_pushinteger(L, 5);
            lua_pushstring(L, 'r2wea');
            lua_pushboolean(L, true);
            {   call:   x args, y result }
            lua_call(L, 3     , 4        );
            writeln ('Pascal received: ', lua_tointeger(L, -4),', ', lua_tostring(L, -3),', ', lua_tointeger(L, -2), ', ', lua_toboolean(L, -1));
            writeln('===================');
    end;
    {--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--=-==-=--}
    begin
            clrscr;
            L := luaL_newstate();
    
            {check if the lua engine is loaded.}
            if (L = nil) then begin
                    writeln('Lua engine not loaded! Terminating!');
                    exit;
            end else begin
                    writeln('Step 1: Loading Lua engine.');
                    luaL_openlibs(L);
    
                    {Register freepascal functions and procedures before loading the lua file.}
                    lua_register(L, pchar('cls'), @cls);
                    lua_register(L, pchar('new_print'), @new_print);
                    if lua_dofile(L, pChar('main.lua')) <> 0 then begin
                            Write('Error Detected: ');
                            WriteLn(lua_tostring(L, -1));
                            exit;
                    end;
            end;
    
            {Check if the main.lua file exists, and executes it.}
            writeln('Step 3: Loaded, and Executed the lua script.');
    {=======================================================================================================================
    Main code for execution goes here.
    =======================================================================================================================}
            SendtoLua();
    
            lua_close(L);
    end.

    The main.lua file is here.
    Code:
    --oprint = print -- Saving original print
    --print = new_print -- Redefinging print
    asd={}
    asd.d=1
    asd.e=3
    
    function luatest(aaa,bbb,ccc)
        print('calling lua test')
        print('Lua received: ' .. aaa .. ', ' .. bbb .. ', ' .. type(ccc) .. '.\n')
    
        if ccc == true then
            print('this is true')
        else
            print('this is false')
        end
    
        print('Alpha.')
        print("Beta.")
        return 99,"a77",43, asd
    end
    
    --cls()
    new_print('Test line.')
    print('Step 2: Loaded main.lua. ')
    print('Testing.')


    Its not the cleanest project, i'll admit. As it was meant as a proof of concept for me. Since freepascal does ship with Lua 5.1 (which i do need above the latest versions), this is what I'll be using myself. I'll do some cleanup, see if i can get the project to do what i need it to do, and see what direction i can take it in terms of a possible console game.
    Last edited by Dilan Rona; 21-03-2023 at 09:43 AM. Reason: Cleaning up the post and placing code inside proper tags

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
  •