Lua with Delphi (Was: Re: creating and evaluating functions -- Lua 4.0 vs Lua 5.0)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Lua with Delphi (Was: Re: creating and evaluating functions -- Lua 4.0 vs Lua 5.0)

marcus.cf
Here is a complete Delphi text-mode program that works.

Your problem was that you were using sizeof(st) instead 
of length(st).

Besides, I have some questions:
When a LUA_ERRMEM or a LUA_ERRERR occurs, does Lua push 
an error message on the stack?
Is it possible to access from Delphi lua_pushfstring and 
lua_pushvfstring? They use varargs...

I'm attaching the ".pas" files that I made. I took the 
liberty to put my name in them. I hope Lavergne Thomas 
doesn't mind... :)
Please send bug reports to me.
I plan to send the files to Thomas soon.

program TestLua;
{$APPTYPE CONSOLE}
uses
  SysUtils,
  lauxlib in 'lauxlib.pas',
  lua in 'lua.pas',
  lualib in 'lualib.pas';

var
  L: Plua_State;
  st: string;
  d: double;

begin
  st := 'print("Hello Lua!")';
  L := lua_open();
  luaopen_base(L);
  case luaL_loadbuffer(L, PChar(st), length(st), 'Test 
buffer') of
    LUA_ERRSYNTAX:
    begin
      { This error will happen if you pass sizeof(st) 
instead of length(st),
      because sizeof(st) is 4 (the size of the pointer. }
      writeln('Syntax error loading buffer. Message:');
      writeln(lua_tostring(L, 1));
    end;
    LUA_ERRMEM:
    begin
      writeln('Memory error loading buffer. Message:');
      // Do memory errors return messages???????
      writeln(lua_tostring(L, 1));
    end;
    else // Success loading. Try to run the code:
      case lua_pcall(L, 0, 0, 0) of
        LUA_ERRRUN:
        begin
          { This error will occur if you forget 
luaopen_base(L).
          Without it, print is nil. }
          writeln('Error executing buffer. Message:');
          writeln(lua_tostring(L, 1));
        end;
        LUA_ERRMEM:
        begin
          writeln('Memory error loading buffer. 
Message:');
          // Do memory errors return messages???????
          writeln(lua_tostring(L, 1));
        end;
        LUA_ERRERR:
        begin
          // Shouldn't happen, since the pcall error 
handler was set to 0.
          writeln('Error running error handler. 
Message:');
          // Do errors in error handlers return 
messages???????
          writeln(lua_tostring(L, 1));
        end;
      end;
  end;
  lua_close(L);
  readln; // wait for Enter
end.

 
__________________________________________________________________________
Seleção de Softwares UOL.
10 softwares escolhidos pelo UOL para você e sua família.
http://www.uol.com.br/selecao



Reply | Threaded
Open this post in threaded view
|

Re: Lua with Delphi (Was: Re: creating and evaluating functions -- Lua 4.0 vs Lua 5.0)

Jilani Khaldi-3
program TestLua;
{$APPTYPE CONSOLE}
uses
SysUtils,
lauxlib in 'lauxlib.pas',
lua in 'lua.pas',
lualib in 'lualib.pas';

var
L: Plua_State;
st: string;
d: double;

begin
st := 'print("Hello Lua!")';
L := lua_open();
luaopen_base(L);

[Error] testlua.dpr(17): Undeclared identifier: 'luaopen_base'

I find "luaopen_base" in lualib.h and not in lualib.pas; so, lualib.pas should be updated.

jilani