Skip to content

Creating instances of JS engines

Taritsyn edited this page Apr 1, 2023 · 3 revisions

It is recommended create instances of JS engines by using the following methods of the IJsEngineSwitcher interface: 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.Current.CreateEngine("ChakraCoreJsEngine");

You can also use constant:

IJsEngine engine = JsEngineSwitcher.Current.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.Current.CreateDefaultEngine();

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

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.