ariya.io About Talks Articles

ChakraCore on macOS

3 min read

Microsoft open-sourced their JavaScript engine, ChakraCore, sometime ago. Since then, it has been ported to run on other platforms than Windows. This is fantastic, as now it is possible to use it on other Unices, including macOS.

It has been more than half a year ago I was playing with ChakraCore on Linux. Back then, the porting was not completed yet but now it is possible to have fun with ChakraCore on non-Windows platform. There are still some caveats, for instance JIT (just-in-time) compilation is not fully supported yet. Still, it should not prevent us from enjoying ChakraCore and start using it.

For macOS, the main build dependencies are Xcode and CMake. The first is necessary for all the development tools, while CMake (version 3.2 or later) is the chosen build tool. Since I am using Nix as my macOS package manager (see why it is awesome), it is a simple step for me:

$ nix-env -i cmake
$ cmake --version
cmake version 3.6.0

ChakraCore also requires ICU library. This is fulfilled by installing the right package and setting up some environment variables:

nix-env -i icu4c
export LIBICU_DEV=$(nix-env -q --xml --out-path icu4c | egrep -o '(/nix[-/.0-9a-zA-Z]+)-dev')

Now it is time to grab ChakraCore from its repository. While the branch is called linux, it is actually for Linux, FreeBSD, and macOS ports. Update: There is no need to use that special branch anymore, just use master.

git clone https://github.com/Microsoft/ChakraCore.git
cd ChakraCore

There are more than 430 C++ sources to compile, so grab a cup of coffee while waiting for ChakraCore to build!

./build.sh --static --icu=$LIBICU_DEV/include

chakracore

Once it is completed without any errors, we can run a simple JavaScript shell built with ChakraCore named ch (it stands for Chakra Host). It does not have a lot of features (i.e. it is not Node.js), but it is sufficient to experiment with. Assuming we have a JavaScript program called hello.js with the following content:

print("Hello world");

then running it will give us:

$ ./BuildLinux/Release/ch hello.js 
Hello world

This example only invokes print, a built-in function (think of console.log) exposed to the JavaScript run-time inside that ch shell.

Of course, ChakraCore knows all the standard JavaScript run-time objects:

$ cat math.js
print("Sqrt of 2 is", Math.sqrt(2));

$ ./BuildLinux/Release/ch math.js
Sqrt of 2 is 1.4142135623730951

We can also play with ES2015 (formerly known as ES6):

$ cat class.js 
class Car {
  constructor(maker, model, year) {
    print('I am', maker, model, year);
  }
}
new Car('Maserati', 'Quattroporte', 2020);

$ ./BuildLinux/Release/ch class.js 
I am Maserati Quattroporte 2020

While it is interesting to play with that simplistic shell, obviously ChakraCore is more than that. At the next installment, we will cover the use of ChakraCore to add a scripting feature to an existing application.

Related posts:

♡ this article? Explore more articles and follow me Twitter.

Share this on Twitter Facebook