1
votes

I'm writing a "C" userdata array structure. As setter and getter i want normal array access (u[0] = 1 u[0]) like it's discussed here: [c array share][1]Share Array between lua and C. For that i need to set __index and __newindex to the set and get functions in c.

Additional i want object-oriented access, too, "like u:mymethod()". My trouble is, that i need to set now __index to the metatable itself.

Is there a way, to achieve both?

1
Why do you need your __index to be the metatable? It is easier if you keep them separate.finnw
I read it here: lua.org/pil/28.3.html. But yes, i don't need it.user2328978

1 Answers

4
votes

Just one of many possible ways to achieve this:

local userdata = { _array = { "A", "B", "C" } }
local mt = { }
local methods = { }

function mt.__index(userdata, k)
    if methods[k] then
        return methods[k]
    else
        return rawget(userdata, "_array")[k]
    end
end

function mt.__newindex(userdata, k, v)
    if methods[k] then
        error "can't assign to method!"
    else
        rawget(userdata, "_array")[k] = v
    end
end

function methods.count(userdata)
    return #rawget(userdata, "_array")
end

setmetatable(userdata, mt)

userdata[3] = "Z"

print(userdata[1])
print(userdata[2])
print(userdata[3])
print(userdata:count())

userdata.count = 0

edit: As lhf pointed in his comment, it is not dangerous to use metatable as it's __index table at all, because c-methods should always check on what self they operate.