lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


hi list,

I have think about the automatic way to generate C/C++ binding code
for Lua. there are big two ways to make binding:
 - write binding code by hand, suite for small projects. has too much
working for big projects.
 - LuaBind/slb, runtime binding code generator. these can not handle
something like callbacks or overloads. and you must write runtime code
by hand.
 - automatic generate C/C++ code and compile. like tolua/tolua++, SWIG
and lqt. they are not flexible. e.g. you can insert some extra
operations to some function, and you can't change the meanings of
function arguments, e.g. in some functions, void*, size_t are two
different arguments, but in other functions, they represent a single
string/buffer argument. another examples: the Fog Framework[1] have
only template objects to indicate geometry arguments, like:
DrawImage(Point(1, 2), image); but using template object in Lua is
expensive, we want generate bindings that accept two number and a
Point structure.

So maybe we can merge the 2, 3 ways. that is, we create a pure lua
module to generate binding code, but we can use automatic binding
generator to generate the Lua code. the interface may like this:


local B = require 'bindings'
local type,   module,   class,   func =
   B.type, B.module, B.class, B.func

B.basetypes(B)
local voidptr, int, char = B.voidptr, B.int, B.char

type "foofunc"
   :is(1) [[
       is_foofunc(L, narg)
   ]]
   :push(1) [[
       push_foofunc(L, narg)
   ]]
   :to(1) [[
       to_foofunc(L, narg)
   ]]

module "test"
   :pre [[
       #include <stdio.h>
   ]]

   :preinit [[
       printf("init test...\n");
   ]]

   :postinit [[
       printf("finish init test...\n");
   ]]

   class "foo"
       func "mem1"
           :proto (voidptr "a", int "b", char "c")
           :ret (int "ret", int "b")
               :precall [[
                   printf("call function mem1: (%p, %d, %c)\n", a, b, c);
               ]]
               :postcall [[
                   printf("finish call function mem1, ret = %d, b =
%d\n", ret, b);
               ]]

           :proto (int "b", char "c")
           :ret (int "ret", int "b")
               :precall [[
                   printf("call function mem1: (%d, %c)\n", b, c);
               ]]
               :postcall [[
                   printf("finish call function mem1, ret = %d, b =
%d\n", ret, b);
               ]]

B.module.test:generate "test_wrap.cpp"

and a test_wrap.cpp file generated, used to binding a C++ class named foo.

this code can generated by a C/C++ parser, like cpptoxml in Qt. and
the generator can accept a existing Lua file, and try to modify it,
try to add/comment lines in it. so you can use automatic generator to
generate a Lua file, and modify it. and if the library interface is
changed, you can just rerun the generator, and do some modify again.

I'm try to add some mechanism to generate OO style interface over pure
C library (like cairo), Are there any idea about it? I will begin this
plan, and after I have some result, I will create a project on github,
and reply this mail.

thank you :-)

[1]: http://code.google.com/p/fog/