Skip to content

Commit

Permalink
Fix: Incompatible behaviour of table index access
Browse files Browse the repository at this point in the history
  • Loading branch information
neolithos committed Nov 30, 2016
1 parent db294fe commit 5132484
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
8 changes: 8 additions & 0 deletions NeoLua.Test/LuaTable.cs
Expand Up @@ -168,6 +168,14 @@ public void TestIndex01()
for (int i = 1; i <= 18; i++)
Assert.AreEqual(t[i], i);
Assert.AreEqual(t[20], 20);

Assert.IsNull(t[(object)null]);
Assert.IsNull(t[(string)null]);

try { t[(object)null] = 0; Assert.Fail(); }
catch (ArgumentNullException) { }
try { t[(string)null] = 0; Assert.Fail(); }
catch (ArgumentNullException) { }
}

[TestMethod]
Expand Down
20 changes: 10 additions & 10 deletions NeoLua/LuaTable.cs
Expand Up @@ -362,7 +362,7 @@ public override DynamicMetaObject BindGetIndex(GetIndexBinder binder, DynamicMet

if (arg.Value == null)
{
expr = Lua.ThrowExpression(Properties.Resources.rsTableKeyNotNullable, typeof(object));
expr = Expression.Default(typeof(object));
restrictions = restrictions.Merge(BindingRestrictions.GetInstanceRestriction(arg.Expression, null));
}
else if (IsIndexKey(arg.LimitType)) // integer access
Expand Down Expand Up @@ -1975,23 +1975,23 @@ private bool SetClassMemberValue(int entryIndex, object key, object value, bool
public object GetMemberValue(string memberName, bool ignoreCase = false, bool rawGet = false)
{
if (memberName == null)
throw new ArgumentNullException(Properties.Resources.rsTableKeyNotNullable);
return null;

// find the member
int iEntryIndex = FindKey(memberName, GetMemberHashCode(memberName), ignoreCase ? compareStringIgnoreCase : comparerObject);
if (iEntryIndex < 0)
var entryIndex = FindKey(memberName, GetMemberHashCode(memberName), ignoreCase ? compareStringIgnoreCase : comparerObject);
if (entryIndex < 0)
{
if (rawGet)
return null;
else
return OnIndex(memberName);
}
else if (iEntryIndex < classDefinition.Count)
else if (entryIndex < classDefinition.Count)
{
return GetClassMemberValue(iEntryIndex, memberName, rawGet);
return GetClassMemberValue(entryIndex, memberName, rawGet);
}
else
return entries[iEntryIndex].value;
return entries[entryIndex].value;
} // func GetMemberValue

private object GetClassMemberValue(int iEntryIndex, bool lRawGet)
Expand Down Expand Up @@ -2019,7 +2019,7 @@ private object GetClassMemberValue(int iEntryIndex, object key, bool lRawGet)
public bool ContainsMember(string memberName, bool ignoreCase = false)
{
if (memberName == null)
throw new ArgumentNullException(Properties.Resources.rsTableKeyNotNullable);
return false;

return FindKey(memberName, GetMemberHashCode(memberName), ignoreCase ? compareStringIgnoreCase : compareString) >= 0;
} // func ContainsMember
Expand Down Expand Up @@ -2567,7 +2567,7 @@ public object GetValue(object key, bool rawGet = false)

if (key == null)
{
throw new ArgumentNullException(Properties.Resources.rsTableKeyNotNullable);
return null;
}
else if (IsIndexKey(key, out index))
{
Expand Down Expand Up @@ -2640,7 +2640,7 @@ public bool ContainsKey(object key)
int iIndex;
string sKey;
if (key == null)
throw new ArgumentNullException(Properties.Resources.rsTableKeyNotNullable);
return false;
else if (IsIndexKey(key, out iIndex))
return ContainsIndex(iIndex);
else if ((sKey = (key as string)) != null)
Expand Down

0 comments on commit 5132484

Please sign in to comment.