lua-users home
lua-l archive

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


Alex Bradbury wrote:
> Just to check I haven't missed anything - am I correct to think that
> if I create a uint8_t by e.g. ui8 = ffi.new("uint8_t") then there's no
> way of assigning to it directly from Lua. Instead I want to do ui8 =
> ffi.new("uint8_t[1]") and then do ui8[0] = whatever?

Scalar cdata is immutable after creation. You'll rarely need them,
except to force a specific type for vararg arguments.

Either use ffi.new("uint8_t", x) for one-time initialization or
use a one-element array, if you need a mutable type.

The latter is also the preferred way to convert C code which uses
'&' to simulate multiple return values:

C code:
  int buflen = n;
  int status = fillbuf(buf, &buflen);
  if (status == 0) return -1;
  return buflen;

LuaJIT FFI code:
  local buflen = ffi.new("int[1]", n)
  local status = fillbuf(buf, buflen)
  if status == 0 then return -1 end
  return buflen[0]

--Mike