lua-users home
lua-l archive

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


  I am pretty new with Lua, but from what I've been able to tell, the
LUA_TFUNCTION type represents both functions defined within Lua, as well
as C Functions pushed in from the API side.  So it is ambiguous as to
which type the variable actually has.

  I have found that I can get a reference to either type by first
calling lua_tocfunction.  If that returns NULL then the function has
been defined within Lua, and I use lua_ref to get a reference to it.

  To push it back on the stack, lua_pushcfunction is used (if
lua_tocfunction returned a non-NULL value), otherwise lua_getref is used
to push a Lua defined function.

  I would think using references is the proper way to handle what you're
trying to do.

  Dan East

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Tassilo von
Parseval
Sent: Saturday, June 19, 2004 3:05 AM
To: Lua list
Subject: What to do with values of type LUA_TFUNCTION?


Hi there,

looking at the Lua manual, section 3.5 ("Getting Values from the
Stack"), I don't find any information regarding LUA_TFUNCTION values.

The scenario:

I am adding more type conversions for Inline::Lua. The nice thing is
that Perl and Lua types are quite compatible so that it is even possible
to turn a Perl function reference into a real Lua function thusly:

    use Inline Lua;
    
    foo( sub { return 2 * shift } );
    
    __END__
    __Lua__
    function foo (f)
	io.write( f(42) )
    end

which then, quite expectedly, yields '84'. This part already works.

Of course, I'd like to make the other way work too:

    use Inline Lua;

    my $func = foo(42);
    print $func->();
    
    __END__
    __Lua__
    function foo (a) 
	return function (b) io.write(a+b) end
    end

When the Inline::Lua code calls 'foo', it now has a LUA_TFUNCTION value
on the stack which I don't quite know how to deal with. I need to get it
off the stack somehow and store it in a variable that I can return to
the calling Perl code for later usage.

Some naive tests show that I can use lua_touserdata(L, i) to get the Lua
function into a C variable, push it onto the stack later using
lua_pushlightuserdata(L, ptr) and call it with lua_(p)call. Is that how
it's supposed to be done?

Regards,
Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=
_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJb
us#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;
eval