📜 ⬆️ ⬇️

Misconceptions about JavaScript Engine Switcher 2.X

Three pines: MSIE, V8 and ChakraCore


I wrote the English version of this post back 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 little about ReactJS.NET and 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 (by analogy with Less.js and dotless ). When creating ReactJS.NET, a completely different approach was used: The JS code of the React library is launched from .NET using the JS engine. The role of this JS-engine, just performs the library JavaScript Engine Switcher. JavaScript Engine Switcher defines a unified access interface to the basic features 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 another JS engine (provided your JS code is compatible with ECMAScript 5).


When I designed the JavaScript Engine Switcher, I assumed that the library developers in their packages would only refer to JavaScriptEngineSwitcher.Core and leave the library users the choice of the JS engine that suits them best. This is the approach I use in my Bundle Transformer project. But the author of ReactJS.NET went the other 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, sequential 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 overridden and the most appropriate engine can be selected. To do this, you need to register the JS engines before initializing ReactJS.NET. In ASP.NET 4.X, the initialization of ReactJS.NET is usually done in the file App_Start/ReactConfig.cs (thanks to the attribute [assembly: WebActivatorEx.PreApplicationStartMethod(…)] contents of this file are run before the code defined in the Application_Start method from the Global.asax file) , and in ASP.NET Core, the Startup.cs file is used for this purpose. In addition, if during registration you specify the name of the JS engine by default, then the selection of a suitable engine will follow a simplified scheme .

This approach is convenient for library users, because it allows using ReactJS.NET right out of the box (without the need to register JS engines manually). But this approach has one major drawback - the process of selecting the JS engine is opaque. Often, users do not know which JS engine is currently being used, and some do not even know about the existence of the JavaScript Engine Switcher. Lack of information leads to errors and delusions, which first appear in the bug tracker in the form of tips, and then in the form of posts in blogs . Misconceptions tend to spread with greater speed than correct information. At some point, this process goes out of control and there comes the need to write a post like this.


Over the past six months, I have often had to communicate with ReactJS.NET users about errors that occur due to 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 look at most of these misconceptions.


MSIE


For correct operation of the JavaScriptEngineSwitcher.Msie module, 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 also no exception). Full support for the ECMAScript 5 standard appeared in Internet Explorer only from version 9, when a new JavaScript engine, Chakra, was released.


If you run ReactJS.NET in conjunction with the JavaScriptEngineSwitcher.Msie module on a computer with 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 with Internet Explorer 9+ or Microsoft Edge installed. Ideally, the same browser version should be installed on the developer’s computer and the production server.


If you are not able to install a modern version of the browser on the server, then start using the JavaScriptEngineSwitcher.ChakraCore module.


V8


  1. The JavaScriptEngineSwitcher.V8 module requires the msvcp120.dll and msvcr120.dll from the Visual C ++ redistributable packages for Visual Studio 2013 .


    Starting from version 2.2.0, the native assemblies of the JavaScriptEngineSwitcher.V8 module depend on the msvcp140.dll assembly from the Visual C ++ Redistributable Package for Visual Studio 2015 .

  2. The JavaScriptEngineSwitcher.V8 package no longer contains native assemblies, and therefore, in addition to it, you need to install the ClearScript.V8 package.


    Indeed, since version 2.1.0, the JavaScriptEngineSwitcher.V8 package does not contain native builds for Windows, but these assemblies have not disappeared anywhere, but have been moved into 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 JavaScript Engine Switcher. In general, using the ClearScript.V8 package together with JavaScriptEngineSwitcher.V8 leads to errors .

  3. After installing the JavaScriptEngineSwitcher.V8.Native.* Packages, you must manually copy the native assemblies into the application directory, because it 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. Installing native assemblies is done using the following tools:


    1. For .NET 4.X applications and ASP.NET 4.X web applications, MSBuild scripts are used (for example, JavaScriptEngineSwitcher.V8.Native.win-x64.props )
    2. For ASP.NET 4.X websites, PowerShell scripts are used (for example, Install.ps1 and Uninstall.ps1 ).
    3. For .NET Core applications and ASP.NET Core web applications, a mechanism is used based on runtimes directories and runtimes identifiers (RIDs) .

    In the version of JavaScriptEngineSwitcher.V8 for .NET 4.X, native assemblies are loaded from non-standard directories . In this case, the native assemblies are located in the x86 and x64 subdirectories of the bin\[Debug|Release] directory (for web applications and sites, simply the bin directory).

  4. For a 64-bit version of Windows, it is enough to install only the JavaScriptEngineSwitcher.V8.Native.win-x64 package.


    .NET applications, ASP.NET web applications and sites in a 64-bit version of Windows can run not only 64-bit processes, but also 32-bit processes. For JavaScriptEngineSwitcher.V8 to work correctly in a 32-bit process, you need to install the JavaScriptEngineSwitcher.V8.Native.win-x86 package. If you don’t know in which process your application or website will run, then install both packages.


ChakraCore


  1. For correct operation of the JavaScriptEngineSwitcher.ChakraCore module in Windows OS, the assemblies msvcp120.dll and msvcr120.dll from the distributed Visual C ++ packages for Visual Studio 2013 are required .


    Starting from version 2.1.0, the native assemblies of the JavaScriptEngineSwitcher.ChakraCore module for Windows depend on the assembly msvcp140.dll from the Visual C ++ Redistributable Package for Visual Studio 2015 .

  2. 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 JavaScript Engine Switcher.


    In addition to these two packages, three more packages are also available:



    If you need support from 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 source code of ChakraCore, you need to use the version supported by the JavaScriptEngineSwitcher.ChakraCore module.

  3. After installing the JavaScriptEngineSwitcher.ChakraCore.Native.* Packages, you must manually copy the native assemblies into the bin directory.


    After installing these packages, you do not need to perform any additional actions, and this is the main advantage of JavaScriptEngineSwitcher.ChakraCore. Installing native assemblies is done using the following tools:


    1. For .NET 4.X applications and ASP.NET 4.X web applications, MSBuild scripts are used (for example, JavaScriptEngineSwitcher.ChakraCore.Native.win-x64.props ).
    2. For ASP.NET 4.X websites, PowerShell scripts are used (for example, Install.ps1 and Uninstall.ps1 ).
    3. For .NET Core applications and ASP.NET Core web applications, a mechanism is used based on runtimes directories and runtimes identifiers (RIDs) .

    In the version of JavaScriptEngineSwitcher.ChakraCore for .NET 4.X, the standard paths for searching native assemblies are redefined . In this case, the native assemblies are located in the x86 , x64 and arm directories of the bin\[Debug|Release] directory (for web applications and sites, simply the bin directory).

  4. For a 64-bit version of Windows, it is enough to install only the JavaScriptEngineSwitcher.ChakraCore.Native.win-x64 package.


    .NET applications, ASP.NET web applications and websites 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 don’t know in which process your application or website will run, then install both packages.


PS: To avoid similar misconceptions in the future, I recommend that you at least fluently read the “Release Notes” sections of updated NuGet packages. In the event that problems have already arisen, read the CHANGELOG.md or the “Releases” section in the project repository on GitHub. Also do not forget about the documentation and bugtracker .


UPDATE 01/01/2019: Fixed broken links and updated information about the JavaScriptEngineSwitcher.ChakraCore.Native.* Packages. It is also worth noting that some changes have occurred in JavaScript Engine Switcher 3.X. For example, the JavaScriptEngineSwitcher.V8 and JavaScriptEngineSwitcher.ChakraCore for Windows modules now require the installation of a Microsoft Visual C ++ redistributable component for Visual Studio 2017 . You can learn about other changes in the documentation section "How to upgrade applications to version 3.X" .


')

Source: https://habr.com/ru/post/339440/


All Articles