Skip to content

ChakraCore

Taritsyn edited this page Apr 23, 2024 · 36 revisions

JavaScriptEngineSwitcher.ChakraCore contains a ChakraCoreJsEngine adapter (wrapper for the ChakraCore version of April 21, 2024. Project was based on the code of Chakra-Samples and jsrt-dotnet.

This package does not contain the native implementations of ChakraCore. Therefore, you need to choose and install the most appropriate package(s) for your platform. The following packages are available:

If you need support for other operating systems, then you can build the ChakraCore assembly by using following instructions from official repository. Only instead of the latest version of ChakraCore's source code, you need to use a version, that is supported by the JavaScriptEngineSwitcher.ChakraCore.

Mono support

JavaScriptEngineSwitcher.ChakraCore.Native.linux-x64 and JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64 packages do not support installation under Mono, but you can to install the native assemblies manually.

Linux

First you need to get the libChakraCore.so assembly file. You have 3 ways to do this:

  1. Download a assembly file from official repository.
  2. Download a assembly file from the JavaScript Engine Switcher's project repository.
  3. Extract a assembly file from the JavaScriptEngineSwitcher.ChakraCore.Native.linux-x64 package. The libChakraCore.so file is located in the runtimes/linux-x64/native/ directory of NuGet package.
  4. Build a assembly file from the source code.

Afterwards open a terminal window and change directory to the directory where the libChakraCore.so file is located. Next, enter the following command:

sudo cp libChakraCore.so /usr/local/lib/
sudo ldconfig

OS X

First you need to get the libChakraCore.dylib assembly file. You have 3 ways to do this:

  1. Download a assembly file from official repository.
  2. Download a assembly file from the JavaScript Engine Switcher's project repository.
  3. Extract a assembly file from the JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64 package. The libChakraCore.dylib file is located in the runtimes/osx-x64/native/ directory of NuGet package.
  4. Build a assembly file from the source code.

Afterwards open a terminal window and change directory to the directory where the libChakraCore.dylib file is located. Next, enter the following command:

mkdir -p /usr/local/lib/ && cp libChakraCore.dylib "$_"

Engine settings

You can specify a settings of JS engine during its registration:

engineSwitcher.EngineFactories
    .AddChakraCore(new ChakraCoreSettings
    {
        DisableEval = true,
        EnableExperimentalFeatures = true
    })
    ;

If you manually create an instance of JS engine, then you can pass settings via the constructor:

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

Consider in detail properties of the ChakraCoreSettings class:

Property name Data type Default value Description
AllowReflection Boolean false

Flag for whether to allow the usage of reflection API in the script code.

This affects Object.GetType, Exception.GetType, Exception.TargetSite and Delegate.Method.

DisableBackgroundWork Boolean false Flag for whether to disable any background work (such as garbage collection).
DisableEval Boolean false Flag for whether to disable calls of eval function with custom code and Function constructors taking function code as string.
DisableExecutablePage­Allocation Boolean false

Flag for whether to disable executable page allocation.

This also implies that Native Code generation will be turned off.

Note that this will break JavaScript stack decoding in tools like WPA since they rely on allocation of unique thunks to interpret each function and allocation of those thunks will be disabled as well.

DisableFatalOnOOM Boolean true Flag for whether to disable Failfast fatal error on OOM.
DisableNativeCodeGeneration Boolean false Flag for whether to disable native code generation.
EnableExperimentalFeatures Boolean false Flag for whether to enable all experimental features.
MaxStackSize Int32 503 808 or 1 007 616

Maximum stack size in bytes.

Set a 0 to use the default maximum stack size specified in the header for the executable.

Not supported in version for .NET Core 1.X.

MemoryLimit UIntPtr new UIntPtr(uint.MaxValue) or new UIntPtr(ulong.MaxValue) Current memory limit for a runtime in bytes. By default, no limit.