Make Script conversion at end of execution use Type Binder, general Type Binder fixes

This commit is contained in:
Deukhoofd 2018-12-10 17:52:15 +01:00
parent f1d8904ec9
commit efea92d32a
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
2 changed files with 27 additions and 33 deletions

View File

@ -35,7 +35,6 @@ namespace Upsilon.BaseTypes.UserData
public bool IsValidMatch(MethodBase match, ref object[] args)
{
var validMatch = true;
var parameters = match.GetParameters();
for (var i = 0; i < parameters.Length; i++)
{
@ -54,7 +53,7 @@ namespace Upsilon.BaseTypes.UserData
switch (typeCode)
{
case TypeCode.Boolean:
if (argument is ScriptBoolean b)
if (argument is ScriptBoolean)
{
continue;
}
@ -62,7 +61,7 @@ namespace Upsilon.BaseTypes.UserData
break;
case TypeCode.Char:
case TypeCode.String:
if (argument is ScriptString s)
if (argument is ScriptString)
{
continue;
}
@ -79,7 +78,7 @@ namespace Upsilon.BaseTypes.UserData
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
if (argument is ScriptNumber numeric)
if (argument is ScriptNumber)
{
continue;
}
@ -107,7 +106,7 @@ namespace Upsilon.BaseTypes.UserData
return false;
}
}
return validMatch;
return true;
}
public override object ChangeType(object value, System.Type type, CultureInfo culture)
@ -116,6 +115,13 @@ namespace Upsilon.BaseTypes.UserData
{
return value;
}
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
if (value == null)
// ReSharper disable once HeuristicUnreachableCode
{
// ReSharper disable once HeuristicUnreachableCode
return null;
}
var typeCode = System.Type.GetTypeCode(type);
switch (typeCode)
@ -181,7 +187,7 @@ namespace Upsilon.BaseTypes.UserData
var dictionary = (IDictionary)Activator.CreateInstance(requiredDictionaryType);
foreach (var variable in table.EvaluationScope.Variables)
{
dictionary.Add(variable.Key, variable.Value.ToCSharpObject());
dictionary.Add(variable.Key, ChangeType(variable.Value, requiredDictionaryType, culture));
}
return dictionary;
}
@ -200,12 +206,18 @@ namespace Upsilon.BaseTypes.UserData
var list = (IList)Activator.CreateInstance(requiredListType);
foreach (var variable in table.EvaluationScope.Variables)
{
list.Add(variable.Value.ToCSharpObject());
list.Add(ChangeType(variable.Value, requiredListType, culture));
}
return list;
}
return table.EvaluationScope.Variables.Select(x => x.Value.ToCSharpObject()).ToArray();
else
{
var elementType = type.GetElementType();
var input = table.EvaluationScope.Variables.Select(x => ChangeType(x.Value, elementType, culture)).ToArray();
var array = Array.CreateInstance(elementType, input.Length);
input.CopyTo(array, 0);
return array;
}
}
}

View File

@ -1,8 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Upsilon.BaseTypes;
using Upsilon.BaseTypes.UserData;
using Upsilon.Binder;
using Upsilon.Parser;
using Upsilon.StandardLibraries;
@ -115,34 +117,14 @@ namespace Upsilon.Evaluator
private static T Convert<T>(ScriptType t)
{
if (t == null)
return default(T);
var res = Convert(typeof(T), t);
if (res == null)
return default(T);
if (typeof(IEnumerable).IsAssignableFrom(typeof(T)))
{
if (typeof(T).IsGenericType)
{
var elementType = typeof(T).GetGenericArguments()[0];
var input = (object[])res;
Array array = Array.CreateInstance(elementType, input.Length);
input.CopyTo(array, 0);
res = array;
}
}
return (T)res;
var result = UpsilonBinder.Default.ChangeType(t, typeof(T), CultureInfo.InvariantCulture);
if (result == null) return default(T);
return (T)result;
}
private static object Convert(System.Type type, ScriptType t)
{
if (t == null)
return null;
if (type == typeof(double))
return System.Convert.ToDouble(t.ToCSharpObject());
if (type == typeof(long))
return System.Convert.ToInt64(t.ToCSharpObject());
return t.ToCSharpObject();
return UpsilonBinder.Default.ChangeType(t, type, CultureInfo.InvariantCulture);
}
private static object Convert(ScriptType t)