Newby questions #2: ffi.load, ffi.metatype, ffi.C

  • From: QuentinC <webmaster@xxxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sun, 01 Jul 2012 10:24:47 +0200

Hello listers,
I have more newby questions to ask you :

Question 1
Let's say that I embed luajit into my application, and also link it with another DLL. Basicly compiling with :
gcc 1.c 2.c 3.c ... -lluajit -lmylib

I logically though that all functions coming from mylib.dll inked to my executable where accessible from luajit in the ffi.C namespace.

However, it's not the case and I have to load explicitely mylib2.dll in lua with ffi.load, allthough it is in theory (at least I think so) already loaded by the executable itself.

Why can't I access the functions from linked DLLs using ffi.C ? Yet that's precisely what is done with implicitely linked DLLs such as kernel32.dll or user32.dll.
Is it normal or am I doing something wrong ?


Question 2
It appear that you can define a function and bind it with a different name with the asm keyword, as quickly shown in another thread.
e.g.
ffi.cdef[[ int myfunc (int) asm("otherfunc"); ]]
Then reference it as myfunc in lua, allthough it is exported as otherfunc in the DLL. Is that a suitable way to arrange your function names to have more lua-friendly names, e.g. delete prefixes, camel case vs underscore names, etc. ?
Does it affect performances or is it harmless ?
What would happen if there was a name clash ?


Question 3 which is actually more a suggestion than a question
This code :
ffi.cdef[[
typedef struct foo foo;
foo* foo_new (void) ;
void foo_method (foo*) ;
void foo_free (foo*) ;
]]
local methodTable = { method = ffi.C.foo_method }
local foo = ffi.metatype('foo', {__index=methodTable})

Allow me to write this :
local obj = ffi.C.foo_new()
obj:method()

What is pretty cool. However, in some particular cases, it could also be useful to be able to write unstead:
foo.method(obj)
and this is currently not possible, allthough it looks quite natural.
Adding the ability to write that would also allow usig foo.new() to be used to create a new foo, unstead of going back to ffi.C or the DLL namespace (of course in that case the 'new' key must be added in the __index table)
Would it be possible to add that feature ?

Thank you very much for your answers and have a nice sunday.


Other related posts: