Skip to content

Commit 71e2356

Browse files
committed
In JavaScriptEngineSwitcher.ChakraCore added the ability to change configuration settings of ChakraCore JS engine: DisableBackgroundWork (default false), DisableNativeCodeGeneration (default false), DisableEval (default false) and EnableExperimentalFeatures (default false)
1 parent 9c7f796 commit 71e2356

File tree

7 files changed

+172
-42
lines changed

7 files changed

+172
-42
lines changed

src/JavaScriptEngineSwitcher.ChakraCore.Net4/JavaScriptEngineSwitcher.ChakraCore.Net40.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@
7272
<Compile Include="..\JavaScriptEngineSwitcher.ChakraCore\ChakraCoreJsEngineFactory.cs">
7373
<Link>ChakraCoreJsEngineFactory.cs</Link>
7474
</Compile>
75+
<Compile Include="..\JavaScriptEngineSwitcher.ChakraCore\ChakraCoreSettings.cs">
76+
<Link>ChakraCoreSettings.cs</Link>
77+
</Compile>
7578
<Compile Include="..\JavaScriptEngineSwitcher.ChakraCore\Helpers\NumericHelpers.cs">
7679
<Link>Helpers\NumericHelpers.cs</Link>
7780
</Compile>
@@ -135,9 +138,6 @@
135138
<Compile Include="..\JavaScriptEngineSwitcher.ChakraCore\JsRt\JsRuntimeAttributes.cs">
136139
<Link>JsRt\JsRuntimeAttributes.cs</Link>
137140
</Compile>
138-
<Compile Include="..\JavaScriptEngineSwitcher.ChakraCore\JsRt\JsRuntimeVersion.cs">
139-
<Link>JsRt\JsRuntimeVersion.cs</Link>
140-
</Compile>
141141
<Compile Include="..\JavaScriptEngineSwitcher.ChakraCore\JsRt\JsScope.cs">
142142
<Link>JsRt\JsScope.cs</Link>
143143
</Compile>

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreJsEngine.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,40 @@ static ChakraCoreJsEngine()
9999
/// Constructs a instance of adapter for the ChakraCore JS engine
100100
/// </summary>
101101
public ChakraCoreJsEngine()
102+
: this(new ChakraCoreSettings())
103+
{ }
104+
105+
/// <summary>
106+
/// Constructs a instance of adapter for the ChakraCore JS engine
107+
/// </summary>
108+
/// <param name="settings">Settings of the ChakraCore JS engine</param>
109+
public ChakraCoreJsEngine(ChakraCoreSettings settings)
102110
{
111+
ChakraCoreSettings chakraCoreSettings = settings ?? new ChakraCoreSettings();
112+
113+
JsRuntimeAttributes attributes = JsRuntimeAttributes.None;
114+
if (chakraCoreSettings.DisableBackgroundWork)
115+
{
116+
attributes |= JsRuntimeAttributes.DisableBackgroundWork;
117+
}
118+
if (chakraCoreSettings.DisableNativeCodeGeneration)
119+
{
120+
attributes |= JsRuntimeAttributes.DisableNativeCodeGeneration;
121+
}
122+
if (chakraCoreSettings.DisableEval)
123+
{
124+
attributes |= JsRuntimeAttributes.DisableEval;
125+
}
126+
if (chakraCoreSettings.EnableExperimentalFeatures)
127+
{
128+
attributes |= JsRuntimeAttributes.EnableExperimentalFeatures;
129+
}
130+
103131
_externalObjectFinalizeCallback = ExternalObjectFinalizeCallback;
104132

105133
try
106134
{
107-
_jsRuntime = JsRuntime.Create(JsRuntimeAttributes.AllowScriptInterrupt, JsRuntimeVersion.VersionEdge, null);
135+
_jsRuntime = JsRuntime.Create(attributes, null);
108136
_jsContext = _jsRuntime.CreateContext();
109137
}
110138
catch (Exception e)

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreJsEngineFactory.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ namespace JavaScriptEngineSwitcher.ChakraCore
77
/// </summary>
88
public sealed class ChakraCoreJsEngineFactory : IJsEngineFactory
99
{
10+
/// <summary>
11+
/// Settings of the ChakraCore JS engine
12+
/// </summary>
13+
private readonly ChakraCoreSettings _settings;
14+
1015
/// <summary>
1116
/// Gets a name of JS engine
1217
/// </summary>
@@ -16,13 +21,30 @@ public string EngineName
1621
}
1722

1823

24+
/// <summary>
25+
/// Constructs an instance of the ChakraCore JS engine factory
26+
/// </summary>
27+
public ChakraCoreJsEngineFactory()
28+
: this(new ChakraCoreSettings())
29+
{ }
30+
31+
/// <summary>
32+
/// Constructs an instance of the ChakraCore JS engine factory
33+
/// </summary>
34+
/// <param name="settings">Settings of the ChakraCore JS engine</param>
35+
public ChakraCoreJsEngineFactory(ChakraCoreSettings settings)
36+
{
37+
_settings = settings;
38+
}
39+
40+
1941
/// <summary>
2042
/// Creates a instance of the ChakraCore JS engine
2143
/// </summary>
2244
/// <returns>Instance of the ChakraCore JS engine</returns>
2345
public IJsEngine CreateEngine()
2446
{
25-
return new ChakraCoreJsEngine();
47+
return new ChakraCoreJsEngine(_settings);
2648
}
2749
}
2850
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
namespace JavaScriptEngineSwitcher.ChakraCore
2+
{
3+
/// <summary>
4+
/// Settings of the ChakraCore JS engine
5+
/// </summary>
6+
public sealed class ChakraCoreSettings
7+
{
8+
/// <summary>
9+
/// Gets or sets a flag for whether to disable any work (such as garbage collection)
10+
/// on background threads
11+
/// </summary>
12+
public bool DisableBackgroundWork
13+
{
14+
get;
15+
set;
16+
}
17+
18+
/// <summary>
19+
/// Gets or sets a flag for whether to disable native code generation
20+
/// </summary>
21+
public bool DisableNativeCodeGeneration
22+
{
23+
get;
24+
set;
25+
}
26+
27+
/// <summary>
28+
/// Gets or sets a flag for whether to disable calls of <code>eval</code> function
29+
/// </summary>
30+
public bool DisableEval
31+
{
32+
get;
33+
set;
34+
}
35+
36+
/// <summary>
37+
/// Gets or sets a flag for whether to enable all experimental features
38+
/// </summary>
39+
public bool EnableExperimentalFeatures
40+
{
41+
get;
42+
set;
43+
}
44+
45+
46+
/// <summary>
47+
/// Constructs instance of the ChakraCore settings
48+
/// </summary>
49+
public ChakraCoreSettings()
50+
{
51+
DisableBackgroundWork = false;
52+
DisableNativeCodeGeneration = false;
53+
DisableEval = false;
54+
EnableExperimentalFeatures = false;
55+
}
56+
}
57+
}

src/JavaScriptEngineSwitcher.ChakraCore/JsEngineFactoryCollectionExtensions.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,55 @@ public static JsEngineFactoryCollection AddChakraCore(this JsEngineFactoryCollec
2222
throw new ArgumentNullException("source");
2323
}
2424

25-
source.Add(new ChakraCoreJsEngineFactory());
25+
return source.AddChakraCore(new ChakraCoreSettings());
26+
}
27+
28+
/// <summary>
29+
/// Adds a instance of <see cref="ChakraCoreJsEngineFactory"/> to
30+
/// the specified <see cref="JsEngineFactoryCollection" />
31+
/// </summary>
32+
/// <param name="source">Instance of <see cref="JsEngineFactoryCollection" /></param>
33+
/// <param name="configure">The delegate to configure the provided <see cref="ChakraCoreSettings"/></param>
34+
/// <returns>Instance of <see cref="JsEngineFactoryCollection" /></returns>
35+
public static JsEngineFactoryCollection AddChakraCore(this JsEngineFactoryCollection source,
36+
Action<ChakraCoreSettings> configure)
37+
{
38+
if (source == null)
39+
{
40+
throw new ArgumentNullException("source");
41+
}
42+
43+
if (configure == null)
44+
{
45+
throw new ArgumentNullException("configure");
46+
}
47+
48+
var settings = new ChakraCoreSettings();
49+
configure(settings);
50+
51+
return source.AddChakraCore(settings);
52+
}
53+
54+
/// <summary>
55+
/// Adds a instance of <see cref="ChakraCoreJsEngineFactory"/> to
56+
/// the specified <see cref="JsEngineFactoryCollection" />
57+
/// </summary>
58+
/// <param name="source">Instance of <see cref="JsEngineFactoryCollection" /></param>
59+
/// <param name="settings">Settings of the ChakraCore JS engine</param>
60+
/// <returns>Instance of <see cref="JsEngineFactoryCollection" /></returns>
61+
public static JsEngineFactoryCollection AddChakraCore(this JsEngineFactoryCollection source, ChakraCoreSettings settings)
62+
{
63+
if (source == null)
64+
{
65+
throw new ArgumentNullException("source");
66+
}
67+
68+
if (settings == null)
69+
{
70+
throw new ArgumentNullException("settings");
71+
}
72+
73+
source.Add(new ChakraCoreJsEngineFactory(settings));
2674

2775
return source;
2876
}

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/JsRuntime.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,36 +101,34 @@ public bool Disabled
101101
/// <summary>
102102
/// Creates a new runtime
103103
/// </summary>
104-
/// <param name="attributes">The attributes of the runtime to be created</param>
105-
/// <param name="version">The version of the runtime to be created</param>
106-
/// <param name="threadServiceCallback">The thread service for the runtime. Can be null</param>
107104
/// <returns>The runtime created</returns>
108-
public static JsRuntime Create(JsRuntimeAttributes attributes, JsRuntimeVersion version, JsThreadServiceCallback threadServiceCallback)
105+
public static JsRuntime Create()
109106
{
110-
JsRuntime handle;
111-
JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateRuntime(attributes, threadServiceCallback, out handle));
112-
113-
return handle;
107+
return Create(JsRuntimeAttributes.None, null);
114108
}
115109

116110
/// <summary>
117111
/// Creates a new runtime
118112
/// </summary>
119113
/// <param name="attributes">The attributes of the runtime to be created</param>
120-
/// <param name="version">The version of the runtime to be created</param>
121114
/// <returns>The runtime created</returns>
122-
public static JsRuntime Create(JsRuntimeAttributes attributes, JsRuntimeVersion version)
115+
public static JsRuntime Create(JsRuntimeAttributes attributes)
123116
{
124-
return Create(attributes, version, null);
117+
return Create(attributes, null);
125118
}
126119

127120
/// <summary>
128121
/// Creates a new runtime
129122
/// </summary>
123+
/// <param name="attributes">The attributes of the runtime to be created</param>
124+
/// <param name="threadServiceCallback">The thread service for the runtime. Can be null</param>
130125
/// <returns>The runtime created</returns>
131-
public static JsRuntime Create()
126+
public static JsRuntime Create(JsRuntimeAttributes attributes, JsThreadServiceCallback threadServiceCallback)
132127
{
133-
return Create(JsRuntimeAttributes.None, JsRuntimeVersion.Version11, null);
128+
JsRuntime handle;
129+
JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateRuntime(attributes, threadServiceCallback, out handle));
130+
131+
return handle;
134132
}
135133

136134
/// <summary>

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/JsRuntimeVersion.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)