lua-users home
lua-l archive

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


Jilani Khaldi wrote:
> Hi All,
Hi.

First, add a lua_[p]call to call_add_xy:

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.

  // Call the function at stack with 2
  // arguments, returning 1 value.
  lua_call(L, 2, 1);

  Result := lua_tonumber(L, -1);
  lua_pop(L, 1);
end;

Then, add another lua_[p]call to the click handler:

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 only loads the file as a chunk (function) and leave
 // it on stack's top. You have to manually call this chunk.
 luaL_loadfile(L, FName);
 lua_call(L, 0, 0);

 R := call_add_xy(L, 3.14159, 2.71828);
 st := Format('pi + e = %1.5f', [R]);
 memo1.Lines.Add(st);
 lua_close(L);
end;



--rb