Support using tables to assign to lists and arrays

This commit is contained in:
Deukhoofd 2019-09-22 12:12:27 +02:00
parent 6b53877aa5
commit b6934d153a
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
5 changed files with 133 additions and 6 deletions

View File

@ -79,6 +79,9 @@ namespace PorygonSharp.EvalValues
return EvaluateString(); return EvaluateString();
case TypeClass.UserData: case TypeClass.UserData:
return EvaluateGenericObject(); return EvaluateGenericObject();
case TypeClass.Table:
var val = new TableEvalValue(_handle);
return val.EvaluateNumericalTable();
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
} }

View File

@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace PorygonSharp.EvalValues
{
internal class TableEvalValue
{
private readonly IntPtr _handle;
public TableEvalValue(IntPtr ptr)
{
_handle = ptr;
}
public enum TableType
{
Unknown,
Numerical,
StringKeyed,
Dictionary
}
public TableType GetTableType()
{
return GetTableType(_handle);
}
public int GetLength()
{
return GetTableLength(_handle);
}
public EvalValue IndexTable(uint hash)
{
var ptr = IndexTableHash(_handle, hash);
return new EvalValue(ptr);
}
public EvalValue IndexTable(EvalValue key)
{
var ptr = IndexTableObj(_handle, key.GetPointer());
return new EvalValue(ptr);
}
public IEnumerator<object> EvaluateNumericalEnumerator()
{
if (GetTableType() != TableType.Numerical)
throw new Exception("Can't evaluate table as array. It's not a numerical table");
var length = GetLength();
for (uint i = 1; i <= length; i++)
{
var val = IndexTable(i);
yield return val.GetObjectValue();
}
}
public object[] EvaluateNumericalTable()
{
var length = GetLength();
var array = new object[length];
var c = 0;
using (var enumerator = EvaluateNumericalEnumerator())
{
while (enumerator.MoveNext())
{
array[c] = enumerator.Current;
c++;
}
}
return array;
}
[DllImport("PorygonLang", EntryPoint = "GetTableType", CallingConvention = CallingConvention.Cdecl)]
private static extern TableType GetTableType(IntPtr ptr);
[DllImport("PorygonLang", EntryPoint = "GetTableLength", CallingConvention = CallingConvention.Cdecl)]
private static extern int GetTableLength(IntPtr ptr);
[DllImport("PorygonLang", EntryPoint = "IndexTableHash", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr IndexTableHash(IntPtr ptr, uint hash);
[DllImport("PorygonLang", EntryPoint = "IndexTableObj", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr IndexTableObj(IntPtr ptr, IntPtr key);
}
}

View File

@ -2,6 +2,7 @@ using System;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using PorygonSharp.EvalValues; using PorygonSharp.EvalValues;
using PorygonSharp.Utilities;
namespace PorygonSharp.UserData namespace PorygonSharp.UserData
{ {
@ -29,7 +30,7 @@ namespace PorygonSharp.UserData
_setter = (ptr, val) => _setter = (ptr, val) =>
{ {
var obj = GCHandle.FromIntPtr(ptr).Target; var obj = GCHandle.FromIntPtr(ptr).Target;
var evalValue = Convert.ChangeType(new EvalValue(val).GetObjectValue(), field.FieldType); var evalValue = Caster.Cast(new EvalValue(val), field.FieldType);
field.SetValue(obj, evalValue); field.SetValue(obj, evalValue);
}; };
} }
@ -51,7 +52,7 @@ namespace PorygonSharp.UserData
_setter = (ptr, val) => _setter = (ptr, val) =>
{ {
var obj = GCHandle.FromIntPtr(ptr).Target; var obj = GCHandle.FromIntPtr(ptr).Target;
var evalValue = Convert.ChangeType(new EvalValue(val).GetObjectValue(), property.PropertyType); var evalValue = Caster.Cast(new EvalValue(val), property.PropertyType);
property.SetValue(obj, evalValue); property.SetValue(obj, evalValue);
}; };
} }

View File

@ -1,4 +1,7 @@
using System; using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using PorygonSharp.EvalValues; using PorygonSharp.EvalValues;
namespace PorygonSharp.Utilities namespace PorygonSharp.Utilities
@ -8,16 +11,47 @@ namespace PorygonSharp.Utilities
public static object Cast(EvalValue val, Type t) public static object Cast(EvalValue val, Type t)
{ {
var eval = val.GetObjectValue(); var eval = val.GetObjectValue();
return Cast(eval, t);
}
private static object Cast(object o, Type t)
{
if (t.IsEnum) if (t.IsEnum)
{ {
var underlying = t.GetEnumUnderlyingType(); var underlying = t.GetEnumUnderlyingType();
return Convert.ChangeType(eval, underlying); return Convert.ChangeType(o, underlying);
} }
if (eval is IConvertible) if (o is IConvertible)
{ {
return Convert.ChangeType(eval, t); return Convert.ChangeType(o, t);
} }
return eval; if (typeof(IList).IsAssignableFrom(t) && o is object[] arr)
{
return CastList(arr, t);
}
return o;
}
private static object CastList(IReadOnlyList<object> o, Type t)
{
IList arr;
Type elementType;
if (t.IsArray)
{
elementType = t.GetElementType();
arr = Array.CreateInstance(elementType, o.Count);
}
else
{
elementType = t.GetGenericArguments()[0];
arr = (IList)Activator.CreateInstance(t, o.Count);
}
for (var i = 0; i < o.Count; i++)
{
arr[i] = Cast(o[i], elementType);
}
return arr;
} }
} }
} }

Binary file not shown.