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
39c028da
Commit
39c028da
authored
Nov 27, 2014
by
Vinicius Jarina
Browse files
Fixed #125 calling methods with params keyword
parent
627dedea
Changes
3
Hide whitespace changes
Inline
Side-by-side
Core/NLua/Metatables.cs
View file @
39c028da
...
...
@@ -1267,17 +1267,17 @@ namespace NLua
currentLuaParam
++;
}
// Type does not match, ignore if the parameter is optional
else
if
(
IsParamsArray
(
luaState
,
currentLuaParam
,
currentNetParam
,
out
extractValue
))
{
else
if
(
IsParamsArray
(
luaState
,
nLuaParams
,
currentLuaParam
,
currentNetParam
,
out
extractValue
))
{
var
paramArrayType
=
currentNetParam
.
ParameterType
.
GetElementType
();
int
count
=
(
nLuaParams
-
currentLuaParam
)
+
1
;
Type
paramArrayType
=
currentNetParam
.
ParameterType
.
GetElementType
();
Func
<
int
,
object
>
extractDelegate
=
(
currentParam
)
=>
{
currentLuaParam
++;
currentLuaParam
++;
return
extractValue
(
luaState
,
currentParam
);
};
int
count
=
(
nLuaParams
-
currentLuaParam
)
+
1
;
Array
paramArray
=
TableToArray
(
extractDelegate
,
paramArrayType
,
currentLuaParam
,
count
);
paramList
.
Add
(
paramArray
);
int
index
=
paramList
.
LastIndexOf
(
paramArray
);
var
methodArg
=
new
MethodArgs
();
...
...
@@ -1286,6 +1286,7 @@ namespace NLua
methodArg
.
isParamsArray
=
true
;
methodArg
.
paramsArrayType
=
paramArrayType
;
argTypes
.
Add
(
methodArg
);
}
else
if
(
currentLuaParam
>
nLuaParams
)
{
// Adds optional parameters
if
(
currentNetParam
.
IsOptional
)
...
...
@@ -1333,11 +1334,14 @@ namespace NLua
}
}
private
bool
IsParamsArray
(
LuaState
luaState
,
int
currentLuaParam
,
ParameterInfo
currentNetParam
,
out
ExtractValue
extractValue
)
private
bool
IsParamsArray
(
LuaState
luaState
,
int
nLuaParams
,
int
currentLuaParam
,
ParameterInfo
currentNetParam
,
out
ExtractValue
extractValue
)
{
extractValue
=
null
;
bool
isParamArray
=
false
;
if
(
currentNetParam
.
GetCustomAttributes
(
typeof
(
ParamArrayAttribute
),
false
).
Any
())
{
isParamArray
=
nLuaParams
<
currentLuaParam
;
LuaTypes
luaType
;
try
{
...
...
@@ -1374,8 +1378,7 @@ namespace NLua
}
}
Debug
.
WriteLine
(
"Type wasn't Params object."
);
return
false
;
return
isParamArray
;
}
}
}
\ No newline at end of file
tests/LuaTests.cs
View file @
39c028da
...
...
@@ -2356,6 +2356,20 @@ namespace NLuaTest
}
}
[
Test
]
public
void
TestCallMethodWithParams2
()
{
using
(
var
l
=
new
Lua
())
{
l
.
LoadCLRPackage
();
l
.
DoString
(
" import ('NLuaTest','NLuaTest.Mock') "
);
l
.
DoString
(
@"
r = TestClass.MethodWithParams(2)
"
);
int
r
=
(
int
)
l
.
GetNumber
(
"r"
);
Assert
.
AreEqual
(
0
,
r
,
"#1"
);
}
}
static
Lua
m_lua
;
}
...
...
tests/TestLua.cs
View file @
39c028da
...
...
@@ -586,6 +586,16 @@ namespace NLuaTest.Mock
}
Console
.
WriteLine
(
output
);
}
static
public
int
MethodWithParams
(
int
a
,
params
int
[]
others
)
{
Console
.
WriteLine
(
a
);
int
i
=
0
;
foreach
(
int
val
in
others
)
{
Console
.
WriteLine
(
val
);
i
++;
}
return
i
;
}
}
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