Skip to content

Commit 43dc6fe

Browse files
committed
In JavaScriptEngineSwitcher.ChakraCore fixed a minor errors
1 parent f86469d commit 43dc6fe

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed

NuGet/JavaScriptEngineSwitcher.ChakraCore/JavaScriptEngineSwitcher.ChakraCore.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This package does not contain the native implementations of ChakraCore. Therefor
2020
* JavaScriptEngineSwitcher.ChakraCore.Native.linux-x64
2121
* JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64</description>
2222
<summary>JavaScriptEngineSwitcher.ChakraCore contains adapter `ChakraCoreJsEngine` (wrapper for the ChakraCore).</summary>
23-
<releaseNotes>ChakraCore was updated to version 1.8.2.</releaseNotes>
23+
<releaseNotes>Fixed a minor errors.</releaseNotes>
2424
<copyright>Copyright (c) 2013-2018 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
2525
<language>en-US</language>
2626
<tags>JavaScriptEngineSwitcher JavaScript ECMAScript ChakraCore</tags>

NuGet/JavaScriptEngineSwitcher.ChakraCore/readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
=============
3131
RELEASE NOTES
3232
=============
33-
ChakraCore was updated to version 1.8.2.
33+
Fixed a minor errors.
3434

3535
=============
3636
DOCUMENTATION

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreJsEngine.cs

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -156,23 +156,33 @@ public ChakraCoreJsEngine(ChakraCoreSettings settings)
156156
_externalObjectFinalizeCallback = ExternalObjectFinalizeCallback;
157157
_promiseContinuationCallback = PromiseContinuationCallback;
158158

159-
_dispatcher.Invoke(() =>
159+
try
160160
{
161-
try
161+
_dispatcher.Invoke(() =>
162162
{
163163
_jsRuntime = JsRuntime.Create(attributes, null);
164164
_jsRuntime.MemoryLimit = settings.MemoryLimit;
165165

166166
_jsContext = _jsRuntime.CreateContext();
167-
_jsContext.AddRef();
168-
}
169-
catch (Exception e)
167+
if (_jsContext.IsValid)
168+
{
169+
_jsContext.AddRef();
170+
}
171+
});
172+
}
173+
catch (Exception e)
174+
{
175+
throw new JsEngineLoadException(
176+
string.Format(CoreStrings.Runtime_JsEngineNotLoaded,
177+
EngineName, e.Message), EngineName, EngineVersion, e);
178+
}
179+
finally
180+
{
181+
if (!_jsContext.IsValid)
170182
{
171-
throw new JsEngineLoadException(
172-
string.Format(CoreStrings.Runtime_JsEngineNotLoaded,
173-
EngineName, e.Message), EngineName, EngineVersion, e);
183+
Dispose();
174184
}
175-
});
185+
}
176186
}
177187

178188
/// <summary>
@@ -249,8 +259,15 @@ private JsScope CreateJsScope()
249259
private static void PromiseContinuationCallback(JsValue task, IntPtr callbackState)
250260
{
251261
task.AddRef();
252-
task.CallFunction(JsValue.GlobalObject);
253-
task.Release();
262+
263+
try
264+
{
265+
task.CallFunction(JsValue.GlobalObject);
266+
}
267+
finally
268+
{
269+
task.Release();
270+
}
254271
}
255272

256273
#region Mapping
@@ -1061,11 +1078,17 @@ protected override object InnerCallFunction(string functionName, params object[]
10611078
.Concat(processedArgs)
10621079
.ToArray()
10631080
;
1064-
resultValue = functionValue.CallFunction(allProcessedArgs);
10651081

1066-
foreach (JsValue processedArg in processedArgs)
1082+
try
10671083
{
1068-
RemoveReferenceToValue(processedArg);
1084+
resultValue = functionValue.CallFunction(allProcessedArgs);
1085+
}
1086+
finally
1087+
{
1088+
foreach (JsValue processedArg in processedArgs)
1089+
{
1090+
RemoveReferenceToValue(processedArg);
1091+
}
10691092
}
10701093
}
10711094
else
@@ -1160,7 +1183,16 @@ protected override void InnerSetVariableValue(string variableName, object value)
11601183
try
11611184
{
11621185
JsValue inputValue = MapToScriptType(value);
1163-
JsValue.GlobalObject.SetProperty(variableName, inputValue, true);
1186+
AddReferenceToValue(inputValue);
1187+
1188+
try
1189+
{
1190+
JsValue.GlobalObject.SetProperty(variableName, inputValue, true);
1191+
}
1192+
finally
1193+
{
1194+
RemoveReferenceToValue(inputValue);
1195+
}
11641196
}
11651197
catch (OriginalJsException e)
11661198
{
@@ -1264,7 +1296,10 @@ private void Dispose(bool disposing)
12641296
{
12651297
_dispatcher.Invoke(() =>
12661298
{
1267-
_jsContext.Release();
1299+
if (_jsContext.IsValid)
1300+
{
1301+
_jsContext.Release();
1302+
}
12681303
_jsRuntime.Dispose();
12691304
});
12701305
_dispatcher.Dispose();

0 commit comments

Comments
 (0)