Re: Accessing FFI cdata from the Lua C API

  • From: Mike Pall <mike-1207@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sun, 15 Jul 2012 22:09:25 +0200

Florian Weimer wrote:
> I've got a C function which accepts Lua strings (using
> luaL_checklstring) and I want to provide a variant which accepts cdata
> (const char *, size_t) pairs.  I cannot call my functing using FFI
> because it allocates Lua objects.
> Right now, I'm using the code below, and it works as expected.  I know
> I need to cache compile function and the ctype object.  But I wonder
> if there is a completely different approach which is much simpler and
> does not involve calling back into Lua.

static void *luaL_checkcdata(lua_State *L, int narg)
  if (lua_type(L, narg) != 10)
    luaL_typerror(L, narg, "cdata");
  return lua_topointer(L, narg);

const char *buffer = *(const char **)luaL_checkcdata(L, 2);

Whether the size_t ends up as a cdata object depends on how it's
generated and whether it's a 32 bit or 64 bit type.


Other related posts: