Skip to content

Commit

Permalink
fix some type conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
nilproject committed Aug 2, 2023
1 parent bc98e02 commit 97cb9ce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
12 changes: 11 additions & 1 deletion NiL.JS/Core/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ internal static object ConvertJStoObj(JSValue jsobj, Type targetType, bool hight

if (targetType.GetTypeInfo().IsEnum)
{
#if NET461 || NET48
try
{
return Enum.Parse(targetType, jsobj.Value.ToString());
Expand All @@ -684,12 +685,19 @@ internal static object ConvertJStoObj(JSValue jsobj, Type targetType, bool hight
{
return null;
}
#else
if (Enum.TryParse(targetType, jsobj.Value.ToString(), out object result))
return result;
#endif
}

if (targetType == typeof(Guid))
{
return Guid.Parse(jsobj.Value.ToString());
}

if (targetType == typeof(bool))
return bool.TryParse(jsobj.Value.ToString(), out var result) ? result : null;
}

if (targetType == typeof(string))
Expand Down Expand Up @@ -800,7 +808,9 @@ internal static object ConvertJStoObj(JSValue jsobj, Type targetType, bool hight

if (value is BaseLibrary.Array array)
{
if (hightLoyalty || elementType == typeof(JSValue))
if (hightLoyalty
|| elementType == typeof(JSValue)
|| elementType == typeof(object))
{
if (targetType.IsAssignableFrom(elementType.MakeArrayType()))
{
Expand Down
17 changes: 17 additions & 0 deletions Tests/OverloadedMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public int Method()
return 0;
}

public int Method(int[] a)
{
return 3;
}

public int Method(int a, int b)
{
return 2;
Expand Down Expand Up @@ -64,5 +69,17 @@ public void OverloadedMethods_2()

Assert.AreEqual(2, result.Value);
}

[TestMethod]
public void OverloadedMethods_3()
{
var context = new Context();
var instance = new Class();

context.DefineVariable($"{nameof(instance)}").Assign(Context.CurrentGlobalContext.ProxyValue(instance));
var result = context.Eval($"{nameof(instance)}.{nameof(instance.Method)}([1, 2])");

Assert.AreEqual(3, result.Value);
}
}
}

0 comments on commit 97cb9ce

Please sign in to comment.