lua-users home
lua-l archive

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


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{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval