lua-users home
lua-l archive

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


Yes, thank you very much. The last error was that the last position of
array should be filled with nil!
Thank you for the friendness. I am so happy with my first library!

library opr;
{$mode objfpc}{$H+}

uses
lua;

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

function luaopen_opr(l: plua_state):integer; cdecl;
var
r: array[0..0] of luaL_reg;
begin
r[0].name := 'sum';
r[0].func := @sum;
luaL_openlib(l, 'opr', @r, 0);
result := 1;
end;

exports
luaopen_opr;
END.



2012/11/6, luciano de souza <luchyanus@gmail.com>:
> Romulo, I think I am closer, but the error persists. I do two
> modifications. I fixed the syntax of lua_tonumber and I declared x and
> y as lua_number and not integer. The code compiles, but it crashhes.
> Thijs, I corrected the indexes. The "2" was lacking!
>
> library opr;
> {$calling cdecl}
> {$mode objfpc}{$H+}
>
> uses
> lua;
>
> function sum(l: plua_state):integer;
> var
> x, y: lua_number;
> begin
> x := lua_tonumber(l, 1);
> y := lua_tonumber(l, 1);
> 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.
>
>
>
> 2012/11/6, Thijs Schreijer <thijs@thijsschreijer.nl>:
>> 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
>>>
>>
>
>
> --
> Luciano de Souza
>


-- 
Luciano de Souza