lua-users home
lua-l archive

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

Hello all,

I'm attempting to access Lua through Pascal. I've compiled a DLL in VC6 and
used a '__declspec(dllexport)' to export functions. I assume this uses the
stdcall calling convention.

I can call the basic functions like lua_open, lua_dofile. My problem occurs
when I try to register a Pascal function. Passing my pascal function to
lua_pushcclosure() causes it to get called. Switching into assembler view
shows that the right before RET is called (at the end of lua_pushcfunction),
the address on top of the stack is the address to my pascal function. This
explains why it gets called, but not why it on top of the stack. It seems as
if there was an extra POP somewhere. This made me suspect I may have a
problem with my calling convention. I'm confused and I don't know much C.
Maybe I am doing something stupid here. Maybe someone can help.

I have included the relevant code below:

luadll = 'lua.dll';

lua_cfunction = procedure; stdcall;
procedure lua_pushcfunction(f: lua_cfunction);
procedure lua_pushcclosure(f: lua_cfunction; n: int); stdcall; external
procedure lua_setglobal(name: PChar); stdcall; external luadll;

procedure lua_register(n: PChar; f: lua_cfunction);

procedure lua_pushcfunction(f: lua_cfunction);
  lua_pushcclosure(f, 0);
end;   {  <--- before RET occurs, top of stack has f() address instead of
calling address  }