forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsScope.cs
More file actions
52 lines (42 loc) · 1.08 KB
/
JsScope.cs
File metadata and controls
52 lines (42 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using JavaScriptEngineSwitcher.Core;
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
{
/// <summary>
/// The scope automatically sets a context to current and resets the original context
/// when disposed
/// </summary>
internal struct JsScope : IDisposable
{
/// <summary>
/// The previous context
/// </summary>
private readonly JsContext _previousContext;
/// <summary>
/// Whether the structure has been disposed
/// </summary>
private StatedFlag _disposedFlag;
/// <summary>
/// Initializes a new instance of the <see cref="JsScope"/> struct
/// </summary>
/// <param name="context">The context to create the scope for</param>
public JsScope(JsContext context)
{
_disposedFlag = new StatedFlag();
_previousContext = JsContext.Current;
JsContext.Current = context;
}
#region IDisposable implementation
/// <summary>
/// Disposes the scope and sets the previous context to current
/// </summary>
public void Dispose()
{
if (_disposedFlag.Set())
{
JsContext.Current = _previousContext;
}
}
#endregion
}
}