Re: ffi type of pointer to

  • From: Justin Cormack <justin@xxxxxxxxxxxxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sun, 17 Jun 2012 11:20:30 +0100

On Thu, Jun 14, 2012 at 7:28 PM, Mike Pall <mike-1206@xxxxxxxxxx> wrote:

> The '$' character is the marker to be replaced with parameters in
> a C type declaration:
>
> * A ctype or a cdata parameter is interpreted like an anonymous
>  typedef. You can use that to construct derived types, e.g.
>  ffi.typeof("$*", ct) is a pointer to ct, or "$[10]" is a 10
>  element array. Unlike simplistic string concatenation, this
>  works for all cases (e.g. pointers to function pointers).
>
> * A string parameter is treated like an identifier or keyword,
>  except the string is _not_ parsed again or split into words
>  (this isn't simple textual substitution). You can use that for
>  field names, function names, argument names etc.
>
>  [I'm pondering whether I should drop the keyword and type lookup
>  on the string. That would allow you to use arbitrary names for
>  fields, even when they collide with keywords or typedefs. I may
>  change that before writing the docs.]
>
> * A number parameter is treated as an integer. You can use that
>  to construct fixed size arrays. In some contexts you may prefer
>  this over VLAs. It works for multi-dimensional types, too. E.g.:
>
>    local matrix_t = ffi.typeof("uint8_t[$][$]", width, height)
>    ...
>    local m = matrix_t()
>
> Only ffi.typeof() and ffi.cdef() parse parameterized types. The
> other functions, e.g. ffi.new(), don't do that, because they
> already take other arguments (e.g. initializers). I guess it would
> be too confusing to mix two kinds of arguments.
>
>
Here is a quick update of an array slice library (based on the Go one more
or less) to use this new functionality. You couldn't really sanely write
something like this before generically. Needs some more work, but serves as
an example.

https://github.com/justincormack/slice

Justin

Other related posts: