Skip to content

Commit d6b6665

Browse files
committed
Fixed a error “Out of stack space”
1 parent b2fc98e commit d6b6665

File tree

9 files changed

+620
-296
lines changed

9 files changed

+620
-296
lines changed

NuGet/MsieJavaScriptEngine.nuspec

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1313
<description>This library is a .NET wrapper for working with the JavaScript engines of Internet Explorer and Edge (JsRT versions of Chakra, ActiveScript version of Chakra and Classic JavaScript Engine). Project was based on the code of SassAndCoffee.JavaScript (http://github.com/paulcbetts/SassAndCoffee), Chakra Sample Hosts (http://github.com/panopticoncentral/chakra-host) and jsrt-dotnet (http://github.com/robpaveza/jsrt-dotnet).</description>
1414
<summary>This library is a .NET wrapper for working with the JavaScript engines of Internet Explorer and Edge (JsRT versions of Chakra, ActiveScript version of Chakra and Classic JavaScript Engine).</summary>
15-
<releaseNotes>Now when you call the overloaded version of the `ExecuteResource` method, that takes the type, need to pass the resource name without the namespace.</releaseNotes>
15+
<releaseNotes>1. Now when you call the overloaded version of the `ExecuteResource` method, that takes the type, need to pass the resource name without the namespace;
16+
2. Fixed a error “Out of stack space”.</releaseNotes>
1617
<copyright>Copyright (c) 2012-2016 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
1718
<language>en-US</language>
1819
<tags>JavaScript ECMAScript MSIE IE Edge Chakra</tags>
@@ -36,11 +37,9 @@
3637
<dependency id="System.Text.Encoding" version="4.0.11" />
3738
<dependency id="System.Text.RegularExpressions" version="4.1.0" />
3839
<dependency id="System.Threading" version="4.0.11" />
40+
<dependency id="System.Threading.Thread" version="4.0.0" />
3941
</group>
4042
</dependencies>
41-
<frameworkAssemblies>
42-
<frameworkAssembly assemblyName="WindowsBase" targetFramework=".NETFramework4.5.1" />
43-
</frameworkAssemblies>
4443
<references>
4544
<reference file="MsieJavaScriptEngine.dll" />
4645
</references>

NuGet/readme.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
=============
2222
RELEASE NOTES
2323
=============
24-
Now when you call the overloaded version of the `ExecuteResource` method, that
25-
takes the type, need to pass the resource name without the namespace.
24+
1. Now when you call the overloaded version of the `ExecuteResource` method, that
25+
takes the type, need to pass the resource name without the namespace;
26+
2. Fixed a error “Out of stack space”.
2627

2728
============
2829
PROJECT SITE

src/MsieJavaScriptEngine.Net4/MsieJavaScriptEngine.Net40.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@
345345
<Compile Include="..\MsieJavaScriptEngine\Resources\NetFrameworkStrings.ru-ru.Designer.cs">
346346
<Link>Resources\NetFrameworkStrings.ru-ru.Designer.cs</Link>
347347
</Compile>
348+
<Compile Include="..\MsieJavaScriptEngine\ScriptDispatcher.cs">
349+
<Link>ScriptDispatcher.cs</Link>
350+
</Compile>
348351
<Compile Include="..\MsieJavaScriptEngine\StatedFlag.cs">
349352
<Link>StatedFlag.cs</Link>
350353
</Compile>
@@ -368,7 +371,6 @@
368371
</ItemGroup>
369372
<ItemGroup>
370373
<Reference Include="System" />
371-
<Reference Include="WindowsBase" />
372374
</ItemGroup>
373375
<ItemGroup>
374376
<None Include="..\..\tools\Key.snk">

src/MsieJavaScriptEngine/ActiveScript/ActiveScriptJsEngineBase.cs

Lines changed: 120 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Reflection;
66
using System.Runtime.InteropServices;
77
using System.Runtime.InteropServices.Expando;
8-
using System.Windows.Threading;
98

109
using EXCEPINFO = System.Runtime.InteropServices.ComTypes.EXCEPINFO;
1110

@@ -87,9 +86,9 @@ internal abstract class ActiveScriptJsEngineBase : IInnerJsEngine, IActiveScript
8786
private readonly string _engineModeName;
8887

8988
/// <summary>
90-
/// <see cref="System.Windows.Threading.Dispatcher"/> for the thread currently executing
89+
/// Script dispatcher
9190
/// </summary>
92-
private readonly Dispatcher _dispatcher = Dispatcher.CurrentDispatcher;
91+
private static readonly ScriptDispatcher _dispatcher = new ScriptDispatcher();
9392

9493
/// <summary>
9594
/// Flag that object is destroyed
@@ -111,53 +110,57 @@ protected ActiveScriptJsEngineBase(string clsid, JsEngineMode engineMode, string
111110
{
112111
_engineMode = engineMode;
113112
_engineModeName = JsEngineModeHelpers.GetModeName(engineMode);
114-
_pActiveScript = IntPtr.Zero;
113+
_documentVersion = DateTime.UtcNow.ToString("o");
115114

116-
try
117-
{
118-
_pActiveScript = ComHelpers.CreateInstanceByClsid<IActiveScript>(clsid);
119-
_activeScript = (IActiveScript)Marshal.GetObjectForIUnknown(_pActiveScript);
120-
}
121-
catch (Exception e)
115+
_dispatcher.Invoke(() =>
122116
{
123-
throw new JsEngineLoadException(
124-
string.Format(CommonStrings.Runtime_IeJsEngineNotLoaded,
125-
_engineModeName, lowerIeVersion, e.Message), _engineModeName);
126-
}
117+
_pActiveScript = IntPtr.Zero;
127118

128-
if (languageVersion != ScriptLanguageVersion.None)
129-
{
130-
var activeScriptProperty = _activeScript as IActiveScriptProperty;
131-
if (activeScriptProperty != null)
119+
try
120+
{
121+
_pActiveScript = ComHelpers.CreateInstanceByClsid<IActiveScript>(clsid);
122+
_activeScript = (IActiveScript)Marshal.GetObjectForIUnknown(_pActiveScript);
123+
}
124+
catch (Exception e)
125+
{
126+
throw new JsEngineLoadException(
127+
string.Format(CommonStrings.Runtime_IeJsEngineNotLoaded,
128+
_engineModeName, lowerIeVersion, e.Message), _engineModeName);
129+
}
130+
131+
if (languageVersion != ScriptLanguageVersion.None)
132132
{
133-
object scriptLanguageVersion = (int)languageVersion;
134-
uint result = activeScriptProperty.SetProperty((uint)ScriptProperty.InvokeVersioning,
135-
IntPtr.Zero, ref scriptLanguageVersion);
136-
if (result != (uint)ScriptHResult.Ok)
133+
var activeScriptProperty = _activeScript as IActiveScriptProperty;
134+
if (activeScriptProperty != null)
137135
{
138-
throw new JsEngineLoadException(
139-
string.Format(NetFrameworkStrings.Runtime_ActiveScriptLanguageVersionSelectionFailed, languageVersion));
136+
object scriptLanguageVersion = (int)languageVersion;
137+
uint result = activeScriptProperty.SetProperty((uint)ScriptProperty.InvokeVersioning,
138+
IntPtr.Zero, ref scriptLanguageVersion);
139+
if (result != (uint)ScriptHResult.Ok)
140+
{
141+
throw new JsEngineLoadException(
142+
string.Format(NetFrameworkStrings.Runtime_ActiveScriptLanguageVersionSelectionFailed, languageVersion));
143+
}
140144
}
141145
}
142-
}
143146

144-
_activeScriptParse = new ActiveScriptParseWrapper(_pActiveScript, _activeScript);
145-
_activeScriptParse.InitNew();
147+
_activeScriptParse = new ActiveScriptParseWrapper(_pActiveScript, _activeScript);
148+
_activeScriptParse.InitNew();
146149

147-
_pActiveScriptGarbageCollector = ComHelpers.QueryInterfaceNoThrow<IActiveScriptGarbageCollector>(_pActiveScript);
148-
_activeScriptGarbageCollector = _activeScript as IActiveScriptGarbageCollector;
150+
_pActiveScriptGarbageCollector = ComHelpers.QueryInterfaceNoThrow<IActiveScriptGarbageCollector>(_pActiveScript);
151+
_activeScriptGarbageCollector = _activeScript as IActiveScriptGarbageCollector;
149152

150-
_activeScript.SetScriptSite(this);
151-
_activeScript.SetScriptState(ScriptState.Started);
153+
_activeScript.SetScriptSite(this);
154+
_activeScript.SetScriptState(ScriptState.Started);
152155

153-
InitScriptDispatch();
154-
_documentVersion = DateTime.UtcNow.ToString("o");
156+
InitScriptDispatch();
157+
});
155158

156159
LoadResources(useEcmaScript5Polyfill, useJson2Library);
157160
}
158161

159162
/// <summary>
160-
/// Destructs instance of ActiveScript JavaScript engine
163+
/// Destructs an instance of ActiveScript JavaScript engine
161164
/// </summary>
162165
~ActiveScriptJsEngineBase()
163166
{
@@ -318,46 +321,52 @@ private void ThrowError()
318321

319322
private void InvokeScript(Action action)
320323
{
321-
try
324+
_dispatcher.Invoke(() =>
322325
{
323-
_dispatcher.Invoke(DispatcherPriority.Input, action);
324-
}
325-
catch (ActiveScriptException e)
326-
{
327-
throw ConvertActiveScriptExceptionToJsRuntimeException(e);
328-
}
329-
catch (TargetInvocationException e)
330-
{
331-
var activeScriptException = e.InnerException as ActiveScriptException;
332-
if (activeScriptException != null)
326+
try
327+
{
328+
action();
329+
}
330+
catch (ActiveScriptException e)
333331
{
334-
throw ConvertActiveScriptExceptionToJsRuntimeException(activeScriptException);
332+
throw ConvertActiveScriptExceptionToJsRuntimeException(e);
335333
}
334+
catch (TargetInvocationException e)
335+
{
336+
var activeScriptException = e.InnerException as ActiveScriptException;
337+
if (activeScriptException != null)
338+
{
339+
throw ConvertActiveScriptExceptionToJsRuntimeException(activeScriptException);
340+
}
336341

337-
throw;
338-
}
342+
throw;
343+
}
344+
});
339345
}
340346

341347
private T InvokeScript<T>(Func<T> func)
342348
{
343-
try
349+
return _dispatcher.Invoke(() =>
344350
{
345-
return (T)_dispatcher.Invoke(DispatcherPriority.Input, func);
346-
}
347-
catch (ActiveScriptException e)
348-
{
349-
throw ConvertActiveScriptExceptionToJsRuntimeException(e);
350-
}
351-
catch (TargetInvocationException e)
352-
{
353-
var activeScriptException = e.InnerException as ActiveScriptException;
354-
if (activeScriptException != null)
351+
try
352+
{
353+
return func();
354+
}
355+
catch (ActiveScriptException e)
355356
{
356-
throw ConvertActiveScriptExceptionToJsRuntimeException(activeScriptException);
357+
throw ConvertActiveScriptExceptionToJsRuntimeException(e);
357358
}
359+
catch (TargetInvocationException e)
360+
{
361+
var activeScriptException = e.InnerException as ActiveScriptException;
362+
if (activeScriptException != null)
363+
{
364+
throw ConvertActiveScriptExceptionToJsRuntimeException(activeScriptException);
365+
}
358366

359-
throw;
360-
}
367+
throw;
368+
}
369+
});
361370
}
362371

363372
/// <summary>
@@ -554,51 +563,6 @@ private void ExecuteResource(string resourceName, Assembly assembly)
554563
Execute(code);
555564
}
556565

557-
/// <summary>
558-
/// Destroys object
559-
/// </summary>
560-
/// <param name="disposing">Flag, allowing destruction of
561-
/// managed objects contained in fields of class</param>
562-
private void Dispose(bool disposing)
563-
{
564-
_dispatcher.Invoke(DispatcherPriority.Input, (Action)(() =>
565-
{
566-
if (_disposedFlag.Set())
567-
{
568-
if (_dispatch != null)
569-
{
570-
ComHelpers.ReleaseComObject(ref _dispatch, !disposing);
571-
_dispatch = null;
572-
}
573-
574-
_activeScriptGarbageCollector = null;
575-
ComHelpers.ReleaseAndEmpty(ref _pActiveScriptGarbageCollector);
576-
577-
if (_activeScriptParse != null)
578-
{
579-
_activeScriptParse.Dispose();
580-
_activeScriptParse = null;
581-
}
582-
583-
if (_activeScript != null)
584-
{
585-
_activeScript.Close();
586-
Marshal.FinalReleaseComObject(_activeScript);
587-
_activeScript = null;
588-
}
589-
590-
ComHelpers.ReleaseAndEmpty(ref _pActiveScript);
591-
592-
if (_hostItems != null)
593-
{
594-
_hostItems.Clear();
595-
}
596-
597-
_lastException = null;
598-
}
599-
}));
600-
}
601-
602566
#region IActiveScriptSite implementation
603567

