Skip to content

Support metatables for GoStruct #74

@andreysm

Description

@andreysm

I'm trying to set a metatable to a GoStruct object to be able calling methods in the manner like obj:Foo().

Currently, testudata() doesn't allow me to do this, requiring the object metatable to be MT_GOINTERFACE or MT_GOFUNCTION.

I understand the idea of this check, and suggest extending it in the following manner:

if (!lua_rawequal(L, -1, -2))  /* not the same? */

    // Check the second-level metatable
    if (lua_getmetatable(L, -2)) {
        if (lua_rawequal(L, -2, -1)) {  /* equals! */
            lua_pop(L, 3);  /* cleanup */
            return p;
        }
        lua_pop(L, 1);
    }
    // end of new code

    p = NULL;
}
lua_pop(L, 2);  /* remove both metatables */
return p;

In addition, the setmetatable() call should be modified (as following), or a new function (e.g. SetGoStructMetaTable) may be introduced if you find this way more appropriate.

void clua_setmetatable(lua_State* L, unsigned int index)
{
    if (clua_isgostruct(index)) {
        // Set MT_GOINTERFACE as a second-level metatable for the user metatable
        luaL_getmetatable(L, MT_GOINTERFACE);
        lua_setmetatable(L,-2);
    }

    lua_setmetatable(L, index);
    return true;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions