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
caff04f5
Commit
caff04f5
authored
May 26, 2013
by
Vinicius Jarina
Browse files
Fixed build
Added TableToArray method (from MonoLuaInterface)
parent
33608952
Changes
4
Hide whitespace changes
Inline
Side-by-side
Core/NLua/Metatables.cs
View file @
caff04f5
...
@@ -61,11 +61,11 @@ namespace NLua
...
@@ -61,11 +61,11 @@ namespace NLua
@"local function index(obj,name)
@"local function index(obj,name)
local meta=getmetatable(obj)
local meta=getmetatable(obj)
local cached=meta.cache[name]
local cached=meta.cache[name]
if cached then
if cached
~= nil
then
return cached
return cached
else
else
local value,isFunc = get_object_member(obj,name)
local value,isFunc = get_object_member(obj,name)
if not value then error(isFunc,2) end
if isFunc then
if isFunc then
meta.cache[name]=value
meta.cache[name]=value
end
end
...
@@ -814,7 +814,47 @@ namespace NLua
...
@@ -814,7 +814,47 @@ namespace NLua
LuaLib
.
lua_pushnil
(
luaState
);
LuaLib
.
lua_pushnil
(
luaState
);
return
1
;
return
1
;
}
}
private
static
bool
IsInteger
(
double
x
)
{
return
Math
.
Ceiling
(
x
)
==
x
;
}
internal
Array
TableToArray
(
object
luaParamValue
,
Type
paramArrayType
)
{
Array
paramArray
;
if
(
luaParamValue
is
LuaTable
)
{
LuaTable
table
=
(
LuaTable
)
luaParamValue
;
IDictionaryEnumerator
tableEnumerator
=
table
.
GetEnumerator
();
tableEnumerator
.
Reset
();
paramArray
=
Array
.
CreateInstance
(
paramArrayType
,
table
.
Values
.
Count
);
int
paramArrayIndex
=
0
;
while
(
tableEnumerator
.
MoveNext
())
{
object
value
=
tableEnumerator
.
Value
;
if
(
paramArrayType
==
typeof
(
object
))
{
if
(
value
!=
null
&&
value
.
GetType
()
==
typeof
(
double
)
&&
IsInteger
((
double
)
value
))
value
=
Convert
.
ToInt32
((
double
)
value
);
}
#if SILVERLIGHT
paramArray
.
SetValue
(
Convert
.
ChangeType
(
value
,
paramArrayType
,
System
.
Globalization
.
CultureInfo
.
InvariantCulture
),
paramArrayIndex
);
#else
paramArray
.
SetValue
(
Convert
.
ChangeType
(
value
,
paramArrayType
),
paramArrayIndex
);
#endif
paramArrayIndex
++;
}
}
else
{
paramArray
=
Array
.
CreateInstance
(
paramArrayType
,
1
);
paramArray
.
SetValue
(
luaParamValue
,
0
);
}
return
paramArray
;
}
/*
/*
* Matches a method against its arguments in the Lua stack. Returns
* Matches a method against its arguments in the Lua stack. Returns
* if the match was succesful. It it was also returns the information
* if the match was succesful. It it was also returns the information
...
@@ -862,29 +902,10 @@ namespace NLua
...
@@ -862,29 +902,10 @@ namespace NLua
currentLuaParam
++;
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
,
currentLuaParam
,
currentNetParam
,
out
extractValue
))
{
else
if
(
_IsParamsArray
(
luaState
,
currentLuaParam
,
currentNetParam
,
out
extractValue
))
{
object
luaParamValue
=
extractValue
(
luaState
,
currentLuaParam
);
var
paramArrayType
=
currentNetParam
.
ParameterType
.
GetElementType
();
Array
paramArray
;
if
(
luaParamValue
is
LuaTable
)
{
var
table
=
(
LuaTable
)
luaParamValue
;
var
tableEnumerator
=
table
.
GetEnumerator
();
paramArray
=
Array
.
CreateInstance
(
paramArrayType
,
table
.
Values
.
Count
);
tableEnumerator
.
Reset
();
int
paramArrayIndex
=
0
;
while
(
tableEnumerator
.
MoveNext
())
{
object
luaParamValue
=
extractValue
(
luaState
,
currentLuaParam
);
#if SILVERLIGHT
var
paramArrayType
=
currentNetParam
.
ParameterType
.
GetElementType
();
paramArray
.
SetValue
(
Convert
.
ChangeType
(
tableEnumerator
.
Value
,
currentNetParam
.
ParameterType
.
GetElementType
(),
System
.
Globalization
.
CultureInfo
.
InvariantCulture
),
paramArrayIndex
);
Array
paramArray
=
TableToArray
(
luaParamValue
,
paramArrayType
);
#else
paramArray
.
SetValue
(
Convert
.
ChangeType
(
tableEnumerator
.
Value
,
currentNetParam
.
ParameterType
.
GetElementType
()),
paramArrayIndex
);
#endif
paramArrayIndex
++;
}
}
else
{
paramArray
=
Array
.
CreateInstance
(
paramArrayType
,
1
);
paramArray
.
SetValue
(
luaParamValue
,
0
);
}
paramList
.
Add
(
paramArray
);
paramList
.
Add
(
paramArray
);
int
index
=
paramList
.
LastIndexOf
(
paramArray
);
int
index
=
paramList
.
LastIndexOf
(
paramArray
);
...
...
Core/NLua/Method/LuaMethodWrapper.cs
View file @
caff04f5
...
@@ -134,38 +134,26 @@ namespace NLua.Method
...
@@ -134,38 +134,26 @@ namespace NLua.Method
//LuaLib.lua_remove(luaState,1); // Pops the receiver
//LuaLib.lua_remove(luaState,1); // Pops the receiver
if
(!
_LastCalledMethod
.
cachedMethod
.
IsNull
())
{
// Cached?
if
(!
_LastCalledMethod
.
cachedMethod
.
IsNull
())
{
// Cached?
int
numStackToSkip
=
isStatic
?
0
:
1
;
// If this is an instance invoe we will have an extra arg on the stack for the targetObject
int
numStackToSkip
=
isStatic
?
0
:
1
;
// If this is an instance invoe we will have an extra arg on the stack for the targetObject
int
numArgsPassed
=
LuaLib
.
lua_gettop
(
luaState
)
-
numStackToSkip
;
int
numArgsPassed
=
LuaLib
.
lua_gettop
(
luaState
)
-
numStackToSkip
;
MethodBase
method
=
_LastCalledMethod
.
cachedMethod
;
if
(
numArgsPassed
==
_LastCalledMethod
.
argTypes
.
Length
)
{
// No. of args match?
if
(
numArgsPassed
==
_LastCalledMethod
.
argTypes
.
Length
)
{
// No. of args match?
if
(!
LuaLib
.
lua_checkstack
(
luaState
,
_LastCalledMethod
.
outList
.
Length
+
6
))
if
(!
LuaLib
.
lua_checkstack
(
luaState
,
_LastCalledMethod
.
outList
.
Length
+
6
))
throw
new
LuaException
(
"Lua stack overflow"
);
throw
new
LuaException
(
"Lua stack overflow"
);
object
[]
args
=
_LastCalledMethod
.
args
;
try
{
try
{
for
(
int
i
=
0
;
i
<
_LastCalledMethod
.
argTypes
.
Length
;
i
++)
{
for
(
int
i
=
0
;
i
<
_LastCalledMethod
.
argTypes
.
Length
;
i
++)
{
if
(
_LastCalledMethod
.
argTypes
[
i
].
isParamsArray
)
{
object
luaParamValue
=
_LastCalledMethod
.
argTypes
[
i
].
extractValue
(
luaState
,
i
+
1
+
numStackToSkip
);
MethodArgs
type
=
_LastCalledMethod
.
argTypes
[
i
];
var
paramArrayType
=
_LastCalledMethod
.
argTypes
[
i
].
paramsArrayType
;
object
luaParamValue
=
type
.
extractValue
(
luaState
,
i
+
1
+
numStackToSkip
);
Array
paramArray
;
if
(
_LastCalledMethod
.
argTypes
[
i
].
isParamsArray
)
{
if
(
luaParamValue
is
LuaTable
)
{
Array
paramArray
=
_Translator
.
tableToArray
(
luaParamValue
,
type
.
paramsArrayType
);
var
table
=
(
LuaTable
)
luaParamValue
;
args
[
_LastCalledMethod
.
argTypes
[
i
].
index
]
=
paramArray
;
paramArray
=
Array
.
CreateInstance
(
paramArrayType
,
table
.
Values
.
Count
);
}
else
{
args
[
type
.
index
]
=
luaParamValue
;
for
(
int
x
=
1
;
x
<=
table
.
Values
.
Count
;
x
++)
#if SILVERLIGHT
paramArray
.
SetValue
(
Convert
.
ChangeType
(
table
[
x
],
paramArrayType
,
System
.
Globalization
.
CultureInfo
.
InvariantCulture
),
x
-
1
);
#else
paramArray
.
SetValue
(
Convert
.
ChangeType
(
table
[
x
],
paramArrayType
),
x
-
1
);
#endif
}
else
{
paramArray
=
Array
.
CreateInstance
(
paramArrayType
,
1
);
paramArray
.
SetValue
(
luaParamValue
,
0
);
}
_LastCalledMethod
.
args
[
_LastCalledMethod
.
argTypes
[
i
].
index
]
=
paramArray
;
}
else
{
_LastCalledMethod
.
args
[
_LastCalledMethod
.
argTypes
[
i
].
index
]
=
_LastCalledMethod
.
argTypes
[
i
].
extractValue
(
luaState
,
i
+
1
+
numStackToSkip
);
}
}
if
(
_LastCalledMethod
.
args
[
_LastCalledMethod
.
argTypes
[
i
].
index
]
==
null
&&
if
(
_LastCalledMethod
.
args
[
_LastCalledMethod
.
argTypes
[
i
].
index
]
==
null
&&
...
@@ -231,7 +219,7 @@ namespace NLua.Method
...
@@ -231,7 +219,7 @@ namespace NLua.Method
}
}
}
else
{
// Method from MethodBase instance
}
else
{
// Method from MethodBase instance
if
(
methodToCall
.
ContainsGenericParameters
)
{
if
(
methodToCall
.
ContainsGenericParameters
)
{
/*bool isMethod = */
_Translator
.
matchParameters
(
luaState
,
methodToCall
,
ref
_LastCalledMethod
);
_Translator
.
matchParameters
(
luaState
,
methodToCall
,
ref
_LastCalledMethod
);
if
(
methodToCall
.
IsGenericMethodDefinition
)
{
if
(
methodToCall
.
IsGenericMethodDefinition
)
{
...
@@ -286,7 +274,6 @@ namespace NLua.Method
...
@@ -286,7 +274,6 @@ namespace NLua.Method
// Pushes out and ref return values
// Pushes out and ref return values
for
(
int
index
=
0
;
index
<
_LastCalledMethod
.
outList
.
Length
;
index
++)
{
for
(
int
index
=
0
;
index
<
_LastCalledMethod
.
outList
.
Length
;
index
++)
{
nReturnValues
++;
nReturnValues
++;
//for(int i=0;i<lastCalledMethod.outList.Length;i++)
_Translator
.
push
(
luaState
,
_LastCalledMethod
.
args
[
_LastCalledMethod
.
outList
[
index
]]);
_Translator
.
push
(
luaState
,
_LastCalledMethod
.
args
[
_LastCalledMethod
.
outList
[
index
]]);
}
}
...
...
Core/NLua/Method/MethodCache.cs
View file @
caff04f5
...
@@ -43,12 +43,9 @@ namespace NLua.Method
...
@@ -43,12 +43,9 @@ namespace NLua.Method
_cachedMethod
=
value
;
_cachedMethod
=
value
;
var
mi
=
value
as
MethodInfo
;
var
mi
=
value
as
MethodInfo
;
if
(!
mi
.
IsNull
())
if
(!
mi
.
IsNull
())
{
#if SILVERLIGHT
IsReturnVoid
=
mi
.
ReturnType
==
typeof
(
void
);
IsReturnVoid
=
string
.
Compare
(
mi
.
ReturnType
.
Name
,
"System.Void"
,
StringComparison
.
InvariantCulture
)
==
0
;
}
#else
IsReturnVoid
=
string
.
Compare
(
mi
.
ReturnType
.
Name
,
"System.Void"
,
true
)
==
0
;
#endif
}
}
}
}
...
...
Core/NLua/ObjectTranslator.cs
View file @
caff04f5
...
@@ -874,6 +874,10 @@ namespace NLua
...
@@ -874,6 +874,10 @@ namespace NLua
internal
bool
matchParameters
(
LuaCore
.
lua_State
luaState
,
MethodBase
method
,
ref
MethodCache
methodCache
)
internal
bool
matchParameters
(
LuaCore
.
lua_State
luaState
,
MethodBase
method
,
ref
MethodCache
methodCache
)
{
{
return
metaFunctions
.
matchParameters
(
luaState
,
method
,
ref
methodCache
);
return
metaFunctions
.
matchParameters
(
luaState
,
method
,
ref
methodCache
);
}
internal
Array
tableToArray
(
object
luaParamValue
,
Type
paramArrayType
)
{
return
metaFunctions
.
TableToArray
(
luaParamValue
,
paramArrayType
);
}
}
}
}
}
}
\ No newline at end of file
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