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
e60ba816
Commit
e60ba816
authored
Feb 06, 2018
by
Dobosz Dariusz-FJTP67
Browse files
Fix of calling methods with 'params' array parameters when nil (null) is used as first one.
parent
cdab1126
Changes
3
Show whitespace changes
Inline
Side-by-side
Core/NLua/Metatables.cs
View file @
e60ba816
...
@@ -1265,19 +1265,7 @@ namespace NLua
...
@@ -1265,19 +1265,7 @@ namespace NLua
{
{
paramList
.
Add
(
null
);
paramList
.
Add
(
null
);
outList
.
Add
(
paramList
.
LastIndexOf
(
null
));
outList
.
Add
(
paramList
.
LastIndexOf
(
null
));
}
else
if
(
IsTypeCorrect
(
luaState
,
currentLuaParam
,
currentNetParam
,
out
extractValue
))
{
// Type checking
var
value
=
extractValue
(
luaState
,
currentLuaParam
);
paramList
.
Add
(
value
);
int
index
=
paramList
.
LastIndexOf
(
value
);
var
methodArg
=
new
MethodArgs
();
methodArg
.
index
=
index
;
methodArg
.
extractValue
=
extractValue
;
argTypes
.
Add
(
methodArg
);
if
(
currentNetParam
.
ParameterType
.
IsByRef
)
outList
.
Add
(
index
);
currentLuaParam
++;
}
// Type does not match, ignore if the parameter is optional
}
// Type does not match, ignore if the parameter is optional
else
if
(
IsParamsArray
(
luaState
,
nLuaParams
,
currentLuaParam
,
currentNetParam
,
out
extractValue
))
{
else
if
(
IsParamsArray
(
luaState
,
nLuaParams
,
currentLuaParam
,
currentNetParam
,
out
extractValue
))
{
...
@@ -1299,7 +1287,21 @@ namespace NLua
...
@@ -1299,7 +1287,21 @@ namespace NLua
methodArg
.
paramsArrayType
=
paramArrayType
;
methodArg
.
paramsArrayType
=
paramArrayType
;
argTypes
.
Add
(
methodArg
);
argTypes
.
Add
(
methodArg
);
}
else
if
(
IsTypeCorrect
(
luaState
,
currentLuaParam
,
currentNetParam
,
out
extractValue
))
{
// Type checking
var
value
=
extractValue
(
luaState
,
currentLuaParam
);
paramList
.
Add
(
value
);
int
index
=
paramList
.
LastIndexOf
(
value
);
var
methodArg
=
new
MethodArgs
();
methodArg
.
index
=
index
;
methodArg
.
extractValue
=
extractValue
;
argTypes
.
Add
(
methodArg
);
if
(
currentNetParam
.
ParameterType
.
IsByRef
)
outList
.
Add
(
index
);
currentLuaParam
++;
}
else
if
(
currentLuaParam
>
nLuaParams
)
{
// Adds optional parameters
}
else
if
(
currentLuaParam
>
nLuaParams
)
{
// Adds optional parameters
if
(
currentNetParam
.
IsOptional
)
if
(
currentNetParam
.
IsOptional
)
paramList
.
Add
(
currentNetParam
.
DefaultValue
);
paramList
.
Add
(
currentNetParam
.
DefaultValue
);
...
...
tests/LuaTests.cs
View file @
e60ba816
...
@@ -2490,6 +2490,51 @@ namespace NLuaTest
...
@@ -2490,6 +2490,51 @@ namespace NLuaTest
}
}
}
}
[
Test
]
public
void
TestCallMethodWithParamsOptional
()
{
using
(
var
l
=
new
Lua
())
{
l
.
LoadCLRPackage
();
l
.
DoString
(
" import ('NLuaTest','NLuaTest.Mock') "
);
l
.
DoString
(
@"
r = TestClass.MethodWithParams(2, 7, 4)
"
);
int
r
=
(
int
)
l
.
GetNumber
(
"r"
);
Assert
.
AreEqual
(
2
,
r
,
"#1"
);
}
}
[
Test
]
public
void
TestCallMethodWithObjectParams
()
{
using
(
var
l
=
new
Lua
())
{
l
.
LoadCLRPackage
();
l
.
DoString
(
" import ('NLuaTest','NLuaTest.Mock') "
);
l
.
DoString
(
@"
r = TestClass.MethodWithObjectParams(2, nil, 4, 'abc')
"
);
int
r
=
(
int
)
l
.
GetNumber
(
"r"
);
Assert
.
AreEqual
(
4
,
r
,
"#1"
);
}
}
[
Test
]
public
void
TestCallMethodWithObjectParamsAndNilAsFirstArgument
()
{
using
(
var
l
=
new
Lua
())
{
l
.
LoadCLRPackage
();
l
.
DoString
(
" import ('NLuaTest','NLuaTest.Mock') "
);
l
.
DoString
(
@"
r = TestClass.MethodWithObjectParams(nil, 4, 'abc')
"
);
int
r
=
(
int
)
l
.
GetNumber
(
"r"
);
Assert
.
AreEqual
(
3
,
r
,
"#1"
);
}
}
[
Test
]
[
Test
]
public
void
TestConstructorOverload
()
public
void
TestConstructorOverload
()
{
{
...
...
tests/TestLua.cs
View file @
e60ba816
...
@@ -614,6 +614,17 @@ namespace NLuaTest.Mock
...
@@ -614,6 +614,17 @@ namespace NLuaTest.Mock
}
}
return
i
;
return
i
;
}
}
static
public
int
MethodWithObjectParams
(
params
object
[]
others
)
{
int
i
=
0
;
foreach
(
var
val
in
others
)
{
Console
.
WriteLine
(
val
);
i
++;
}
return
i
;
}
}
}
public
class
TestClassWithOverloadedMethod
public
class
TestClassWithOverloadedMethod
...
...
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