hoelzro / inline-lua

Perl extension for embedding Lua scripts into Perl code
4 stars 5 forks source link

Lua Objects -> Perl Objects? #11

Closed unhandyandy closed 9 years ago

unhandyandy commented 9 years ago

Can I define an object with a method in Inline Lua and then call it in Perl?

For example, should I be able to have

__Lua__
local thing = {}
thing.mt = {__index = thing}
function thing.action( x )
    return x + 1
end

and then call it in perl by thing->action( 3 )?

unhandyandy commented 9 years ago

Or maybe by

&{$thing->{action}}(3)
hoelzro commented 9 years ago

@unhandyandy This is not currently possible; IIRC, Inline::Lua marshals objects (including tables) between the Perl and Lua environments, so tables lose their metatable. You can, however, do the $object->{method_name}->(@args) trick, as long as method_name is a key of $object itself and not accessed via a metatable.

unhandyandy commented 9 years ago

OK, thanks.