Newly Open Sourced Elk Engine Brings JavaScript to Eight-Bit Microcontrollers Like the Arduino Nano
Taking up only 20kB of flash and 100 bytes of RAM, Elk lets you call C/C++ functions from JavaScript and vice versa.
Embedded specialist Cesanta Software has released a tool designed to help JavaScript developers make the move to embedded projects: Elk, an ultra-compact embeddable JavaScript engine for microcontrollers.
"Elk is a tiny embeddable JavaScript engine that implements a small but usable subset of ES6," Cesanta Software explains of its open source release. "It is designed for microcontroller development. Instead of writing firmware code in C/C++, Elk allows to develop in JavaScript. Another use case is providing customers with a secure, protected scripting environment for product customisation."
While designed with microcontrollers in mind - Cesanta Software has demonstrated the package running on an Arduino Nano, offering just 2kB of RAM and 30kB of usable flash — Elk is designed to be cross-platform, supporting anything from eight-bit microcontrollers to high-power 64-bit systems. It compiles in any ISO C or C++ compiler without external dependencies, and requires only two files - elk.c and elk.h - for embedding into a source tree.
Built around a compact and easy-to-use application programming interface (API), Elk allows the user to call C/C++ functions from JavaScript or JavaScript functions from C/C++, consumes only 20kB of storage and requires around 100 bytes of RAM for the core virtual machine. Another key feature: "[There is] no bytecode," Cesanta Software explains. "[Elk] interprets JS code directly."
There are a few caveats, of course - the first being performance. "Since Elk parses and interprets JS code on the fly," its creators admit, "it is not meant to be used in a performance-critical scenarios." Other restrictions include a lack of != and == with !== and === recommended in their place, no var or const, no do, switch, or for, no ternary operator, arrays, closures, or prototypes, enforced handling of strings as binary data chunks, and no standard library.
If none of those are deal-breakers, Elk is now available on GitHub — newly published this week under a dual license with the source code available under the reciprocal GNU General Public License 2 on top of the project's existing commercial license offering.