lua-users home
lua-l archive

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


Hi Tom,

Some people might have a better advice, but I think I would use two tables like this in your case:

-- methods
methods = { C methods, __index = C method }
-- metatable
mt = { __newindex, __index = methods, __self = userdata}

With this system, when you fire

object:foo(value) --> mt.__index(object, "foo") --> methods.foo(object, value)

object.x --> mt.__index(object, "x") --> methods.__index(mt, "x") --> your call to resolve

Beware that the C method now contains the mt table and "x" on stack, not object and "x".

You can easily test this in pure Lua to be sure...

Gaspard

On Wed, Jan 5, 2011 at 9:02 PM, Tom Skwara <tskwara@mobileappsystems.com> wrote:
I need to be able to do the following, where objectA is full user data:

objectA:add(arg1, arg2)

-and-

objectA.x = objectA.x + 2;



This requires __index, __newindex, and method calling.


I tried a couple different approaches:

Method A
  1. Create metatable methods loaded with functions such as add and destroy.
  2. Assign methods.__index to a custom C function that examines it's arguments and returns the value of the instance variable of my object pointed to by userdata.
  3. Assign methods.__newindex to a custom C function that examines it's arguments and sets the value of the instance variable of my object pointed to by userdata.
  4. Assign methods as the metatable of the userdata object (objectA) after it is created.

What's Working - Setting and getting properties (objectA.x = objectA.x + 2)
What's Not Working -  The functions contained in the methods metatable are not automatically called.  I added some code to the methods.__index C function to catch the function calls, but the arguments only include the methods table, and function key - no function arguments are included (self, arg1, and arg2 are missing).


Method B
  1. Create empty metatable attributes.
  2. Assign attributes.__index to a custom C function that examines it's arguments and returns the value of the instance variable of my object pointed to by userdata.
  3. Assign attributes.__newindex to a custom C function that examines it's arguments and sets the value of the instance variable of my object pointed to by userdata.
  4. Create metatable methods loaded with functions such as add and destroy.
  5. Assign attributes as the metatable of methods metatable.
  6. Assign methods as the metatable of the userdata object (objectA) after it is created.

What's Working - The functions contained in the methods metatable are automatically called with the correct arguments
What's Not Working -  Setting and getting properties (objectA.x = objectA.x + 2) does not work as the attrbutes.__index and attrbutes.__newindex custom C functions are not getting the proper arguments.

This would seem to be a common requirement: a userdata object requiring functions on self, along with custom setter and getter routines.  This is all in C as I need to manipulate an Objective C object through it's methods and properties.  The Obj-C object is pointed to by the userdata object.

Does it make more sense to use a table vs userdata?  I'm literally on day #4 with this problem and would greatly appreciate the group's input.

Thanks in advance!

-Tom