lua-users home
lua-l archive

The first Lua library

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


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

AltStyle によって変換されたページ (->オリジナル) /