Re: Using FFI as a plugin

  • From: Coda Highland <chighland@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 17 May 2012 14:26:00 -0500

On Thu, May 17, 2012 at 2:11 PM, Mike Pall <mike-1205@xxxxxxxxxx> wrote:
> Coda Highland wrote:
>> On Thu, May 17, 2012 at 1:41 PM, Mike Pall <mike-1205@xxxxxxxxxx> wrote:
>> > You can pass a pointer to a struct of initialized function
>> > pointers as lightuserdata from the C side, then ffi.cast that back
>> > to the struct pointer on the Lua side. Ok, so there's a little bit
>> > more duplicated stuff on each side. But the net effect is you're
>> > able to just call lib.foo(...).
>>
>> Hmm... I like this solution! For the sake of some dynamism, would it
>> work to copy the members out of that struct? Or would that carry the
>> same caveats as hoisting functions out of a namespace?
>
> It doesn't really matter whether the function pointer comes from a
> struct or a local variable. A struct is not a library namespace,
> so it doesn't share the same optimizations. In particular, function
> pointers are not assumed to be constants, so you always get indirect
> calls (*), which doesn't matter much on Intel/AMD chips.
>
> (*) Indirect calls are the norm on x64 for dlsyms from shared
> libraries, since they're usually mapped farther away than +-2GB
> from the calling code, anyway.
>
> --Mike
>

Good to know! Not that it'll help me in this project, but that's still
useful information for standalone stuff.

/s/ Adam

Other related posts: