Misconceptions about the JavaScript Engine Switcher 2.X
- Transfer
I wrote the English version of this post in May and published it in the bug tracker of the ReactJS.NET project. Initially, I did not plan to translate this post into Russian, but on Monday I saw the program of the 13th meeting of the MskDotNet Community , and decided that such a translation would be useful to the community
For a better understanding of the material presented in the post, I will talk a bit about ReactJS.NET and the JavaScript Engine Switcher . ReactJS.NET is a .NET library that compiles JSX code into JS code. This library is not a .NET port of the React library (similar to c Less.js and dotless ). When creating ReactJS.NET, a completely different approach was used: the Jact code of the React library is launched from .NET using the JS engine. The role of this JS engine is precisely performed by the JavaScript Engine Switcher library. JavaScript Engine Switcher defines a unified interface for accessing the basic capabilities of popular JS engines ( MSIE JavaScript Engine for .Net , Microsoft ClearScript.V8, Jurassic , Jint, and ChakraCore ) and allows you to quickly switch your library or application to use a different JS engine (provided that your JS code is compatible with the ECMAScript 5 standard).
When I designed the JavaScript Engine Switcher, I assumed that the library developers in their packages would refer only to JavaScriptEngineSwitcher.Core and leave the library users the opportunity to choose the most suitable implementation of the JS engine. This is exactly the approach I use in my Bundle Transformer project . But the author of ReactJS.NET went a different way: the React.Core package in its list of dependencies already contains several pre-installed JavaScript Engine Switcher modules: MSIE , V8 and ChakraCore . During the initialization of the library, successive attempts are made to create an instance of one of the above engines and the first successfully created engine becomes the default engine.
It is worth noting that this behavior can be redefined and you can choose the most suitable engine yourself. To do this, register JS engines before initializing ReactJS.NET. In ASP.NET 4.X, ReactJS.NET is usually initialized in a fileApp_Start/ReactConfig.cs
(due to the attribute, the[assembly: WebActivatorEx.PreApplicationStartMethod(…)]
contents of this file are run before the code defined in the methodApplication_Start
from the fileGlobal.asax
), and in ASP.NET Core a file is used for these purposesStartup.cs
. In addition, if you specify the default JS engine name during registration, then the selection of a suitable engine will follow a simplified scheme .
This approach is convenient for library users, because it allows you to use ReactJS.NET right out of the box (without having to register JS engines manually). But this approach has one significant drawback - the JS engine selection process is opaque. Often, users do not know which JS engine is currently being used, and some do not even know about the existence of a JavaScript Engine Switcher. The lack of information leads to errors and errors that first appear in the bugtracker in the form of tips, and then in the form of blog posts . Misconceptions tend to spread at a faster rate than correct information. At some point, this process gets out of control and it becomes necessary to write a post like this.
Over the past six months, I often had to talk with ReactJS.NET users about errors that occur due to the JavaScript Engine Switcher . In 80% of cases, these were not errors, but simply misuse of the library, caused by misconceptions about how it works. In this post, I will cover most of these misconceptions.
Msie
For the JavaScriptEngineSwitcher.Msie module to work correctly, it is enough that Internet Explorer is installed on the computer.
At the moment, most JS libraries are written in ECMAScript 5 (the React library is no exception). Full support for the ECMAScript 5 standard appeared in Internet Explorer only since version 9, when the new JavaScript engine, Chakra, was released.
If you run ReactJS.NET in conjunction with the JavaScriptEngineSwitcher.Msie module on a computer that has Internet Explorer 8 or lower installed, you will get an error like this:
Object doesn't support this property or method
Therefore, you should always use JavaScriptEngineSwitcher.Msie only on computers that have Internet Explorer 9+ or Microsoft Edge installed. Ideally, the same browser version should be installed on the developer's computer and production server.
If you are unable to install a modern version of the browser on the server, then start using the JavaScriptEngineSwitcher.ChakraCore module.
V8
Module JavaScriptEngineSwitcher.V8 necessary assembly
msvcp120.dll
andmsvcr120.dll
of the Visual C ++ Redistributable packages for Visual Studio 2013 .Starting with version 2.2.0, native builds of the JavaScriptEngineSwitcher.V8 module depend on the build
msvcp140.dll
from the Visual C ++ redistributable package for Visual Studio 2015 .The JavaScriptEngineSwitcher.V8 package no longer contains native assemblies, and therefore, in addition to it, you need to install the ClearScript.V8 package .
Indeed, starting from version 2.1.0, the JavaScriptEngineSwitcher.V8 package does not contain native assemblies for Windows, but these assemblies did not disappear anywhere, but were moved to separate packages: JavaScriptEngineSwitcher.V8.Native.win-x86 and JavaScriptEngineSwitcher.V8.Native.win -x64 . Therefore, you do not need to install the ClearScript.V8 package, which has nothing to do with the JavaScript Engine Switcher. In general, using the ClearScript.V8 package with JavaScriptEngineSwitcher.V8 leads to errors .
After installing the packages,
JavaScriptEngineSwitcher.V8.Native.*
you must manually copy the native assemblies into the application directory, because this is recommended by the authors of ClearScript .After installing these packages, you do not need to perform any additional actions, and this is the main advantage of JavaScriptEngineSwitcher.V8. Native builds are installed using the following tools:
- For .NET 4.X applications and ASP.NET 4.X web applications, MSBuild scripts are used (for example,
JavaScriptEngineSwitcher.V8.Native.win-x64.props
) - For ASP.NET 4.X websites, PowerShell scripts (such as
Install.ps1
andUninstall.ps1
) are used. - For .NET Core applications and ASP.NET Core web applications, a mechanism based on directories
runtimes
and runtime identifiers (RIDs) is used .
In JavaScriptEngineSwitcher.V8 for .NET 4.X, native assemblies are loaded from non-standard directories . In this case, native assemblies are located in subdirectories
x86
and ax64
directorybin\[Debug|Release]
(for web applications and sites just a directorybin
).- For .NET 4.X applications and ASP.NET 4.X web applications, MSBuild scripts are used (for example,
For a 64-bit version of Windows, you only need to install the JavaScriptEngineSwitcher.V8.Native.win-x64 package.
.NET applications, ASP.NET web applications and sites in the 64-bit version of Windows can run not only in 64-bit processes, but also in 32-bit ones. For JavaScriptEngineSwitcher.V8 to work correctly in a 32-bit process, you need to install the JavaScriptEngineSwitcher.V8.Native.win-x86 package. If you do not know in which process your application or website will run, then install both packages.
Chakracore
To work correctly, JavaScriptEngineSwitcher.ChakraCore module in Windows needed the assembly
msvcp120.dll
andmsvcr120.dll
of the Visual C ++ Redistributable packages for Visual Studio 2013 .Starting with version 2.1.0, native builds of the JavaScriptEngineSwitcher.ChakraCore module for Windows depend on the assembly
msvcp140.dll
from the Visual C ++ redistributable package for Visual Studio 2015 .The JavaScriptEngineSwitcher.ChakraCore package no longer contains native assemblies, and therefore, in addition to it, you need to install the Microsoft.ChakraCore package .
Indeed, starting from version 2.1.0, the JavaScriptEngineSwitcher.ChakraCore package does not contain native assemblies, but these assemblies did not disappear anywhere, but were moved to separate packages: JavaScriptEngineSwitcher.ChakraCore.Native.win-x86 and JavaScriptEngineSwitcher.ChakraCore.Native.win-x64 . Therefore, you do not need to install the Microsoft.ChakraCore package, which has nothing to do with the JavaScript Engine Switcher.
In addition to these two packages, three more packages are also available:
- JavaScriptEngineSwitcher.ChakraCore.Native.win-arm contains a native assembly for ARM versions of Windows. This package is compatible only with .NET Core and .NET Framework 4.5.
- JavaScriptEngineSwitcher.ChakraCore.Native.linux-x64 contains a native assembly for 64-bit Linux distributions. This package is compatible only with .NET Core.
- JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64 contains a native assembly for 64-bit versions of OS X. This package is compatible only with .NET Core.
If you need support for other operating systems, then you can build the ChakraCore library using the following instructions from the official repository . Only instead of the latest version of the ChakraCore source code, you need to use the version that is supported by the JavaScriptEngineSwitcher.ChakraCore module.
After installing the packages,
JavaScriptEngineSwitcher.ChakraCore.Native.*
you must manually copy the native assemblies to the directorybin
.After installing these packages, you do not need to perform any additional actions, and this is the main advantage of JavaScriptEngineSwitcher.ChakraCore. Native builds are installed using the following tools:
- For .NET 4.X applications and ASP.NET 4.X web applications, MSBuild scripts are used (for example,
JavaScriptEngineSwitcher.ChakraCore.Native.win-x64.props
). - For ASP.NET 4.X websites, PowerShell scripts (such as
Install.ps1
andUninstall.ps1
) are used. - For .NET Core applications and ASP.NET Core web applications, a mechanism based on directories
runtimes
and runtime identifiers (RIDs) is used .
The JavaScriptEngineSwitcher.ChakraCore version for .NET 4.X redefines the standard paths for finding native assemblies. In this case, the native assembly are located in subdirectories
x86
,x64
andarm
directorybin\[Debug|Release]
(for Web applications and sites just a directorybin
).- For .NET 4.X applications and ASP.NET 4.X web applications, MSBuild scripts are used (for example,
For a 64-bit version of Windows, you only need to install the JavaScriptEngineSwitcher.ChakraCore.Native.win-x64 package.
.NET applications, ASP.NET web applications and sites in 64-bit Windows can run not only in 64-bit processes, but also in 32-bit ones. For the JavaScriptEngineSwitcher.ChakraCore module to work correctly in a 32-bit process, you need to install the JavaScriptEngineSwitcher.ChakraCore.Native.win-x86 package. If you do not know in which process your application or website will run, then install both packages.
PS: To avoid such misconceptions in the future, I recommend that you at least briefly read the "Release Notes" sections of the updated NuGet packages. In the event that problems have already arisen, read CHANGELOG.md or the "Releases" section in the project repository on GitHub. Also, do not forget about the documentation and the bugtracker .
UPDATE 01/01/2019: Broken links fixed and package information updated JavaScriptEngineSwitcher.ChakraCore.Native.*
. It is also worth noting that some changes have occurred in the JavaScript Engine Switcher 3.X. For example, the JavaScriptEngineSwitcher.V8 and JavaScriptEngineSwitcher.ChakraCore modules for Windows now require the installation of the Microsoft Visual C ++ Redistributable for Visual Studio 2017 . For other changes you can learn from the documentation section «How to upgrade to version 3.X applications» .