lua-users home
lua-l archive

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


Hi All,
well, this is the next step:
having  the lua function:
function sum_tab(tab)
   local sum
   sum = 0
   for i = 1,table.getn(tab) do
         sum = sum + tab[i]
   end
   return sum
end

and the delphi procedure:
procedure set_mytable(L: Plua_State; a: array of double); cdecl;
which calls "sum_tab(tab)" lua function, passing to it the array of doubles.
Now my question is how to implement this and a delphi function to read the result (sum) calculated by the lua function.

I have tried writing (translating) this code from C, but I failed :-(
procedure set_mytable(L: lua_State; a: array of double); cdecl;
var
 i, count: integer;
begin
 lua_newtable(L);
 count := 0;
 for i := 0 to high(a) do
 begin
   inc(count);
   lua_pushnumber(L, i);   //* Push the table index */
   lua_pushnumber(L, i*2); //* Push the cell value */
   lua_rawset(L, -3);      //* Stores the pair in the table */
 end;
 // "n" and contains the total number of elements in the table.
 lua_pushliteral(L, 'n');        //* Pushes the literal */
 lua_pushnumber(L, count-1);       //* Pushes the total number of cells */
 lua_rawset(L, -3);              //* Stores the pair in the table */
 // By which name is the script going to reference our table ? */
 lua_setglobal(L, 'tab');
 lua_pcall(L, 0, LUA_MULTRET, 0);
 lua_pop(L, 1);
end;

Any hint is welcome.
Thank you.

jk