Special UserData types for ILists(includes arrays) and IDictionaries
This commit is contained in:
59
Upsilon/BaseTypes/UserData/GenericUserData.cs
Normal file
59
Upsilon/BaseTypes/UserData/GenericUserData.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user