"Core/LuaInterface/LuaInterface.csproj" did not exist on "d7bb0c9380d7a29151ed1c9222e9993ec89c8578"
Commit 5cc2ec45 authored by capresti's avatar capresti
Browse files

- Implemented a basic fix for issues with indexers (multiple indexers on an...

- Implemented a basic fix for issues with indexers (multiple indexers on an object, indexers with values the same as a property name of the object)

git-svn-id: http://luainterface.googlecode.com/svn/trunk@3 63eb109e-e254-0410-a61e-ed0b8f8614f5
parent c653ca2b
...@@ -147,9 +147,16 @@ namespace LuaInterface ...@@ -147,9 +147,16 @@ namespace LuaInterface
string methodName = index as string; // will be null if not a string arg string methodName = index as string; // will be null if not a string arg
Type objType = obj.GetType(); Type objType = obj.GetType();
// Handle the most common case, looking up the method by name // Handle the most common case, looking up the method by name.
if (methodName != null && isMemberPresent(objType, methodName))
return getMember(luaState, objType, obj, methodName, BindingFlags.Instance | BindingFlags.IgnoreCase); // CP: This will fail when using indexers and attempting to get a value with the same name as a property of the object,
// ie: xmlelement['item'] <- item is a property of xmlelement
try
{
if (methodName != null && isMemberPresent(objType, methodName))
return getMember(luaState, objType, obj, methodName, BindingFlags.Instance | BindingFlags.IgnoreCase);
}
catch { }
// Try to access by array if the type is right and index is an int (lua numbers always come across as double) // Try to access by array if the type is right and index is an int (lua numbers always come across as double)
if (objType.IsArray && index is double) if (objType.IsArray && index is double)
...@@ -161,40 +168,55 @@ namespace LuaInterface ...@@ -161,40 +168,55 @@ namespace LuaInterface
else else
{ {
// Try to use get_Item to index into this .net object // Try to use get_Item to index into this .net object
MethodInfo getter = objType.GetMethod("get_Item"); //MethodInfo getter = objType.GetMethod("get_Item");
ParameterInfo[] actualParms = (getter != null) ? getter.GetParameters() : null; MethodInfo[] methods = objType.GetMethods();
if (actualParms == null || actualParms.Length != 1) foreach (MethodInfo mInfo in methods)
{
translator.throwError(luaState, "method not found (or no indexer): " + index);
LuaDLL.lua_pushnil(luaState);
}
else
{ {
// Get the index in a form acceptable to the getter if (mInfo.Name == "get_Item")
index = translator.getAsType(luaState, 2, actualParms[0].ParameterType);
object[] args = new object[1];
// Just call the indexer - if out of bounds an exception will happen
args[0] = index;
try
{
object result = getter.Invoke(obj, args);
translator.push(luaState, result);
}
catch (TargetInvocationException e)
{ {
// Provide a more readable description for the common case of key not found //check if the signature matches the input
if(e.InnerException is KeyNotFoundException) if (mInfo.GetParameters().Length == 1)
translator.throwError(luaState, "key '" + index + "' not found "); {
else MethodInfo getter = mInfo;
translator.throwError(luaState, "exception indexing '" + index + "' " + e.Message); ParameterInfo[] actualParms = (getter != null) ? getter.GetParameters() : null;
LuaDLL.lua_pushnil(luaState); if (actualParms == null || actualParms.Length != 1)
{
translator.throwError(luaState, "method not found (or no indexer): " + index);
LuaDLL.lua_pushnil(luaState);
}
else
{
// Get the index in a form acceptable to the getter
index = translator.getAsType(luaState, 2, actualParms[0].ParameterType);
object[] args = new object[1];
// Just call the indexer - if out of bounds an exception will happen
args[0] = index;
try
{
object result = getter.Invoke(obj, args);
translator.push(luaState, result);
}
catch (TargetInvocationException e)
{
// Provide a more readable description for the common case of key not found
if (e.InnerException is KeyNotFoundException)
translator.throwError(luaState, "key '" + index + "' not found ");
else
translator.throwError(luaState, "exception indexing '" + index + "' " + e.Message);
LuaDLL.lua_pushnil(luaState);
}
}
}
} }
} }
} }
LuaDLL.lua_pushboolean(luaState, false); LuaDLL.lua_pushboolean(luaState, false);
...@@ -365,6 +387,7 @@ namespace LuaInterface ...@@ -365,6 +387,7 @@ namespace LuaInterface
{ {
// Member type must be 'method' // Member type must be 'method'
LuaCSFunction wrapper = new LuaCSFunction((new LuaMethodWrapper(translator, objType, methodName, bindingType)).call); LuaCSFunction wrapper = new LuaCSFunction((new LuaMethodWrapper(translator, objType, methodName, bindingType)).call);
if (cachedMember == null) setMemberCache(memberCache, objType, methodName, wrapper); if (cachedMember == null) setMemberCache(memberCache, objType, methodName, wrapper);
translator.pushFunction(luaState, wrapper); translator.pushFunction(luaState, wrapper);
translator.push(luaState, true); translator.push(luaState, true);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment