Skip to content

Commit 9e0a284

Browse files
committed
In JavaScriptEngineSwitcher.Vroom fixed a minor errors
1 parent 43dc6fe commit 9e0a284

File tree

3 files changed

+76
-29
lines changed

3 files changed

+76
-29
lines changed

NuGet/JavaScriptEngineSwitcher.Vroom/JavaScriptEngineSwitcher.Vroom.nuspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
For correct working of the VroomJs on Windows require the Visual C++ Redistributable for Visual Studio 2012 and Microsoft Visual C++ 2015 Redistributable.</description>
1616
<summary>JavaScriptEngineSwitcher.Vroom contains adapter `VroomJsEngine` (wrapper for the VroomJs version 1.2.3 with support of V8 version 3.17.16.2).</summary>
17+
<releaseNotes>Fixed a minor errors.</releaseNotes>
1718
<copyright>Copyright (c) 2013-2018 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
1819
<language>en-US</language>
1920
<tags>JavaScriptEngineSwitcher JavaScript ECMAScript VroomJs V8</tags>

NuGet/JavaScriptEngineSwitcher.Vroom/readme.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
--------------------------------------------------------------------------------
77

8-
Copyright (c) 2013-2017 Andrey Taritsyn - http://www.taritsyn.ru
8+
Copyright (c) 2013-2018 Andrey Taritsyn - http://www.taritsyn.ru
99

1010

1111
===========
@@ -22,7 +22,7 @@
2222
=============
2323
RELEASE NOTES
2424
=============
25-
Added support of .NET Core 1.0.4.
25+
Fixed a minor errors.
2626

2727
====================
2828
POST-INSTALL ACTIONS

src/JavaScriptEngineSwitcher.Vroom/VroomJsEngine.cs

Lines changed: 73 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ public sealed class VroomJsEngine : JsEngineBase
5151
/// </summary>
5252
private readonly Dictionary<string, object> _hostItems = new Dictionary<string, object>();
5353

54+
/// <summary>
55+
/// Synchronizer of JS engine initialization
56+
/// </summary>
57+
private static readonly object _initializationSynchronizer = new object();
58+
59+
/// <summary>
60+
/// Flag indicating whether the JS engine is initialized
61+
/// </summary>
62+
private static bool _initialized;
63+
5464
/// <summary>
5565
/// Unique document name manager
5666
/// </summary>
@@ -82,17 +92,6 @@ public override bool SupportsGarbageCollection
8292
}
8393

8494

85-
/// <summary>
86-
/// Static constructor
87-
/// </summary>
88-
static VroomJsEngine()
89-
{
90-
if (Utils.IsWindows())
91-
{
92-
OriginalAssemblyLoader.EnsureLoaded();
93-
}
94-
}
95-
9695
/// <summary>
9796
/// Constructs an instance of adapter for the Vroom JS engine
9897
/// (cross-platform bridge to the V8 JS engine)
@@ -108,23 +107,66 @@ public VroomJsEngine()
108107
/// <param name="settings">Settings of the Vroom JS engine</param>
109108
public VroomJsEngine(VroomSettings settings)
110109
{
110+
Initialize();
111+
111112
VroomSettings vroomSettings = settings ?? new VroomSettings();
112113

113114
try
114115
{
115-
_jsEngine = new OriginalJsEngine(vroomSettings.MaxYoungSpaceSize,
116-
vroomSettings.MaxOldSpaceSize);
116+
_jsEngine = new OriginalJsEngine(vroomSettings.MaxYoungSpaceSize, vroomSettings.MaxOldSpaceSize);
117117
_jsContext = _jsEngine.CreateContext();
118118
}
119119
catch (Exception e)
120120
{
121121
throw new JsEngineLoadException(
122-
string.Format(CoreStrings.Runtime_JsEngineNotLoaded,
123-
EngineName, e.Message), EngineName, EngineVersion, e);
122+
string.Format(CoreStrings.Runtime_JsEngineNotLoaded, EngineName, e.Message),
123+
EngineName, EngineVersion, e);
124+
}
125+
finally
126+
{
127+
if (_jsContext == null)
128+
{
129+
Dispose();
130+
}
124131
}
125132
}
126133

127134

135+
/// <summary>
136+
/// Initializes a JS engine
137+
/// </summary>
138+
private static void Initialize()
139+
{
140+
if (_initialized)
141+
{
142+
return;
143+
}
144+
145+
lock (_initializationSynchronizer)
146+
{
147+
if (_initialized)
148+
{
149+
return;
150+
}
151+
152+
if (Utils.IsWindows())
153+
{
154+
try
155+
{
156+
OriginalAssemblyLoader.EnsureLoaded();
157+
}
158+
catch (Exception e)
159+
{
160+
throw new JsEngineLoadException(
161+
string.Format(CoreStrings.Runtime_JsEngineNotLoaded, EngineName, e.Message),
162+
EngineName, EngineVersion, e);
163+
}
164+
}
165+
166+
_initialized = true;
167+
}
168+
}
169+
128170
/// <summary>
129171
/// Makes a mapping from the host type to a Vroom type
130172
/// </summary>
@@ -235,17 +277,14 @@ protected override void InnerExecute(string code, string documentName)
235277

236278
protected override object InnerCallFunction(string functionName, params object[] args)
237279
{
238-
string serializedArguments = string.Empty;
280+
string functionCallExpression;
239281
int argumentCount = args.Length;
240282

241-
if (argumentCount == 1)
283+
if (argumentCount > 0)
242284
{
243-
object value = args[0];
244-
serializedArguments = SimplisticJsSerializer.Serialize(value);
245-
}
246-
else if (argumentCount > 1)
247-
{
248-
var serializedArgumentsBuilder = new StringBuilder();
285+
var functionCallBuilder = new StringBuilder();
286+
functionCallBuilder.Append(functionName);
287+
functionCallBuilder.Append("(");
249288

250289
for (int argumentIndex = 0; argumentIndex < argumentCount; argumentIndex++)
251290
{
@@ -254,15 +293,22 @@ protected override object InnerCallFunction(string functionName, params object[]
254293

255294
if (argumentIndex > 0)
256295
{
257-
serializedArgumentsBuilder.Append(", ");
296+
functionCallBuilder.Append(", ");
258297
}
259-
serializedArgumentsBuilder.Append(serializedValue);
298+
functionCallBuilder.Append(serializedValue);
260299
}
261300

262-
serializedArguments = serializedArgumentsBuilder.ToString();
301+
functionCallBuilder.Append(");");
302+
303+
functionCallExpression = functionCallBuilder.ToString();
304+
functionCallBuilder.Clear();
305+
}
306+
else
307+
{
308+
functionCallExpression = string.Format("{0}();", functionName);
263309
}
264310

265-
object result = Evaluate(string.Format("{0}({1});", functionName, serializedArguments));
311+
object result = Evaluate(functionCallExpression);
266312

267313
return result;
268314
}

0 commit comments

Comments
 (0)