Skip to content

Commit 85d2d69

Browse files
committed
Fix after review
1 parent c40e903 commit 85d2d69

File tree

3 files changed

+18
-29
lines changed

3 files changed

+18
-29
lines changed

Src/EmbeddedScripts.CSharp.MonoEvaluator/MonoCSharpRunner.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Data;
3-
using System.IO;
1+
using System.IO;
42
using System.Text;
53
using System.Threading;
64
using System.Threading.Tasks;

Src/EmbeddedScripts.JS.ChakraCore/TypeMapper.cs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using System.Collections.Generic;
2+
using System.Collections.Concurrent;
33
using System.Linq;
44
using System.Reflection;
55
using ChakraHost.Hosting;
@@ -9,16 +9,15 @@ namespace EmbeddedScripts.JS.ChakraCore
99
public class TypeMapper : IDisposable
1010
{
1111
private readonly JsContext _context;
12-
12+
1313
// stores registered delegates to prevent their garbage collection
14-
private readonly List<JavaScriptNativeFunction> _jsNativeFunctions = new();
15-
private readonly object _listSynchronizer = new();
14+
private readonly ConcurrentBag<JavaScriptNativeFunction> _jsNativeFunctions = new();
1615

1716
public TypeMapper(JsContext context)
1817
{
1918
_context = context;
2019
}
21-
20+
2221
private JavaScriptValue MapClrPrimitivesToJs(object value)
2322
{
2423
if (value is null)
@@ -32,7 +31,7 @@ private JavaScriptValue MapClrPrimitivesToJs(object value)
3231
case TypeCode.Int16:
3332
case TypeCode.Int32:
3433
return JavaScriptValue.FromInt32(Convert.ToInt32(value));
35-
34+
3635
case TypeCode.UInt32:
3736
case TypeCode.Int64:
3837
case TypeCode.UInt64:
@@ -46,19 +45,19 @@ private JavaScriptValue MapClrPrimitivesToJs(object value)
4645

4746
case TypeCode.String:
4847
return JavaScriptValue.FromString((string) value);
49-
48+
5049
default:
5150
throw new ArgumentException("Type is not supported");
5251
}
5352
}
54-
53+
5554
private object ToNumber(JavaScriptValue value)
5655
{
5756
var num = value.ToDouble();
5857

5958
if (Math.Abs(num - Math.Round(num)) < double.Epsilon)
6059
return (int) num;
61-
60+
6261
return num;
6362
}
6463

@@ -106,26 +105,22 @@ private JavaScriptValue MapDelegate(Delegate func)
106105
return JavaScriptValue.Undefined;
107106
});
108107

109-
lock (_listSynchronizer)
110-
_jsNativeFunctions.Add(callback);
108+
_jsNativeFunctions.Add(callback);
111109

112110
var f = JavaScriptValue.CreateFunction(callback, IntPtr.Zero);
113111

114112
return f;
115113
}
116114

117115
private object Map(JsValue value)
118-
{
119-
object t;
116+
{
120117
using (_context.Scope)
121-
t = MapJsPrimitivesToClr(value);
122-
123-
return t;
118+
return MapJsPrimitivesToClr(value);
124119
}
125-
126-
public T Map<T>(JsValue value) =>
127-
(T)Map(value);
128-
120+
121+
public T Map<T>(JsValue value) =>
122+
(T) Map(value);
123+
129124
public JsValue Map(object value)
130125
{
131126
using (_context.Scope)
@@ -139,8 +134,7 @@ public JsValue Map(object value)
139134

140135
public void Dispose()
141136
{
142-
lock (_listSynchronizer)
143-
_jsNativeFunctions.Clear();
137+
_jsNativeFunctions.Clear();
144138
}
145139
}
146140
}

Tests/EmbeddedScripts.JS.ChakraCore.Tests/ChakraCoreRunnerTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using EmbeddedScripts.Shared.Exceptions;
55
using HelperObjects;
66
using Xunit;
7-
using Xunit.Abstractions;
87

98
namespace EmbeddedScripts.JS.ChakraCore.Tests
109
{
@@ -224,10 +223,8 @@ public async Task RunWithGlobalVariables_Succeed()
224223
[Fact]
225224
public void TryToRegisterUnsupportedType_RunnerThrowsException()
226225
{
227-
var runner = new ChakraCoreRunner();
226+
using var runner = new ChakraCoreRunner();
228227
Assert.Throws<ArgumentException>(() => runner.Register(new HelperObject(), "x"));
229-
230-
runner.Dispose();
231228
}
232229

233230
[Fact]

0 commit comments

Comments
 (0)