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

Fixed conversion of Lua String -> .NET `byte []` + test

parent 321aeb27
......@@ -32,6 +32,7 @@ namespace NLua
_extractValues.Add(typeof(bool), GetAsBoolean);
_extractValues.Add(typeof(string), GetAsString);
_extractValues.Add(typeof(char[]), GetAsCharArray);
_extractValues.Add(typeof(byte[]), GetAsByteArray);
_extractValues.Add(typeof(LuaFunction), GetAsFunction);
_extractValues.Add(typeof(LuaTable), GetAsTable);
_extractValues.Add(typeof(LuaUserData), GetAsUserdata);
......@@ -113,7 +114,7 @@ namespace NLua
if (luatype == LuaType.Number)
return _extractValues[typeof(double)];
}
bool netParamIsString = paramType == typeof(string) || paramType == typeof(char[]);
bool netParamIsString = paramType == typeof(string) || paramType == typeof(char[]) || paramType == typeof(byte[]);
if (netParamIsNumeric)
{
......@@ -329,6 +330,15 @@ namespace NLua
return retVal.ToCharArray();
}
private object GetAsByteArray(LuaState luaState, int stackPos)
{
if (!luaState.IsString(stackPos))
return null;
byte [] retVal = luaState.ToBuffer(stackPos, false);
return retVal;
}
private object GetAsString(LuaState luaState, int stackPos)
{
if (!luaState.IsString(stackPos))
......
......@@ -16,7 +16,7 @@ using Lua = NLua.Lua;
using LuaFunction = NLua.LuaFunction;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
// ReSharper disable StringLiteralTypo
......@@ -2600,6 +2600,25 @@ namespace NLuaTest
}
}
[Test]
public void ByteArrayParameter()
{
using (var lua = new Lua())
{
lua["WriteBinary"] = (Action<byte[]>)WriteBinary;
lua.DoString(@"
local value = string.char(1, 2, 3, 0x3f, 0x40, 0xff, 0xf3, 0x9f)
WriteBinary (value);
");
}
}
private void WriteBinary(byte [] buffer)
{
byte[] expected = { 1, 2, 3, 0x3f, 0x40, 0xff, 0xf3, 0x9f };
Assert.True(Enumerable.SequenceEqual(expected, buffer));
}
static Lua m_lua;
}
......
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