lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi All,
I am using "luapas.pas" unit to interface Delphi with Lua, and this is my Delphi(7) code:

unit main;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls;

type
 TForm1 = class(TForm)
   Button1: TButton;
   Memo1: TMemo;
   procedure Button1Click(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;

var
 Form1: TForm1;

implementation
uses
 luapas;
{$R *.dfm}

// Call add_xy() from Delphi:
function call_add_xy(L: Plua_State; x, y: Double): Double;
begin
  // Push functions and arguments:
  lua_getglobal(L, 'add_xy');  // Function to be called.
  lua_pushnumber(L ,x);        // Push 1st argument.
  lua_pushnumber(L, y);        // push 2nd argument.
  Result := lua_tonumber(L, -1);
  // pop returned value:
  lua_pop(L, 1);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 L: Plua_State;
 FName: PChar;
 R: double;
 st: string;
begin
 FName := 'exp1.lua';
 L := lua_open();
 luaopen_base(L);
 luaopen_io(L);
 luaopen_string(L);
 luaopen_math(L);
 luaL_loadfile(L, FName);
 R := call_add_xy(L, 3.14159, 2.71828);
 st := Format('pi + e = %1.5f', [R]);
 memo1.Lines.Add(st);
 lua_close(L);
end;

end.

and this is my lua code contained in 'exp1.lua':
function add_xy(x, y)
 return (x + y)
end

But I get as result 0.0000

What am I doing wrong?
Thank you.

jk