Skip to content
Permalink
Browse files
Fixed conversion of Lua String -> .NET byte [] + test
  • Loading branch information
viniciusjarina committed Oct 18, 2019
1 parent 321aeb2 commit d8ae863
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
@@ -32,6 +32,7 @@ public CheckType(ObjectTranslator translator)
_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 @@ internal ExtractValue CheckLuaType(LuaState luaState, int stackPos, Type paramTy
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 @@ private object GetAsCharArray(LuaState luaState, int stackPos)
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 LuaFunction = NLua.LuaFunction;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections;
using System.Linq;

// ReSharper disable StringLiteralTypo

@@ -2600,6 +2600,25 @@ public void CallDictionary()
}
}

[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;
}

0 comments on commit d8ae863

Please sign in to comment.