[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Binding question
- From: Fabio Mascarenhas <mascarenhas@...>
- Date: Fri, 29 Apr 2005 14:55:58 -0300
On 4/29/05, Luis Villegas <luisvill@microsoft.com> wrote:
> Hi,
>
> I am fairly new to Lua and wanted to understand better some of the C/C++
> binding mechanisms. I understand how binding by name works, but I am not
> clear on how "late binding" works. When I say "late binding" I refer to the
> mechanisms used in LuaCOM and LuaInterface for calling methods on tables
> which do not exist at registration time.
>
Do you mean calling methods on C++/COM/.NET objects? What these two
libraries do (and LuaJava does too, and what you usually do when you
want to use C++ objects from Lua, for example) is to store references
to the actual object in an userdata, then set a metatable for this
userdata. The metatable's __index method will return a proxy to the
actual method. This proxy is another userdata, with another metatable.
This metatable's __call method will marshall the arguments from Lua to
C++/COM/.NET/Java/whatever, call the method, and marshall result(s)
back to Lua. On the C++/COM/.NET/Java/whatever side the call is doen
by actual late binding (COM IDispatch, .NET and Java reflection), or
hardcoded (in the case of C++).
> My assumption is that at object creation time there is some extra data
> stored in some table and that at runtime the call is intercepted and then
> forwarded to a function that acts on the function call using the data stored
> during object creation; but I am not getting a clear picture from the source
> code of the projects mentioned above.
Your assumptions are mostly correct. :-) They store the objects in
userdata (a Lua data type for storing arbitrary data). Methods of
metatables (metamethods) intercept the calls, and forward them to the
corresponding object. If you want to understand the details, I suggest
you take a look at the Lua C API (either in Programming in Lua, or in
the Reference manual). Then you can try to understand the binding code
of a simple C++ library, for example. LuaJava, LuaInterface and LuaCOM
are more complicated, as they have to deal with any kind of object
that a Java, .NET or COM application can implement, so they have messy
details about discovering types, introspection, marshalling of several
different data types, and so on. LuaJava is probably the simpler of
the three to understand, followed by LuaInterface (.NET is messier,
with its properties, events, out parameters, etc.) and LuaCOM in a
distant third (IDispatch is *very* messy).
Feel free to direct any specific questions you have about either
LuaInterface or LuaCOM to me. I am the author of LuaInterface and
current maintainer of LuaCOM, so I am quite familiar with both
libraries.
--
Fabio Mascarenhas
mascarenhas@acm.org