[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: LuaJIT 2 ffi (casting / force hotpath)
- From: Mike Pall <mikelu-1101@...>
- Date: Mon, 24 Jan 2011 16:10:26 +0100
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