Skip to content

Commit 3930d87

Browse files
committed
Fixed a errors leading to null reference exceptions in the ReflectionHelpers class
1 parent d6ff483 commit 3930d87

File tree

8 files changed

+63
-15
lines changed

8 files changed

+63
-15
lines changed

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"sdk": {
3-
"version": "3.0.100"
3+
"version": "3.0.101"
44
}
55
}

src/MsieJavaScriptEngine/Helpers/ReflectionHelpers.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public static MethodInfo[] GetFullyFledgedMethods(MethodInfo[] methods)
7070

7171
public static void FixFieldValueType(ref object value, FieldInfo field)
7272
{
73+
if (value == null)
74+
{
75+
return;
76+
}
77+
7378
Type valueType = value.GetType();
7479
Type fieldType = field.FieldType;
7580

@@ -86,6 +91,11 @@ public static void FixFieldValueType(ref object value, FieldInfo field)
8691

8792
public static void FixPropertyValueType(ref object value, PropertyInfo property)
8893
{
94+
if (value == null)
95+
{
96+
return;
97+
}
98+
8999
Type valueType = value.GetType();
90100
Type propertyType = property.PropertyType;
91101

@@ -107,6 +117,11 @@ public static void FixArgumentTypes(ref object[] argValues, ParameterInfo[] para
107117
for (int argIndex = 0; argIndex < argCount; argIndex++)
108118
{
109119
object argValue = argValues[argIndex];
120+
if (argValue == null)
121+
{
122+
continue;
123+
}
124+
110125
Type argType = argValue.GetType();
111126

112127
ParameterInfo parameter = parameters[argIndex];
@@ -149,7 +164,7 @@ public static MethodBase GetBestFitMethod(MethodBase[] methods, object[] argValu
149164
}
150165

151166
Type[] argTypes = argValues
152-
.Select(a => a.GetType())
167+
.Select(a => a != null ? a.GetType() : typeof(object))
153168
.ToArray()
154169
;
155170
var compatibleMethods = new List<MethodWithMetadata>();

src/MsieJavaScriptEngine/MsieJavaScriptEngine.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
<IncludeSymbols>true</IncludeSymbols>
2525
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2626
<PackageTags>JavaScript;ECMAScript;MSIE;IE;Edge;Chakra</PackageTags>
27-
<PackageReleaseNotes>1. Fixed a error that caused a crash during finalization;
28-
2. In JsRT modes during calling of the `CollectGarbage` method is again not performed blocking.</PackageReleaseNotes>
27+
<PackageReleaseNotes>Fixed a errors leading to null reference exceptions in the `ReflectionHelpers` class. Special thanks to Vanjoge.</PackageReleaseNotes>
2928
<NeutralLanguage>en-US</NeutralLanguage>
3029
<PackageOutputPath>../../nuget</PackageOutputPath>
3130
<GeneratePackageOnBuild Condition=" '$(Configuration)' == 'Release' ">true</GeneratePackageOnBuild>

src/MsieJavaScriptEngine/readme.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121
=============
2222
RELEASE NOTES
2323
=============
24-
1. Fixed a error that caused a crash during finalization;
25-
2. In JsRT modes during calling of the `CollectGarbage` method is again not
26-
performed blocking.
24+
Fixed a errors leading to null reference exceptions in the `ReflectionHelpers`
25+
class. Special thanks to Vanjoge.
2726

2827
============
2928
PROJECT SITE

test/MsieJavaScriptEngine.Test.Common/Interop/FileManager.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
using System;
22
using System.IO;
3+
using System.Text;
34

45
namespace MsieJavaScriptEngine.Test.Common.Interop
56
{
67
public sealed class FileManager
78
{
89
public string ReadFile(string path)
10+
{
11+
return ReadFile(path, null);
12+
}
13+
14+
public string ReadFile(string path, Encoding encoding)
915
{
1016
if (path == null)
1117
{
1218
throw new ArgumentNullException("path");
1319
}
1420

15-
string content = File.ReadAllText(path);
21+
encoding = encoding ?? Encoding.UTF8;
22+
23+
string content = File.ReadAllText(path, encoding);
1624

1725
return content;
1826
}

test/MsieJavaScriptEngine.Test.Common/Interop/Person.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,26 @@ public string LastName
1414
set;
1515
}
1616

17+
public string Patronymic
18+
{
19+
get;
20+
set;
21+
}
22+
1723

1824
public Person()
1925
: this(string.Empty, string.Empty)
2026
{ }
2127

2228
public Person(string firstName, string lastName)
29+
: this(firstName, lastName, string.Empty)
30+
{ }
31+
32+
public Person(string firstName, string lastName, string patronymic)
2333
{
2434
FirstName = firstName;
2535
LastName = lastName;
36+
Patronymic = patronymic;
2637
}
2738

2839

test/MsieJavaScriptEngine.Test.Common/Interop/Product.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public sealed class Product
44
{
55
public string Name;
6+
public string Description;
67
public double Price;
78
}
89
}

test/MsieJavaScriptEngine.Test.Common/InteropTestsBase.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,33 +69,41 @@ public virtual void EmbeddingOfInstanceOfCustomReferenceTypeWithFieldsIsCorrect(
6969
var product = new Product
7070
{
7171
Name = "Red T-shirt",
72+
Description = string.Empty,
7273
Price = 995.00
7374
};
7475

75-
const string updateCode = "product.Price *= 1.15;";
76+
const string updateCode = @"product.Description = null;
77+
product.Price *= 1.15;";
7678

7779
const string input1 = "product.Name";
7880
const string targetOutput1 = "Red T-shirt";
7981

80-
const string input2 = "product.Price";
81-
const double targetOutput2 = 1144.25;
82+
const string input2 = "product.Description";
83+
const string targetOutput2 = null;
84+
85+
const string input3 = "product.Price";
86+
const double targetOutput3 = 1144.25;
8287

8388
// Act
8489
string output1;
85-
double output2;
90+
string output2;
91+
double output3;
8692

8793
using (var jsEngine = CreateJsEngine())
8894
{
8995
jsEngine.EmbedHostObject("product", product);
9096
jsEngine.Execute(updateCode);
9197

9298
output1 = jsEngine.Evaluate<string>(input1);
93-
output2 = jsEngine.Evaluate<double>(input2);
99+
output2 = jsEngine.Evaluate<string>(input2);
100+
output3 = jsEngine.Evaluate<double>(input3);
94101
}
95102

96103
// Assert
97104
Assert.AreEqual(targetOutput1, output1);
98105
Assert.AreEqual(targetOutput2, output2);
106+
Assert.AreEqual(targetOutput3, output3);
99107
}
100108

101109
#endregion
@@ -218,17 +226,22 @@ public virtual void EmbeddingOfInstanceOfCustomReferenceTypeWithPropertiesIsCorr
218226
{
219227
// Arrange
220228
var person = new Person("Vanya", "Ivanov");
221-
const string updateCode = "person.LastName = person.LastName.substr(0, 5) + 'ff';";
229+
const string updateCode = @"person.LastName = person.LastName.substr(0, 5) + 'ff';
230+
person.Patronymic = null;";
222231

223232
const string input1 = "person.FirstName";
224233
const string targetOutput1 = "Vanya";
225234

226235
const string input2 = "person.LastName";
227236
const string targetOutput2 = "Ivanoff";
228237

238+
const string input3 = "person.Patronymic";
239+
const string targetOutput3 = null;
240+
229241
// Act
230242
string output1;
231243
string output2;
244+
string output3;
232245

233246
using (var jsEngine = CreateJsEngine())
234247
{
@@ -237,11 +250,13 @@ public virtual void EmbeddingOfInstanceOfCustomReferenceTypeWithPropertiesIsCorr
237250

238251
output1 = jsEngine.Evaluate<string>(input1);
239252
output2 = jsEngine.Evaluate<string>(input2);
253+
output3 = jsEngine.Evaluate<string>(input3);
240254
}
241255

242256
// Assert
243257
Assert.AreEqual(targetOutput1, output1);
244258
Assert.AreEqual(targetOutput2, output2);
259+
Assert.AreEqual(targetOutput3, output3);
245260
}
246261

247262
[Test]
@@ -405,7 +420,7 @@ public virtual void EmbeddingOfInstanceOfCustomReferenceTypeWithMethodIsCorrect(
405420
var fileManager = new FileManager();
406421
string filePath = GetAbsolutePath("SharedFiles/link.txt");
407422

408-
string input = string.Format("fileManager.ReadFile('{0}')", filePath.Replace(@"\", @"\\"));
423+
string input = string.Format("fileManager.ReadFile('{0}', null)", filePath.Replace(@"\", @"\\"));
409424
const string targetOutput = "http://www.panopticoncentral.net/2015/09/09/the-two-faces-of-jsrt-in-windows-10/";
410425

411426
// Act

0 commit comments

Comments
 (0)