Пример #1
0
LUA_INLINE bool CppBindClassBase::buildMetaTable(LuaRef& meta, LuaRef& parent, const char* name,
    void* static_id, void* clazz_id, void* const_id)
{
    LuaRef ref = parent.rawget<LuaRef>(name);
    if (ref != nullptr) {
        meta = ref;
        return false;
    }

    auto L = parent.state();
    std::string type_name = "class<" + CppBindModule::getFullName(parent, name) + ">";

    LuaRef type_const = LuaRef::fromPointer(L, const_id);
    LuaRef type_clazz = LuaRef::fromPointer(L, clazz_id);
    LuaRef type_static = LuaRef::fromPointer(L, static_id);

    LuaRef clazz_const = LuaRef::createTable(L);
    clazz_const.setMetaTable(clazz_const);
    clazz_const.rawset("__index", &CppBindClassMetaMethod::index);
    clazz_const.rawset("__newindex", &CppBindClassMetaMethod::newIndex);
    clazz_const.rawset("___getters", LuaRef::createTable(L));
    clazz_const.rawset("___setters", LuaRef::createTable(L));
    clazz_const.rawset("___type", "const_" + type_name);
    clazz_const.rawset("___const", clazz_const);
    clazz_const.rawset(CppBindClassMetaMethod::signature(), type_const);

    LuaRef clazz = LuaRef::createTable(L);
    clazz.setMetaTable(clazz);
    clazz.rawset("__index", &CppBindClassMetaMethod::index);
    clazz.rawset("__newindex", &CppBindClassMetaMethod::newIndex);
    clazz.rawset("___getters", clazz_const.rawget<LuaRef>("___getters"));
    clazz.rawset("___setters", LuaRef::createTable(L));
    clazz.rawset("___type", type_name);
    clazz.rawset("___const", clazz_const);
    clazz.rawset(CppBindClassMetaMethod::signature(), type_clazz);

    LuaRef clazz_static = LuaRef::createTable(L);
    clazz_static.setMetaTable(clazz_static);
    clazz_static.rawset("__index", &CppBindClassMetaMethod::index);
    clazz_static.rawset("__newindex", &CppBindClassMetaMethod::newIndex);
    clazz_static.rawset("___getters", LuaRef::createTable(L));
    clazz_static.rawset("___setters", LuaRef::createTable(L));
    clazz_static.rawset("___type", "static_" + type_name);
    clazz_static.rawset("___class", clazz);
    clazz_static.rawset("___const", clazz_const);
    clazz_static.rawset("___parent", parent);
    clazz_static.rawset(CppBindClassMetaMethod::signature(), type_static);

    LuaRef registry(L, LUA_REGISTRYINDEX);
    registry.rawset(type_clazz, clazz);
    registry.rawset(type_const, clazz_const);
    registry.rawset(type_static, clazz_static);
    parent.rawset(name, clazz_static);

    meta = clazz_static;
    return true;
}
Пример #2
0
LUA_INLINE bool CppBindClassBase::buildMetaTable(LuaRef& meta, LuaRef& parent, const char* name,
    void* static_id, void* clazz_id, void* const_id, void* super_static_id)
{
    if (buildMetaTable(meta, parent, name, static_id, clazz_id, const_id)) {
        LuaRef registry(parent.state(), LUA_REGISTRYINDEX);
        LuaRef super = registry.rawget<LuaRef>(super_static_id);
        meta.rawset("___super", super);
        meta.rawget<LuaRef>("___class").rawset("___super", super.rawget<LuaRef>("___class"));
        meta.rawget<LuaRef>("___const").rawset("___super", super.rawget<LuaRef>("___const"));
        return true;
    }
    return false;
}