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
@"local function index(obj,name)
local meta=getmetatable(obj)
local cached=meta.cache[name]
if cached then
if cached ~= nil then
return cached
else
local value,isFunc = get_object_member(obj,name)
if not value then error(isFunc,2) end
if isFunc then
meta.cache[name]=value
end
......@@ -814,7 +814,47 @@ namespace NLua
LuaLib.lua_pushnil (luaState);
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
* if the match was succesful. It it was also returns the information
......@@ -862,29 +902,10 @@ namespace NLua
currentLuaParam++;
} // Type does not match, ignore if the parameter is optional
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()) {
#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);
}
object luaParamValue = extractValue (luaState, currentLuaParam);
var paramArrayType = currentNetParam.ParameterType.GetElementType ();
Array paramArray = TableToArray (luaParamValue, paramArrayType);
paramList.Add (paramArray);
int index = paramList.LastIndexOf (paramArray);
......
......@@ -134,38 +134,26 @@ namespace NLua.Method
//LuaLib.lua_remove(luaState,1); // Pops the receiver
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 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 (!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 {
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;
} else {
_LastCalledMethod.args [_LastCalledMethod.argTypes [i].index] =
_LastCalledMethod.argTypes [i].extractValue (luaState, i + 1 + numStackToSkip);
for (int i = 0; i < _LastCalledMethod.argTypes.Length; i++) {
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 {
args [type.index] = luaParamValue;
}
if (_LastCalledMethod.args [_LastCalledMethod.argTypes [i].index] == null &&
......@@ -231,7 +219,7 @@ namespace NLua.Method
}
} else { // Method from MethodBase instance
if (methodToCall.ContainsGenericParameters) {
/*bool isMethod = */
_Translator.matchParameters (luaState, methodToCall, ref _LastCalledMethod);
if (methodToCall.IsGenericMethodDefinition) {
......@@ -286,7 +274,6 @@ namespace NLua.Method
// Pushes out and ref return values
for (int index = 0; index < _LastCalledMethod.outList.Length; index++) {
nReturnValues++;
//for(int i=0;i<lastCalledMethod.outList.Length;i++)
_Translator.push (luaState, _LastCalledMethod.args [_LastCalledMethod.outList [index]]);
}
......
......@@ -43,12 +43,9 @@ namespace NLua.Method
_cachedMethod = value;
var mi = value as MethodInfo;
if (!mi.IsNull ())
#if SILVERLIGHT
IsReturnVoid = string.Compare(mi.ReturnType.Name, "System.Void", StringComparison.InvariantCulture) == 0;
#else
IsReturnVoid = string.Compare (mi.ReturnType.Name, "System.Void", true) == 0;
#endif
if (!mi.IsNull ()) {
IsReturnVoid = mi.ReturnType == typeof (void);
}
}
}
......
......@@ -874,6 +874,10 @@ namespace NLua
internal bool matchParameters (LuaCore.lua_State luaState, MethodBase method, ref MethodCache 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