Skip to content

Commit ed045a5

Browse files
committed
In IJsEngine interface was added SupportsGarbageCollection property and CollectGarbage method
1 parent 94bf240 commit ed045a5

File tree

8 files changed

+125
-0
lines changed

8 files changed

+125
-0
lines changed

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreJsEngine.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ public override string Version
7878
get { return EngineVersion; }
7979
}
8080

81+
/// <summary>
82+
/// Gets a value that indicates if the JS engine supports garbage collection
83+
/// </summary>
84+
public override bool SupportsGarbageCollection
85+
{
86+
get { return true; }
87+
}
88+
8189

8290
/// <summary>
8391
/// Static constructor
@@ -1010,6 +1018,14 @@ protected override void InnerEmbedHostType(string itemName, Type type)
10101018
});
10111019
}
10121020

1021+
protected override void InnerCollectGarbage()
1022+
{
1023+
lock (_executionSynchronizer)
1024+
{
1025+
_jsRuntime.CollectGarbage();
1026+
}
1027+
}
1028+
10131029
#endregion
10141030

10151031
#region IDisposable implementation

src/JavaScriptEngineSwitcher.Core/IJsEngine.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ string Version
2525
get;
2626
}
2727

28+
/// <summary>
29+
/// Gets a value that indicates if the JS engine supports garbage collection
30+
/// </summary>
31+
bool SupportsGarbageCollection
32+
{
33+
get;
34+
}
35+
2836

2937
/// <summary>
3038
/// Evaluates an expression
@@ -138,5 +146,10 @@ string Version
138146
/// methods are bound to the type's static members.
139147
/// </remarks>
140148
void EmbedHostType(string itemName, Type type);
149+
150+
/// <summary>
151+
/// Performs a full garbage collection
152+
/// </summary>
153+
void CollectGarbage();
141154
}
142155
}

src/JavaScriptEngineSwitcher.Core/JsEngineBase.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ protected virtual void InnerEmbedHostType(string itemName, Type type)
5757
throw new NotImplementedException();
5858
}
5959

60+
protected virtual void InnerCollectGarbage()
61+
{
62+
throw new NotImplementedException();
63+
}
64+
6065
#region IJsEngine implementation
6166

6267
public abstract string Name
@@ -69,6 +74,14 @@ public abstract string Version
6974
get;
7075
}
7176

77+
public virtual bool SupportsGarbageCollection
78+
{
79+
get
80+
{
81+
throw new NotImplementedException();
82+
}
83+
}
84+
7285

7386
public virtual object Evaluate(string expression)
7487
{
@@ -440,6 +453,13 @@ public virtual void EmbedHostType(string itemName, Type type)
440453
InnerEmbedHostType(itemName, type);
441454
}
442455

456+
public virtual void CollectGarbage()
457+
{
458+
VerifyNotDisposed();
459+
460+
InnerCollectGarbage();
461+
}
462+
443463
#endregion
444464

445465
#region IDisposable implementation

src/JavaScriptEngineSwitcher.Jint/JintJsEngine.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ public override string Version
5959
get { return EngineVersion; }
6060
}
6161

62+
/// <summary>
63+
/// Gets a value that indicates if the JS engine supports garbage collection
64+
/// </summary>
65+
public override bool SupportsGarbageCollection
66+
{
67+
get { return false; }
68+
}
69+
6270

6371
/// <summary>
6472
/// Constructs a instance of adapter for the Jint JS engine
@@ -477,6 +485,11 @@ protected override void InnerEmbedHostType(string itemName, Type type)
477485
}
478486
}
479487

488+
protected override void InnerCollectGarbage()
489+
{
490+
throw new NotImplementedException();
491+
}
492+
480493
#endregion
481494

482495
#region IDisposable implementation

src/JavaScriptEngineSwitcher.Jurassic/JurassicJsEngine.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public override string Version
5656
get { return EngineVersion; }
5757
}
5858

59+
/// <summary>
60+
/// Gets a value that indicates if the JS engine supports garbage collection
61+
/// </summary>
62+
public override bool SupportsGarbageCollection
63+
{
64+
get { return false; }
65+
}
66+
5967

6068
/// <summary>
6169
/// Constructs a instance of adapter for the Jurassic JS engine
@@ -385,6 +393,11 @@ public override void ExecuteFile(string path, Encoding encoding = null)
385393
}
386394
}
387395

396+
protected override void InnerCollectGarbage()
397+
{
398+
throw new NotImplementedException();
399+
}
400+
388401
#endregion
389402

390403
#region IDisposable implementation

src/JavaScriptEngineSwitcher.Msie/MsieJsEngine.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public override string Version
5050
get { return _engineVersion; }
5151
}
5252

53+
/// <summary>
54+
/// Gets a value that indicates if the JS engine supports garbage collection
55+
/// </summary>
56+
public override bool SupportsGarbageCollection
57+
{
58+
get { return true; }
59+
}
60+
5361

5462
/// <summary>
5563
/// Constructs a instance of adapter for the MSIE JS engine
@@ -308,6 +316,11 @@ protected override void InnerEmbedHostType(string itemName, Type type)
308316
}
309317
}
310318

319+
protected override void InnerCollectGarbage()
320+
{
321+
_jsEngine.CollectGarbage();
322+
}
323+
311324
#endregion
312325

313326
#region IDisposable implementation

src/JavaScriptEngineSwitcher.V8/V8JsEngine.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ public override string Version
7272
get { return EngineVersion; }
7373
}
7474

75+
/// <summary>
76+
/// Gets a value that indicates if the JS engine supports garbage collection
77+
/// </summary>
78+
public override bool SupportsGarbageCollection
79+
{
80+
get { return true; }
81+
}
82+
7583

7684
/// <summary>
7785
/// Static constructor
@@ -429,6 +437,14 @@ protected override void InnerEmbedHostType(string itemName, Type type)
429437
}
430438
}
431439

440+
protected override void InnerCollectGarbage()
441+
{
442+
lock (_executionSynchronizer)
443+
{
444+
_jsEngine.CollectGarbage(true);
445+
}
446+
}
447+
432448
#endregion
433449

434450
#region IDisposable implementation

test/JavaScriptEngineSwitcher.Tests/CommonTestsBase.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,5 +765,26 @@ public virtual void RemovingVariableIsCorrect()
765765
}
766766

767767
#endregion
768+
769+
#region Garbage collection
770+
771+
[Fact]
772+
public virtual void GarbageCollectionIsCorrect()
773+
{
774+
// Arrange
775+
const string input = @"arr = []; for (i = 0; i < 1000000; i++) { arr.push(arr); }";
776+
777+
// Act
778+
using (var jsEngine = CreateJsEngine())
779+
{
780+
jsEngine.Execute(input);
781+
if (jsEngine.SupportsGarbageCollection)
782+
{
783+
jsEngine.CollectGarbage();
784+
}
785+
}
786+
}
787+
788+
#endregion
768789
}
769790
}

0 commit comments

Comments
 (0)