lua-users home
lua-l archive

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


> -----Original Message-----
> From: owner-lua-l@tecgraf.puc-rio.br
> [mailto:owner-lua-l@tecgraf.puc-rio.br]On Behalf Of Jens Wessling
> Sent: Wednesday, August 29, 2001 15:21
> To: Multiple recipients of list
> Subject: Re: The Lua C API

> Lastly, the unified function handler.  This is the meat of the interface.
> The first thing you need to do, is to register every C++ function that you
> want to access from Lua to the same function, then, when the function is
> called, you need to get the name of the function.  I am pretty sure I did
> this the wrong way, by hacking some code in to Lua.  I have heard
> that this
> information is available through the debugging interface.  If anyone could
> tell me how, I would appreciate it.  Then, I look the function up in my
> function database, validate all of the parameters and the return
> value, hand
> load the call stack, and jump to the function in memory.
>
> This is probably a lot more than you wanted.  Hopefully there is
> some useful
> in information here though.

Hi,

working on a similar way of binding scripting langauges i'm
also using Lua (well have to support different langauges on
different plattforms - anybody out there who implemented
such "direct" call on other plattforms, i.e. Mac or some
of the current consoles?) and i think it's better to use
upvalues assigend to the exported cclosures.

Lets say you have some struct describing an exported
function

struct SFuncInfo
{
  void*       m_pFuncAddress;
  const char* m_pFuncName;
  ...
}



// prototyp of the function called for each exported function
int ExecuteLuaFunction( lua_State* _lua );


You could handle the export like this:

void ExportFunction( SFuncInfo* _funcinfo, lua_State* _lua )
{
  lua_pushusertag( _lua, _funcinfo, LUA_ANYTAG );
  lua_pushcclosure( _lua, ExecuteLuaFunction, 1 );
  lua_setglobal( lua, _funcinfo->m_pFuncName );
}


Now if such a function gets called you get the address of your
SFuncInfo struct for free without additional lookups, or even
using the debugging interface.
The upvalues are puhsed on the stack after the regular function
paramters.

int ExecuteLuaFunction( lua_State* _lua )
{
  // get the funinfo
  SFuncInfo* funcinfo = lua_touserdata( _lua, -1 );

  // now ... read the other paramters and use them
  // for the functioncall
}


Regards,
Michael