lua-users home
lua-l archive

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


Try this;

function sum(l: plua_state):integer;
var
x, y: integer;
begin
x:= lua_tonumber(l, 1);
y:= lua_tonumber(l, 2);
lua_pushnumber(l, x+y);
result := 1;
end;

Might not be completely correct, bit rusty on my Pascal after two decades. :)

Thijs

Sent from my espressomachine

luciano de souza <luchyanus@gmail.com> wrote:

>Hello listers,
>I am trying to understand the basic mechanism of Lua stack
>registrations. This Pascal code compiled, but I have a run-time error.
>As it's an generic error, I could not identify what's wrong.
>
>library opr;
>{$calling cdecl}
>{$mode objfpc}{$H+}
>
>uses
>lua;
>
>function sum(l: plua_state):integer;
>var
>x, y: integer;
>begin
>lua_tonumber(l, x);
>lua_tonumber(l, y);
>lua_pushnumber(l, x+y);
>result := 1;
>end;
>
>function luaopen_opr(l: plua_state):integer;
>var
>r: array[0..1] of luaL_reg;
>begin
>l := lua_open;
>luaL_OpenLibs(l);
>r[0].name := 'sum';
>r[0].func := @sum;
>luaL_openlib(l, 'opr', @r, 0);
>lua_close(l);
>result := 1;
>end;
>
>exports
>luaopen_opr;
>end.
>
>In the Lua code, I would like to do it:
>
>require('opr')
>
>print(opr.sum(3, 5))
>
>I follow C API and I believe the fact it was writen in Pascal won't
>create difficulties for the discutions.
>These are my first steps in C API. Do they seem to be wrong? Does
>someone have any tip for me?
>Regards,
>Luciano
>