lua-users home
lua-l archive

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



On 15 Apr 2008, at 11:40, Matias Guijarro wrote:
Is there a way to detect if a function in a table should be called
as a "method" (OO style), e.g foo:bar() or if it has to be called
as a function - foo.bar() ?

I don't think so. It is entirely dependent of the function's implementation. Furthermore, 'foo:bar()' is only a syntactic sugar, therefore it is the exact same thing as 'foo.bar(foo)' with some optimization. Thus, unless you do some source code pre-processing, I don't see how you can find out if the sugar was used either.

Consider a XML-RPC server in Lua : exported functions and
"objects" in the global environment are available for clients. If
a client makes a request like this : "foo.bar", the server needs
to know how to execute the request.

I believe you can do this in many different ways (as most things in Lua ;-). For example, you can simply state that functions in tables are always invoked as methods. Other way is to say that only numbers, booleans and strings values are copied to the remote context. But, more complex values, like tables, functions, userdata or threads, are sent by reference, which will generate a proxy in the remote context that can be used to access the original value remotely. This way, if your protocol can resolve references to local objects, you can replace the references for them before dispatching the invocation. This way, you'll can have:

local get_object = myRPC.newfunction("get_object", "remotehost:80")

local object = get_object() -- remotely invokes function "get_object" that -- returns a table, which will be received by -- reference and will result in the creation of -- a proxy stored in local variable 'object'.

object.func(1,2,3) -- when the proxy is indexed, it creates a function proxy -- for "object.func" that will be invoked remotely with a
                   -- copy of the parameters provided.

object:method(1,2,3) -- same as 'object.method(object,1,2,3)', thus another -- function proxy is created, but for "object.method". -- However, the first parameter is a proxy, which denotes a -- reference to remote table 'object', which shall be -- replaced by the 'object' table when the invocation is
                     -- performed in the remote context.

A similar approach is used in OiL (http://oil.luaforge.net/). In fact, there are many other design decisions you can make that might help you solve this problem. For example, you can force the application to provide interface descriptions that will tell you how each function should be invoked. For a very simple (and somewhat buggy) example of remote method invocation in Lua, see the example at the bottom of the following page:

http://loop.luaforge.net/docs_SocketStream.html

Is there some debug library magic to be able to have a hint ?
I wonder, because for example if we know that the first
argument of function "bar" is "self", it probably indicates
that it should be called with ":" ... Well, even that is not a
good solution :-(


I think you can only define some heuristics that might work in the some cases, but I don't believe this can be done in general.


--
Renato Maia
PhD student at PUC-Rio
__________________________
http://www.inf.puc-rio.br/~maia/