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

Fixed integer parameters conversion to method arguments.

* Release 1.4.12
parent f8d21ae9
using System;
using System;
using System.Collections.Generic;
using KeraLua;
using NLua.Method;
......@@ -186,38 +186,46 @@ namespace NLua
*/
private object GetAsSbyte(LuaState luaState, int stackPos)
{
sbyte retVal = (sbyte)luaState.ToNumber(stackPos);
if (retVal == 0 && !luaState.IsNumber(stackPos))
if (!luaState.IsNumericType(stackPos))
return null;
return retVal;
if (luaState.IsInteger(stackPos))
return (sbyte)luaState.ToInteger(stackPos);
return (sbyte)luaState.ToNumber(stackPos);
}
private object GetAsByte(LuaState luaState, int stackPos)
{
byte retVal = (byte)luaState.ToNumber(stackPos);
if (retVal == 0 && !luaState.IsNumber(stackPos))
if (!luaState.IsNumericType(stackPos))
return null;
return retVal;
if (luaState.IsInteger(stackPos))
return (byte)luaState.ToInteger(stackPos);
return (byte)luaState.ToNumber(stackPos);
}
private object GetAsShort(LuaState luaState, int stackPos)
{
short retVal = (short)luaState.ToNumber(stackPos);
if (retVal == 0 && !luaState.IsNumber(stackPos))
if (!luaState.IsNumericType(stackPos))
return null;
return retVal;
if (luaState.IsInteger(stackPos))
return (short)luaState.ToInteger(stackPos);
return (short)luaState.ToNumber(stackPos);
}
private object GetAsUshort(LuaState luaState, int stackPos)
{
ushort retVal = (ushort)luaState.ToNumber(stackPos);
if (retVal == 0 && !luaState.IsNumericType(stackPos))
if (!luaState.IsNumericType(stackPos))
return null;
return retVal;
if (luaState.IsInteger(stackPos))
return (ushort)luaState.ToInteger(stackPos);
return (ushort)luaState.ToNumber(stackPos);
}
private object GetAsInt(LuaState luaState, int stackPos)
......@@ -225,71 +233,87 @@ namespace NLua
if (!luaState.IsNumericType(stackPos))
return null;
int retVal = (int)luaState.ToNumber(stackPos);
return retVal;
if (luaState.IsInteger(stackPos))
return (int)luaState.ToInteger(stackPos);
return (int)luaState.ToNumber(stackPos);
}
private object GetAsUint(LuaState luaState, int stackPos)
{
uint retVal = (uint)luaState.ToNumber(stackPos);
if (retVal == 0 && !luaState.IsNumericType(stackPos))
if (!luaState.IsNumericType(stackPos))
return null;
return retVal;
if (luaState.IsInteger(stackPos))
return (uint)luaState.ToInteger(stackPos);
return (uint)luaState.ToNumber(stackPos);
}
private object GetAsLong(LuaState luaState, int stackPos)
{
long retVal = (long)luaState.ToNumber(stackPos);
if (retVal == 0 && !luaState.IsNumericType(stackPos))
if (!luaState.IsNumericType(stackPos))
return null;
return retVal;
if (luaState.IsInteger(stackPos))
return luaState.ToInteger(stackPos);
return (long)luaState.ToNumber(stackPos);
}
private object GetAsUlong(LuaState luaState, int stackPos)
{
ulong retVal = (ulong)luaState.ToNumber(stackPos);
if (retVal == 0 && !luaState.IsNumericType(stackPos))
if (!luaState.IsNumericType(stackPos))
return null;
return retVal;
if (luaState.IsInteger(stackPos))
return (ulong)luaState.ToInteger(stackPos);
return (ulong)luaState.ToNumber(stackPos);
}
private object GetAsDouble(LuaState luaState, int stackPos)
{
double retVal = luaState.ToNumber(stackPos);
if (retVal == 0 && !luaState.IsNumericType(stackPos))
if (!luaState.IsNumericType(stackPos))
return null;
return retVal;
if (luaState.IsInteger(stackPos))
return (double)luaState.ToInteger(stackPos);
return luaState.ToNumber(stackPos);
}
private object GetAsChar(LuaState luaState, int stackPos)
{
char retVal = (char)luaState.ToNumber(stackPos);
if (retVal == 0 && !luaState.IsNumericType(stackPos))
if (!luaState.IsNumericType(stackPos))
return null;
return retVal;
if (luaState.IsInteger(stackPos))
return (char)luaState.ToInteger(stackPos);
return (char)luaState.ToNumber(stackPos);
}
private object GetAsFloat(LuaState luaState, int stackPos)
{
float retVal = (float)luaState.ToNumber(stackPos);
if (retVal == 0 && !luaState.IsNumericType(stackPos))
if (!luaState.IsNumericType(stackPos))
return null;
return retVal;
if (luaState.IsInteger(stackPos))
return (float)luaState.ToInteger(stackPos);
return (float)luaState.ToNumber(stackPos);
}
private object GetAsDecimal(LuaState luaState, int stackPos)
{
decimal retVal = (decimal)luaState.ToNumber(stackPos);
if (retVal == 0 && !luaState.IsNumericType(stackPos))
if (!luaState.IsNumericType(stackPos))
return null;
return retVal;
if (luaState.IsInteger(stackPos))
return (decimal)luaState.ToInteger(stackPos);
return (decimal)luaState.ToNumber(stackPos);
}
private object GetAsBoolean(LuaState luaState, int stackPos)
......
......@@ -2428,7 +2428,7 @@ namespace NLuaTest
[Test]
public void PassIntegerToLua()
{
long x = 0x70DEC0DEC0DEC0DE;
long x = 0x7FFFC0DEC0DEC0DE;
using (var lua = new Lua())
{
......@@ -2436,12 +2436,37 @@ namespace NLuaTest
long l = lua.GetLong("x");
Assert.AreEqual(x, l);
Assert.AreEqual(x, l, "#1");
lua.DoString("y = x");
l = lua.GetLong("y");
Assert.AreEqual(x, l, "#1.1");
var testObj = new TestClass();
lua["test"] = testObj;
lua.DoString("test:MethodWithLong(x)");
Assert.AreEqual(x, testObj.LongValue, "#2");
lua.DoString("test:MethodWithLong(0x7FFFC0DEC0DECAFF)");
Assert.AreEqual(0x7FFFC0DEC0DECAFF, testObj.LongValue, "#2.2");
lua.DoString("y = test:MethodWithLong(0x7FFFC0DECADECAFF)");
Assert.AreEqual(0x7FFFC0DECADECAFF, lua.GetLong("y"), "#2.3");
}
}
static Lua m_lua;
}
}
......@@ -8,6 +8,8 @@ namespace NLuaTest.TestTypes
public int val;
private string strVal;
public long LongValue { get; set; }
public TestClass()
{
val = 0;
......@@ -312,7 +314,7 @@ namespace NLuaTest.TestTypes
Console.WriteLine(output);
}
static public int MethodWithParams(int a, params int[] others)
public static int MethodWithParams(int a, params int[] others)
{
Console.WriteLine(a);
int i = 0;
......@@ -324,7 +326,7 @@ namespace NLuaTest.TestTypes
return i;
}
static public int MethodWithObjectParams(params object[] others)
public static int MethodWithObjectParams(params object[] others)
{
int i = 0;
foreach (var val in others)
......@@ -334,5 +336,12 @@ namespace NLuaTest.TestTypes
}
return i;
}
public long MethodWithLong(long param)
{
LongValue = param;
return param;
}
}
}
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