Re: A way to associate a Lua value with a FFI pointer?

  • From: Richard Hundt <richardhundt@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 09 Apr 2014 11:47:44 +0200

On 4/9/14 11:31 AM, Daniel Kolesa wrote:
> The address in C stays constant, so there should be no problem with
> using cdata as a key here.

That the address in C stays constant doesn't mean much. That's my point.

```C
int is_equal(char* a, char* b) {
  return a == b;
}
```

```Lua
a = ffi.new('char[1]')
b = ffi.cast('char*', a)

assert(ffi.C.is_equal(a, b) == 1)

assert(a == b) -- even this works (git head)
t = { }
t[a] = 42
assert(t[b] == nil) -- yep
```

That C function cannot tell the difference between `a` and `b` because
the addresses are the same. However, even so, you can't use `b` as a key
where previously you used `a`.

Maybe you already know all this, and I'm just making a noise, but
perhaps somebody else who's reading this can benefit. It crops up
occasionally.



Other related posts: