lua-users home
lua-l archive

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


Hello Bernhard,

    Use a metatable, create a function for __newindex and __index
metamethods.
    In __newindex you break the assignment, and in __index you return the
function. See manual for info about use of metatable and metamethods.
    A lua code (this an example, you need to implement in C/C++ side):

function theDoSomethingFunction()
     return "theDoSomethingFunction called...";
end

    myObject = {}
    meta_myObject = {}
    function meta_myObject.__newindex( tab, index, value )
        if index == "doSomething" then
            error( index .. " is readonly!" );
        else
            rawset(tab,index,value);
        end
    end

    function meta_myObject.__index( tab, index )
        if index == "doSomething" then
            return theDoSomethingFunction;
        else
            return rawget(tab,index);
        end
    end

    setmetatable(myObject, meta_myObject);

print(myObject:doSomething());
myObject.doSomething = "Hello World"; --> error: "doSomething is
readonly...";

    I imagine this can help you.

                                                                    The
God's Peace,


Leandro.


----- Original Message -----
From: "Bernhard Glück" <bernhard@realspace.org>
To: <lua@bazar2.conectiva.com.br>
Sent: Tuesday, December 09, 2003 10:21 AM
Subject: Question about function call interception.


> Hi!
>
> I have a pretty strange question about Lua.
> We use tables to emulate classes in the Lua 5.0 language.
> Now is there a way to "intercept" method calls this way... so that
> whenever i invoke
> a method on a table a C Function is called ( similar like this )
>
> LUA:
> myObject:doSomething()
>
> C++
> void OnMethodCall( const char * methodname,lua_State * s )
> {
>
> }
>
> This would allow us to automate our export of C++ classes to lua , since
> we have a very sophistcated
> C++ reflection system in place, which allows us to determine a C++ classes
> methods at runtime.
> This way we would never have to write wrappers again etc..
>
>
>
> --
> Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
>