satoren / kaguya

C++ binding to Lua
Boost Software License 1.0
345 stars 70 forks source link

__index and __newindex can not work in kaguya. #33

Closed aster2013 closed 8 years ago

aster2013 commented 8 years ago

e.g.

static const Variant& VariantMapGetVariant(const VariantMap& variantMap, const String& key)
{
    VariantMap::ConstIterator i = variantMap.Find(StringHash(key));
    if (i == variantMap.End())
        return Variant::EMPTY;

    return i->second_;
}

static void VariantMapSetVariant(VariantMap& variantMap, const String& key, const Variant& value)
{
    variantMap[StringHash(key)] = value;
}

static void RegisterVariantMap(kaguya::State& lua)
{
    using namespace kaguya;

    lua["KVariantMap"].setClass(UserdataMetatable<VariantMap>()
        .setConstructors<VariantMap()>()

        // Can not work in kaguya
        // .addStaticFunction("__index", &VariantMapGetVariant)
        // .addStaticFunction("__newindex", &VariantMapSetVariant)

        .addStaticFunction("Get", &VariantMapGetVariant)
        .addStaticFunction("Set", &VariantMapSetVariant)
        );
}
aster2013 commented 8 years ago

Current in kaguya, __index meta method is using for property. This is a good idea.

satoren commented 8 years ago

Property using newindex. All member's using index

aster2013 commented 8 years ago

And please add another function:

template<typename ... Funcs>
UserdataMetatable<T>::addStaticOverloadedFunctions(const char* name, Funcs... f)
{
}
satoren commented 8 years ago

Why?

aster2013 commented 8 years ago

For example:

class Viewport
{
};

Viewport* CreateViewport1();
Viewport* CreateViewport2(Scene*);
Viewport* CreateViewport3(Scene*, Camera*);

 lua["Viewport"].setClass(UserdataMetatable<Viewport>()
      .addStaticFunction("new", CreateViewport1, &CreateViewport2, &CreateViewport3)
// ...
satoren commented 8 years ago

Currently.

        template<typename... Funcs>
        UserdataMetatable& addOverloadedFunctions(const char* name, Funcs... f)

And the future that does not change.

aster2013 commented 8 years ago

Bad performance.

LuaFunction indexfun = kaguya::LuaFunction::loadstring(state, "local arg = {...};local metatable = arg[1];"
                        "return function(table, index)"
                        //                      " if type(table) == 'userdata' then "
                        " local propfun = metatable['_prop_'..index];"
                        " if propfun then return propfun(table) end "
                        //                      " end "
                        " return metatable[index]"
                        " end")(metatable);

the function may call muiti-times, with index like this: prop'name' _propprop_'name' _propprop_prop'name'

you can write print(index) to view it.

satoren commented 8 years ago

index and newindex rewrite on C.

satoren commented 8 years ago

If allow to add index and newindex, do you need what kind of work? Propery and function will not work if it is to overwrite.

aster2013 commented 8 years ago

the index and newindex method is using for HashMap in urho3d. like std::map<std::string, variant>.

satoren commented 8 years ago

It can not be convert to lua table?

satoren commented 8 years ago

Maybe better performance lua-table than userdata, If the HashMap modify a lot.

aster2013 commented 8 years ago

Current the VariantMap is defines as HashMap<StringHash, Variant>, the key is a string hash not a string. and the VariantMap is just for event data. it will not modify a lot.

satoren commented 8 years ago

OK. I understand now.

aster2013 commented 8 years ago

Hi, the function with default argument value is not working now.

satoren commented 8 years ago

default argument value can not detect in C++ code.#17

satoren commented 8 years ago

Added support for default argument value.