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