lua-users home
lua-l archive

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


>From dmarks@dionysus.phs.uiuc.edu Sat Apr 18 01:22:37 1998

>Since lhf asked (I think) about any results using the new lua_state feature
>of 3.1 alpha I thought I would report some results.

good!

>One thing I did not get with 3.1 alpha is what the luaCclosure function
>is for.  In the example it is used for the iolib library to assign tag
>functions.  How is this different than lua_pushcfunction?

a Cclosure is like a Lua closure, except that it's a C function :-)
in 3.1, we have introduced the anonymous functions and the concept of upvalues,
which are a way to access the value of local variables in the code that defines
the function (see manual for example).
A closure is then made of code plus upvalues.
in a Cclosure, the code is  C function, that's all.
the upvalues are the ones pushed before the the call to lua_pushCclosure.

one typical application of Cclosures is to export C functions to Lua with
some fixed arguments. 
for example, you might want to export fputs to Lua, but with a fixed FILE*.
so, you just push this FILE* as an upvalue.
then you get a function in Lua that only needs the string.
the C function itself receives 2 args, but the C closure will provide one
automatically.

hope this helps.
--lhf