Re: API to get length of array cdata

  • From: Mike Pall <mike-1205@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Mon, 14 May 2012 15:21:00 +0200

Cosmin Apreutesei wrote:
> Arrays of ints, words and structs - part of the winapi binding I'm
> working on. I want to accept a cdata array as an argument to a
> function but windows wants me to tell how many elements are in the
> array obviously (and there's no field for that in the struct).

Well, then pass the number of elements, too.

> I always know the struct type so the sizeof(cdata) /
> sizeof(struct_ctype) method works for me

That breaks down when you get passed a pointer and not an array.
I don't think it does good to add too much magic when binding to a
low-level API. What if someone wants to pass a slice of an array?
What if they only have a pointer, returned by another C function?

> just thought it would be cool to be able to say #cdata on a VLA. I'm
> guessing the info is there just needs to be exposed.

I didn't want to have a default __len function, because it would
be rather ambiguous what it's supposed to return. And we get the
override problematic, too. Considering all of those noisy
discussions about the behavior of the '#' operator we had on the
Lua mailing list, I'm not keen on starting such a discussion here. :-)

--Mike

Other related posts: