Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
NLua
Commits
9662ee82
Commit
9662ee82
authored
Aug 04, 2014
by
Vinicius Jarina
Browse files
[NLua] Fixed #98 Don't use case-insensitive search for members
parent
a679211b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Core/NLua/Metatables.cs
View file @
9662ee82
...
@@ -584,7 +584,7 @@ namespace NLua
...
@@ -584,7 +584,7 @@ namespace NLua
// ie: xmlelement['item'] <- item is a property of xmlelement
// ie: xmlelement['item'] <- item is a property of xmlelement
try
{
try
{
if
(!
string
.
IsNullOrEmpty
(
methodName
)
&&
IsMemberPresent
(
objType
,
methodName
))
if
(!
string
.
IsNullOrEmpty
(
methodName
)
&&
IsMemberPresent
(
objType
,
methodName
))
return
GetMember
(
luaState
,
objType
,
obj
,
methodName
,
BindingFlags
.
Instance
|
BindingFlags
.
IgnoreCase
);
return
GetMember
(
luaState
,
objType
,
obj
,
methodName
,
BindingFlags
.
Instance
);
}
catch
{
}
catch
{
}
}
...
@@ -683,12 +683,12 @@ namespace NLua
...
@@ -683,12 +683,12 @@ namespace NLua
return
2
;
return
2
;
}
}
GetMember
(
luaState
,
obj
.
GetType
(),
obj
,
"__luaInterface_base_"
+
methodName
,
BindingFlags
.
Instance
|
BindingFlags
.
IgnoreCase
);
GetMember
(
luaState
,
obj
.
GetType
(),
obj
,
"__luaInterface_base_"
+
methodName
,
BindingFlags
.
Instance
);
LuaLib
.
LuaSetTop
(
luaState
,
-
2
);
LuaLib
.
LuaSetTop
(
luaState
,
-
2
);
if
(
LuaLib
.
LuaType
(
luaState
,
-
1
)
==
LuaTypes
.
Nil
)
{
if
(
LuaLib
.
LuaType
(
luaState
,
-
1
)
==
LuaTypes
.
Nil
)
{
LuaLib
.
LuaSetTop
(
luaState
,
-
2
);
LuaLib
.
LuaSetTop
(
luaState
,
-
2
);
return
GetMember
(
luaState
,
obj
.
GetType
(),
obj
,
methodName
,
BindingFlags
.
Instance
|
BindingFlags
.
IgnoreCase
);
return
GetMember
(
luaState
,
obj
.
GetType
(),
obj
,
methodName
,
BindingFlags
.
Instance
);
}
}
LuaLib
.
LuaPushBoolean
(
luaState
,
false
);
LuaLib
.
LuaPushBoolean
(
luaState
,
false
);
...
@@ -708,8 +708,7 @@ namespace NLua
...
@@ -708,8 +708,7 @@ namespace NLua
if
(
cachedMember
!=
null
)
if
(
cachedMember
!=
null
)
return
true
;
return
true
;
//CP: Removed NonPublic binding search
var
members
=
objType
.
GetMember
(
methodName
,
BindingFlags
.
Static
|
BindingFlags
.
Instance
|
BindingFlags
.
Public
);
var
members
=
objType
.
GetMember
(
methodName
,
BindingFlags
.
Static
|
BindingFlags
.
Instance
|
BindingFlags
.
Public
|
BindingFlags
.
IgnoreCase
);
return
(
members
.
Length
>
0
);
return
(
members
.
Length
>
0
);
}
}
...
@@ -719,12 +718,11 @@ namespace NLua
...
@@ -719,12 +718,11 @@ namespace NLua
* Uses reflection to find members, and stores the reflected MemberInfo object in
* Uses reflection to find members, and stores the reflected MemberInfo object in
* a cache (indexed by the type of the object and the name of the member).
* a cache (indexed by the type of the object and the name of the member).
*/
*/
private
int
GetMember
(
LuaState
luaState
,
IReflect
objType
,
object
obj
,
string
methodName
,
BindingFlags
bindingType
)
int
GetMember
(
LuaState
luaState
,
IReflect
objType
,
object
obj
,
string
methodName
,
BindingFlags
bindingType
)
{
{
bool
implicitStatic
=
false
;
bool
implicitStatic
=
false
;
MemberInfo
member
=
null
;
MemberInfo
member
=
null
;
object
cachedMember
=
CheckMemberCache
(
memberCache
,
objType
,
methodName
);
object
cachedMember
=
CheckMemberCache
(
memberCache
,
objType
,
methodName
);
//object cachedMember=null;
if
(
cachedMember
is
LuaNativeFunction
)
{
if
(
cachedMember
is
LuaNativeFunction
)
{
translator
.
PushFunction
(
luaState
,
(
LuaNativeFunction
)
cachedMember
);
translator
.
PushFunction
(
luaState
,
(
LuaNativeFunction
)
cachedMember
);
...
@@ -733,16 +731,13 @@ namespace NLua
...
@@ -733,16 +731,13 @@ namespace NLua
}
else
if
(
cachedMember
!=
null
)
}
else
if
(
cachedMember
!=
null
)
member
=
(
MemberInfo
)
cachedMember
;
member
=
(
MemberInfo
)
cachedMember
;
else
{
else
{
//CP: Removed NonPublic binding search
var
members
=
objType
.
GetMember
(
methodName
,
bindingType
|
BindingFlags
.
Public
);
var
members
=
objType
.
GetMember
(
methodName
,
bindingType
|
BindingFlags
.
Public
|
BindingFlags
.
IgnoreCase
/*| BindingFlags.NonPublic*/
);
if
(
members
.
Length
>
0
)
if
(
members
.
Length
>
0
)
member
=
members
[
0
];
member
=
members
[
0
];
else
{
else
{
// If we can't find any suitable instance members, try to find them as statics - but we only want to allow implicit static
// If we can't find any suitable instance members, try to find them as statics - but we only want to allow implicit static
// lookups for fields/properties/events -kevinh
members
=
objType
.
GetMember
(
methodName
,
bindingType
|
BindingFlags
.
Static
|
BindingFlags
.
Public
);
//CP: Removed NonPublic binding search and made case insensitive
members
=
objType
.
GetMember
(
methodName
,
bindingType
|
BindingFlags
.
Static
|
BindingFlags
.
Public
|
BindingFlags
.
IgnoreCase
/*| BindingFlags.NonPublic*/
);
if
(
members
.
Length
>
0
)
{
if
(
members
.
Length
>
0
)
{
member
=
members
[
0
];
member
=
members
[
0
];
...
@@ -901,7 +896,7 @@ namespace NLua
...
@@ -901,7 +896,7 @@ namespace NLua
// First try to look up the parameter as a property name
// First try to look up the parameter as a property name
string
detailMessage
;
string
detailMessage
;
bool
didMember
=
TrySetMember
(
luaState
,
type
,
target
,
BindingFlags
.
Instance
|
BindingFlags
.
IgnoreCase
,
out
detailMessage
);
bool
didMember
=
TrySetMember
(
luaState
,
type
,
target
,
BindingFlags
.
Instance
,
out
detailMessage
);
if
(
didMember
)
if
(
didMember
)
return
0
;
// Must have found the property name
return
0
;
// Must have found the property name
...
@@ -977,8 +972,7 @@ namespace NLua
...
@@ -977,8 +972,7 @@ namespace NLua
// Find our member via reflection or the cache
// Find our member via reflection or the cache
var
member
=
(
MemberInfo
)
CheckMemberCache
(
memberCache
,
targetType
,
fieldName
);
var
member
=
(
MemberInfo
)
CheckMemberCache
(
memberCache
,
targetType
,
fieldName
);
if
(
member
==
null
)
{
if
(
member
==
null
)
{
//CP: Removed NonPublic binding search and made case insensitive
var
members
=
targetType
.
GetMember
(
fieldName
,
bindingType
|
BindingFlags
.
Public
);
var
members
=
targetType
.
GetMember
(
fieldName
,
bindingType
|
BindingFlags
.
Public
|
BindingFlags
.
IgnoreCase
/*| BindingFlags.NonPublic*/
);
if
(
members
.
Length
>
0
)
{
if
(
members
.
Length
>
0
)
{
member
=
members
[
0
];
member
=
members
[
0
];
...
@@ -1086,9 +1080,9 @@ namespace NLua
...
@@ -1086,9 +1080,9 @@ namespace NLua
if
(
string
.
IsNullOrEmpty
(
methodName
))
{
if
(
string
.
IsNullOrEmpty
(
methodName
))
{
LuaLib
.
LuaPushNil
(
luaState
);
LuaLib
.
LuaPushNil
(
luaState
);
return
1
;
return
1
;
}
//CP: Ignore case
}
else
else
return
GetMember
(
luaState
,
klass
,
null
,
methodName
,
BindingFlags
.
FlattenHierarchy
|
BindingFlags
.
Static
|
BindingFlags
.
IgnoreCase
);
return
GetMember
(
luaState
,
klass
,
null
,
methodName
,
BindingFlags
.
FlattenHierarchy
|
BindingFlags
.
Static
);
}
}
}
}
...
@@ -1117,7 +1111,7 @@ namespace NLua
...
@@ -1117,7 +1111,7 @@ namespace NLua
}
else
}
else
target
=
(
IReflect
)
obj
;
target
=
(
IReflect
)
obj
;
return
SetMember
(
luaState
,
target
,
null
,
BindingFlags
.
FlattenHierarchy
|
BindingFlags
.
Static
|
BindingFlags
.
IgnoreCase
);
return
SetMember
(
luaState
,
target
,
null
,
BindingFlags
.
FlattenHierarchy
|
BindingFlags
.
Static
);
}
}
/*
/*
...
...
Core/NLua/Method/LuaMethodWrapper.cs
100644 → 100755
View file @
9662ee82
...
@@ -98,8 +98,7 @@ namespace NLua.Method
...
@@ -98,8 +98,7 @@ namespace NLua.Method
_ExtractTarget
=
translator
.
typeChecker
.
GetExtractor
(
targetType
);
_ExtractTarget
=
translator
.
typeChecker
.
GetExtractor
(
targetType
);
_BindingType
=
bindingType
;
_BindingType
=
bindingType
;
//CP: Removed NonPublic binding search and added IgnoreCase
_Members
=
targetType
.
UnderlyingSystemType
.
GetMember
(
methodName
,
MemberTypes
.
Method
,
bindingType
|
BindingFlags
.
Public
);
_Members
=
targetType
.
UnderlyingSystemType
.
GetMember
(
methodName
,
MemberTypes
.
Method
,
bindingType
|
BindingFlags
.
Public
|
BindingFlags
.
IgnoreCase
/*|BindingFlags.NonPublic*/
);
}
}
/// <summary>
/// <summary>
...
...
Core/NLua/ObjectTranslator.cs
View file @
9662ee82
...
@@ -496,9 +496,8 @@ namespace NLua
...
@@ -496,9 +496,8 @@ namespace NLua
signature
[
i
]
=
FindType
(
LuaLib
.
LuaToString
(
luaState
,
i
+
3
).
ToString
());
signature
[
i
]
=
FindType
(
LuaLib
.
LuaToString
(
luaState
,
i
+
3
).
ToString
());
try
{
try
{
//CP: Added ignore case
var
method
=
klass
.
GetMethod
(
methodName
,
BindingFlags
.
Public
|
BindingFlags
.
Static
|
var
method
=
klass
.
GetMethod
(
methodName
,
BindingFlags
.
Public
|
BindingFlags
.
Static
|
BindingFlags
.
Instance
|
BindingFlags
.
FlattenHierarchy
|
BindingFlags
.
IgnoreCase
,
null
,
signature
,
null
);
BindingFlags
.
Instance
|
BindingFlags
.
FlattenHierarchy
,
null
,
signature
,
null
);
PushFunction
(
luaState
,
new
LuaNativeFunction
((
new
LuaMethodWrapper
(
this
,
target
,
klass
,
method
)).
invokeFunction
));
PushFunction
(
luaState
,
new
LuaNativeFunction
((
new
LuaMethodWrapper
(
this
,
target
,
klass
,
method
)).
invokeFunction
));
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
ThrowError
(
luaState
,
e
);
ThrowError
(
luaState
,
e
);
...
...
tests/LuaTests.cs
View file @
9662ee82
...
@@ -22,13 +22,24 @@ using NUnit.Framework;
...
@@ -22,13 +22,24 @@ using NUnit.Framework;
namespace
NLuaTest
namespace
NLuaTest
{
{
#
if
MONOTOUCH
[
Preserve
(
AllMembers
=
true
)]
#
endif
public
class
TestCaseName
{
public
string
name
=
"name"
;
public
string
Name
{
get
{
return
"**"
+
name
+
"**"
;
}
}
}
[
TestFixture
]
[
TestFixture
]
#
if
MONOTOUCH
#
if
MONOTOUCH
[
Preserve
(
AllMembers
=
true
)]
[
Preserve
(
AllMembers
=
true
)]
#
endif
#
endif
public
class
LuaTests
public
class
LuaTests
{
{
public
static
readonly
char
UnicodeChar
=
'\
uE007
'
;
public
static
readonly
char
UnicodeChar
=
'\
uE007
'
;
public
static
string
UnicodeString
public
static
string
UnicodeString
{
{
...
@@ -2015,6 +2026,28 @@ namespace NLuaTest
...
@@ -2015,6 +2026,28 @@ namespace NLuaTest
Assert
.
AreEqual
(
expected
,
res
);
Assert
.
AreEqual
(
expected
,
res
);
}
}
}
}
[
Test
]
public
void
TestCaseFields
()
{
using
(
Lua
lua
=
new
Lua
())
{
lua
.
LoadCLRPackage
();
lua
.
DoString
(
@" import ('NLuaTest')
x = TestCaseName()
name = x.name;
name2 = x.Name;
Name = x.Name;
Name2 = x.name"
);
Assert
.
AreEqual
(
"name"
,
lua
[
"name"
]);
Assert
.
AreEqual
(
"**name**"
,
lua
[
"name2"
]);
Assert
.
AreEqual
(
"**name**"
,
lua
[
"Name"
]);
Assert
.
AreEqual
(
"name"
,
lua
[
"Name2"
]);
}
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment