Skip to content

Creating instances of JS engines

Andrey Taritsyn edited this page Dec 5, 2016 · 3 revisions

It is recommended create instances of JS engines by using the following methods of the JsEngineSwitcher class: CreateEngine and CreateDefaultEngine. In this case, instances of JS engines are created with the settings, that you specified during their registration.

CreateEngine method

CreateEngine method takes only one parameter - name of JS engine:

IJsEngine engine = JsEngineSwitcher.Instance.CreateEngine("ChakraCoreJsEngine");

You can also use constant:

IJsEngine engine = JsEngineSwitcher.Instance.CreateEngine(ChakraCoreJsEngine.EngineName);

Since the name of JS engine is a string value, then you can get it from an external source (for example, configuration file). This feature allows you to quickly switch a whole project to usage of the different JS engine.

CreateDefaultEngine method

CreateDefaultEngine method takes no parameters:

IJsEngine engine = JsEngineSwitcher.Instance.CreateDefaultEngine();

Decision about which JS engine you need to create depends on the value of the DefaultEngineName property of the JsEngineSwitcher class.

Direct creation of instances

You can also directly create instances of JS engines by using constructors:

IJsEngine engine = new ChakraCoreJsEngine(new ChakraCoreSettings
{
	DisableEval = true,
	EnableExperimentalFeatures = true
});

But choosing this approach, you lose all benefits of the JavaScript Engine Switcher infrastructure.

Clone this wiki locally