Special UserData types for ILists(includes arrays) and IDictionaries

This commit is contained in:
2018-11-21 14:49:59 +01:00
parent 1d24be85d6
commit 105c40bc05
11 changed files with 242 additions and 24 deletions

View File

@@ -0,0 +1,59 @@
using Upsilon.Evaluator;
using Upsilon.Text;
namespace Upsilon.BaseTypes.UserData
{
internal class GenericUserData : LuaType, IUserData
{
public GenericUserData(object dictionary)
{
Value = dictionary;
_typeInfo = UserDataTypeHandler.GetTypeInfo(dictionary.GetType());
}
public override Type Type => Type.UserData;
protected virtual object Value { get; }
private readonly UserDataType _typeInfo;
public override object ToCSharpObject()
{
return Value;
}
public override System.Type GetCSharpType()
{
return Value.GetType();
}
public virtual LuaType Get(Diagnostics diagnostics, TextSpan span, LuaType index, EvaluationScope scope)
{
var s = index.ToCSharpObject().ToString();
var (type, failed, error) = _typeInfo.Get(Value, s);
if (failed)
{
diagnostics.LogError(error, span);
}
return type;
}
public virtual void Set(Diagnostics diagnostics, TextSpan span, LuaType index, LuaType value)
{
var s = index.ToCSharpObject().ToString();
var (failed, error) = _typeInfo.Set(Value, s, value);
if (failed)
{
diagnostics.LogError(error, span);
}
}
public (LuaType Type, bool Failed) BinaryOperator(LuaType par1, OperatorType op, LuaType par2)
{
return _typeInfo.BinaryOperator(Value, par1, op, par2);
}
public (LuaType Type, bool Failed) UnaryOperator(LuaType par1, OperatorType op)
{
return _typeInfo.UnaryOperator(Value, par1, op);
}
}
}