lua-users home
lua-l archive

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



The following might work better. You can't quite just put
"testObj.Hello" because Lua will think its an assignment. I put a dummy
Hello function in your luaNET object. As Riki pointed out, __index needs
to return the function object. It's the "luaNET.call(...)" in myIndex
which involkes the function object. You try to involke the function
object twice, and on the second call it's a nil.

Another name might be CSharpProxy as luaNET is general and implies its
embedded in .NET??


-----

luaNET = {
    Hello = function(handle) return "Hello! "..handle end,

    call = function(handle,fn) return luaNET[fn](handle) end
}

function myIndex(t, func)
   return luaNET.call(t.handle, func)
end;

LuaNETObject = {}
LuaNETObject_mt = { __index = myIndex }

function LuaNETObject:new(handle)
   return setmetatable( {handle = handle}, LuaNETObject_mt)
end

-- 1 is a valid handle
testObj = LuaNETObject:new(1)

-- should reroute the call
print( testObj.Hello )

-----Original Message-----
From: Marco Kögler [mailto:marco.koegler@web.de] 
Sent: Tuesday, April 22, 2003 2:21 PM
To: Multiple recipients of list
Subject: Using index-event to call external function


Hi!

I've been tinkering a bit with Lua and C#. I'm trying to use the __index

metatable event to re-route function calls to the C# side. My setup is 
like this:

On the Lua-Side I have a 'handle' to a C# object. This handle is in a 
table, which has a metatable attached to it. I use the metatable to 
intercept the index event. Here is some code, which evolved somewhat out

of the ClassesWithMetatables article in the Wiki. It currently ignores a

lot of stuff, but I want to get this simple thing working first before 
making everything 'nice':

function myIndex(t, func)
   return luaNET.call(t.handle, func);
end;

LuaNETObject = {};
LuaNETObject_mt = { __index = myIndex };

function LuaNETObject:new(handle)
   return setmetatable( {handle = handle}, LuaNETObject_mt); end;

-- 1 is a valid handle
testObj = LuaNETObject:new(1);

-- should reroute the call
testObj:Hello();

OK, now the above code does call the C# function of the object in 
testObj correctly (it just prints something out), but after the last 
line Lua complains with this:

[string "function myIndex(t, func)   return luaNET...."]:1: attempt to 
call method `Hello' (a nil value)

Obviously, the table testObj did not contain a Hello-function. But, I 
thought if the index event handles this, then Lua would be silent. Can 
somebody help me out? Is there a clean way of achieving this? I hope my 
explanation is not too obscure. So, basically I would like to somehow 
signal Lua in the myIndex function that 'everything went alright'.

BTW, this is not related to Lua.NET ... I just couldn't find a better 
name for this, other than luaNET.

Thanks,
Marco