lua-users home
lua-l archive

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


2011/11/10 steve donovan <steve.j.donovan@gmail.com>:
> On Wed, Nov 9, 2011 at 10:22 AM, Xavier Wang <weasley.wx@gmail.com> wrote:
>> 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:
>
> That's a good use of Lua as a DSL.  But I am a little confused - this
> module is a script that generates C extension code? So then where does
> the automatic binding generate?

Sorry for confusion, my bad :-(

the Lua code i showed is the thing that the generator produced. that
means, e.g. I have a library named libfoo.a, and the header is foo.h:

/* foo.h */
void foo(int);

then I run generator against to foo.h:

$ generator foo.h

it will generate a foo.lua file, something like the Lua code I showed before.

/* foo.lua */
local B = require "bindings"
local type, module, class, func = .....
local char, int, cstr = ....

func "foo" :args(int)
....

and then you can modify this file to add what you want, then execute this file:

$ lua foo.lua -o foo_wrap.c

then you can compile foo_wrap.c to get your module:

gcc foo_wrap.c -shared -lfoo -ofoo.so

now you can use foo module in Lua:

local foo = require 'foo'
....


>
> My approach to the C extension issue is to preprocess C until it looks nicer ;)
>
> #include <string.h>
>
> module "str" {
>
>  def at (Str s, Int i = 0) {
>    lua_pushlstring(L,&s[i-1],1);
>    return 1;
>  }
>
>  def upto (Str s, Str delim = " ") {
>    lua_pushinteger(L, strcspn(s,delim) + 1);
>    return 1;
>  }
>
> }
>
> This is processed by LuaMacro into the usual kind of C, but handles
> some of the irritating bookkeeping for you. It is clever enough to
> generate #line directives and so generally it's plain sailing to track
> errors etc.

It's good, but you must write this macro DSL by hand, and my foo.lua
is automatic generated.