Skip to content

Commit 096931c

Browse files
committed
In JavaScriptEngineSwitcher.ChakraCore an attempt was made to prevent occurrence of the access violation exception in the CallFunction method
1 parent 733ceec commit 096931c

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

NuGet/JavaScriptEngineSwitcher.ChakraCore/JavaScriptEngineSwitcher.ChakraCore.nuspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This package does not contain the native implementations of ChakraCore. Therefor
2020
* JavaScriptEngineSwitcher.ChakraCore.Native.debian-x64
2121
* JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64</description>
2222
<summary>JavaScriptEngineSwitcher.ChakraCore contains adapter `ChakraCoreJsEngine` (wrapper for the ChakraCore).</summary>
23+
<releaseNotes>Attempt to prevent occurrence of the access violation exception in the `CallFunction` method.</releaseNotes>
2324
<copyright>Copyright (c) 2013-2016 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
2425
<language>en-US</language>
2526
<tags>JavaScriptEngineSwitcher JavaScript ECMAScript ChakraCore</tags>

NuGet/JavaScriptEngineSwitcher.ChakraCore/readme.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
* JavaScriptEngineSwitcher.ChakraCore.Native.debian-x64
2828
* JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64
2929

30+
=============
31+
RELEASE NOTES
32+
=============
33+
Attempt to prevent occurrence of the access violation exception in the
34+
`CallFunction` method.
35+
3036
=============
3137
DOCUMENTATION
3238
=============

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreJsEngine.cs

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ private JsValue MapToScriptType(object value)
241241
switch (typeCode)
242242
{
243243
case TypeCode.Boolean:
244-
return JsValue.FromBoolean((bool)value);
244+
return (bool)value ? JsValue.True : JsValue.False;
245245

246246
case TypeCode.SByte:
247247
case TypeCode.Byte:
@@ -335,6 +335,50 @@ private object[] MapToHostType(JsValue[] args)
335335
return args.Select(MapToHostType).ToArray();
336336
}
337337

338+
/// <summary>
339+
/// Adds a reference to the value
340+
/// </summary>
341+
/// <param name="value">The value</param>
342+
private static void AddReferenceToValue(JsValue value)
343+
{
344+
if (CanHaveReferences(value))
345+
{
346+
value.AddRef();
347+
}
348+
}
349+
350+
/// <summary>
351+
/// Removes a reference to the value
352+
/// </summary>
353+
/// <param name="value">The value</param>
354+
private static void RemoveReferenceToValue(JsValue value)
355+
{
356+
if (CanHaveReferences(value))
357+
{
358+
value.Release();
359+
}
360+
}
361+
362+
/// <summary>
363+
/// Checks whether the value can have references
364+
/// </summary>
365+
/// <param name="value">The value</param>
366+
/// <returns>Result of check (true - may have; false - may not have)</returns>
367+
private static bool CanHaveReferences(JsValue value)
368+
{
369+
JsValueType valueType = value.ValueType;
370+
371+
switch (valueType)
372+
{
373+
case JsValueType.Null:
374+
case JsValueType.Undefined:
375+
case JsValueType.Boolean:
376+
return false;
377+
default:
378+
return true;
379+
}
380+
}
381+
338382
private JsValue FromObject(object value)
339383
{
340384
var del = value as Delegate;
@@ -951,11 +995,30 @@ protected override object InnerCallFunction(string functionName, params object[]
951995
string.Format(CoreStrings.Runtime_FunctionNotExist, functionName));
952996
}
953997

954-
var processedArgs = MapToScriptType(args);
955-
var allProcessedArgs = new[] { globalObj }.Concat(processedArgs).ToArray();
956-
998+
JsValue resultValue;
957999
JsValue functionValue = globalObj.GetProperty(functionId);
958-
JsValue resultValue = functionValue.CallFunction(allProcessedArgs);
1000+
1001+
if (args.Length > 0)
1002+
{
1003+
JsValue[] processedArgs = MapToScriptType(args);
1004+
1005+
foreach (JsValue processedArg in processedArgs)
1006+
{
1007+
AddReferenceToValue(processedArg);
1008+
}
1009+
1010+
JsValue[] allProcessedArgs = new[] { globalObj }.Concat(processedArgs).ToArray();
1011+
resultValue = functionValue.CallFunction(allProcessedArgs);
1012+
1013+
foreach (JsValue processedArg in processedArgs)
1014+
{
1015+
RemoveReferenceToValue(processedArg);
1016+
}
1017+
}
1018+
else
1019+
{
1020+
resultValue = functionValue.CallFunction(globalObj);
1021+
}
9591022

9601023
return MapToHostType(resultValue);
9611024
});

0 commit comments

Comments
 (0)