lua-users home
lua-l archive

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


> > > What do I use instead of lua_cpcall()?
> > Lua 5.2 will have luaL_cpcall, which is used like this:
> > 
> >   /* call 'pmain' in protected mode */
> >   lua_pushinteger(L, argc);  /* 1st argument */
> >   lua_pushlightuserdata(L, argv); /* 2nd argument */
> >   status = luaL_cpcall(L, &pmain, 2, 1);
> > 
> > This makes protected C calls even easier to make.
> 
> This does indeed look very handy.  Thank you for this.

Here is the code. Of course, it uses LUA_RIDX_CPCALL.

LUALIB_API int luaL_cpcall (lua_State *L, lua_CFunction f, int nargs,
                            int nresults) {
  nargs++;  /* to include function itself */
  lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_CPCALL);
  lua_insert(L, -nargs);
  lua_pushlightuserdata(L, &f);
  lua_insert(L, -nargs);
  return lua_pcall(L, nargs, nresults, 0);
}