lua-users home
lua-l archive

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



hi all

I'm having a spot of bother managing the disparate type system between lua and my host app.

The way I currently expose an object down to Lua is via a table with a metatable attached, implementing __index and __newindex. This allows me to get a callback when a member of the object is accessed (which in Lua relates to the indexing of a table). Simple members of my object go down as lua simple types (i.e. string or number)

e.g

function filter(Session)
   if(Session.Username == "adrien") then
      return true;
   end
   return false;
end

works just great. As long as when I am called back from Lua in the __index callback on 'Session' I return a lua string. However, if I want to extend the type of Session.Username, then it has to be a table again. This then creates problems when I want to use the Username string value in lua as a string (i.e. in other lua calls), but it really needs to be a table as well at the same time.

My proposed solution is a __cast metamethod. Similar to the Base library __tostring and __tonumber metamethods, however it would be able to return the correct value (or Nil) for whatever the type Lua needed it to be. Furthermore it wouldn't require any explicit lua calls to tostring() or tonumber()

that covers the rvalue case, for the lvalue case we would need an __assign metamethod. This allows you to assign a value to a table (rather than an index of a table).

These 2 extensions would allow Lua to encapsulate any hosted type via a table, avoiding explicit calls to conversion and assignment members. E.g I want to avoid having to get my users to write script like:

function filter(Session)
   if(Session.Description.AsString() == "something") then
       Session.Description.Set("some new description")
       Session.Description.MakeUpper();
   end

   if(Session.ClientIP.AsString() == "192.168.0.1") then
      return false;
   else if(Session.ClientIP.InSubnet("192.168.0.0", "255.255.255.0"))then
      return false;
   return true;
end


I'd much rather the syntax be something more like

function filter(Session)
   if(Session.Description == "something") then
      Session.Description = "some new description";
   end

   if(Session.ClientIP == "192.168.0.1") then
      return false;
   else if(Session.ClientIP.InSubnet("192.168.0.0", "255.255.255.0"))then
      return false;
   return true;
end


And have Lua call me back whenever it needs to convert a table into a string or be assigned a value.

this would also then allow lua script like

function dosomething(object1, object2)
   object2.somemember = "hello";
object1 = object2; -- deep copy in host via __assign metamethod rather than table reference
end


I'm pretty sure it can be done by hacking the lua source code, but I don't yet understand the structure well enough to find where to insert metatable calls. Anyone have any ideas?

Regards

Adrien de Croy


--
Adrien de Croy - WinGate Proxy Server - http://www.wingate.com