604568
/// <summary>
@@ -832,7 +796,7 @@ public void EmbedHostType(string itemName, Type type)
832796

833797
public void CollectGarbage()
834798
{
835-
InvokeScript(() => InnerCollectGarbage(ScriptGCType.Exhaustive));
799+
_dispatcher.Invoke(() => InnerCollectGarbage(ScriptGCType.Exhaustive));
836800
}
837801

838802
#endregion
@@ -848,6 +812,54 @@ public void Dispose()
848812
GC.SuppressFinalize(this);
849813
}
850814

815+
/// <summary>
816+
/// Destroys object
817+
/// </summary>
818+
/// <param name="disposing">Flag, allowing destruction of
819+
/// managed objects contained in fields of class</param>
820+
private void Dispose(bool disposing)
821+
{
822+
if (_disposedFlag.Set())
823+
{
824+
_dispatcher.Invoke(() =>
825+
{
826+
if (_dispatch != null)
827+
{
828+
ComHelpers.ReleaseComObject(ref _dispatch, !disposing);
829+
_dispatch = null;
830+
}
831+
832+
_activeScriptGarbageCollector = null;
833+
ComHelpers.ReleaseAndEmpty(ref _pActiveScriptGarbageCollector);
834+
835+
if (_activeScriptParse != null)
836+
{
837+
_activeScriptParse.Dispose();
838+
_activeScriptParse = null;
839+
}
840+
841+
if (_activeScript != null)
842+
{
843+
_activeScript.Close();
844+
Marshal.FinalReleaseComObject(_activeScript);
845+
_activeScript = null;
846+
}
847+
848+
ComHelpers.ReleaseAndEmpty(ref _pActiveScript);
849+
850+
if (disposing)
851+
{
852+
if (_hostItems != null)
853+
{
854+
_hostItems.Clear();
855+
}
856+
857+
_lastException = null;
858+
}
859+
});
860+
}
861+
}
862+
851863
#endregion
852864
}
853865
}

0 commit comments

Comments
 (0)