Skip to content

Commit c9ba013

Browse files
committed
fix #310
1 parent 510092d commit c9ba013

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

NiL.JS/Extensions/JSValueExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public static T GetDefinedOr<T>(this JSValue self, T defaultValue)
155155

156156
case TypeCode.Int16:
157157
{
158-
return (T)(object)(Int16)Tools.JSObjectToInt32(self);
158+
return (T)(object)(short)Tools.JSObjectToInt32(self);
159159
}
160160

161161
case TypeCode.Int32:
@@ -170,16 +170,16 @@ public static T GetDefinedOr<T>(this JSValue self, T defaultValue)
170170

171171
case TypeCode.Object:
172172
{
173+
if (self._oValue is not null && typeof(T).IsAssignableFrom(self._oValue.GetType()))
174+
return (T)self._oValue;
175+
173176
var value = self.Value;
174177
if (value is null)
175178
return default(T);
176179

177180
if (value is Function && typeof(Delegate).IsAssignableFrom(typeof(T)))
178181
return ((Function)value).MakeDelegate<T>();
179182

180-
if (typeof(T).IsAssignableFrom(self._oValue.GetType()))
181-
return (T)self._oValue;
182-
183183
if (typeof(T).IsAssignableFrom(value.GetType()))
184184
return (T)value;
185185

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using NiL.JS.Core;
6+
using NiL.JS.Extensions;
7+
8+
namespace Tests.Extensions;
9+
10+
[TestClass]
11+
public class JSValueExtensionsTests
12+
{
13+
public static IEnumerable<object[]> TestData
14+
{
15+
get
16+
{
17+
yield return new object[] { (JSValue)true, true };
18+
yield return new object[] { (JSValue)1, 1 };
19+
yield return new object[] { (JSValue)"qwe", "qwe" };
20+
yield return new object[] { (JSValue)1.2, 1.2 };
21+
}
22+
}
23+
24+
[TestMethod]
25+
[DynamicData(nameof(TestData))]
26+
public void ConvertJsValueToClrObjectShouldWork(JSValue value, object expected) => Assert.AreEqual(expected, value.As<object>());
27+
}

0 commit comments

Comments
 (0)