Commit caff04f5 authored by Vinicius Jarina's avatar Vinicius Jarina
Browse files

Fixed build

Added TableToArray method (from MonoLuaInterface)
parent 33608952
...@@ -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,6 +814,46 @@ namespace NLua ...@@ -814,6 +814,46 @@ 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
...@@ -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); object luaParamValue = extractValue (luaState, currentLuaParam);
var paramArrayType = currentNetParam.ParameterType.GetElementType (); var paramArrayType = currentNetParam.ParameterType.GetElementType ();
Array paramArray; Array paramArray = TableToArray (luaParamValue, paramArrayType);
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()) {
#if SILVERLIGHT
paramArray.SetValue (Convert.ChangeType (tableEnumerator.Value, currentNetParam.ParameterType.GetElementType (), System.Globalization.CultureInfo.InvariantCulture), paramArrayIndex);
#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);
......
...@@ -135,37 +135,25 @@ namespace NLua.Method ...@@ -135,37 +135,25 @@ namespace NLua.Method
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);
var paramArrayType = _LastCalledMethod.argTypes [i].paramsArrayType;
Array paramArray;
if (luaParamValue is LuaTable) {
var table = (LuaTable)luaParamValue;
paramArray = Array.CreateInstance (paramArrayType, table.Values.Count);
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; MethodArgs type = _LastCalledMethod.argTypes [i];
object luaParamValue = type.extractValue (luaState, i + 1 + numStackToSkip);
if (_LastCalledMethod.argTypes [i].isParamsArray) {
Array paramArray = _Translator.tableToArray (luaParamValue, type.paramsArrayType);
args [_LastCalledMethod.argTypes [i].index] = paramArray;
} else { } else {
_LastCalledMethod.args [_LastCalledMethod.argTypes [i].index] = args [type.index] = luaParamValue;
_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]]);
} }
......
...@@ -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
} }
} }
......
...@@ -875,5 +875,9 @@ namespace NLua ...@@ -875,5 +875,9 @@ namespace NLua
{ {
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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment