Samsung has recently open sourced IoT.js, a platform for IoT applications written in JavaScript, and JerryScript, a JavaScript engine for small, embedded devices.
The purpose of the two projects from Samsung is to enable JavaScript developers to build applications for the Internet of Things. IoT devices come with severe constraints in terms of CPU performance and memory footprint. Because of that, Samsung has designed the JerryScript engine to run in less than 64KB or RAM and the entire code fits in less than 200KB of ROM. Running some JavaScript code on JerryScript looks like this:
{
jerry_init (JERRY_FLAG_ENABLE_LOG);
char script [] = "print ('Hello, World!');";
jerry_parse (script, strlen (script));
jerry_run ();
jerry_cleanup ();
}
The IoT.js platform uses JerryScript to run JavaScript code and libuv for asynchronous I/O, and enables developers to create IoT services that communicate with each other and the outside world. IoT.js currently runs on Linux and NuttX – a real-time OS – and targets Raspberry Pi 2 and a ST board, with plans for other MCUs and IoT devices. The API provides functionality for buffers, console, events, GPIO, streams, timers, among others.
The following graphics show the internal architecture of a JavaScript application running on IoT.js/JerryScript and a comparison between running the same app on IoT.js vs. Node.js:
We reached out to Samsung to find out more about these projects.
InfoQ: A JavaScript engine running on just 64KB of RAM seems to be very constrained. How did you achieve that?
Samsung: There are many quality attributes to consider for a JavaScript engine. Today, the most important one is performance, because we want web applications running in the browser to look like native applications.
From the perspective of IoT, we only focused on memory footprint.
JerryScript is a pure interpreter in contrast with multi-stage adaptive JIT engines today. So it has no overhead for storing compiled code. Even the parser doesn’t store AST. It produces bytecode line-by-line directly from the source code. For data representation, objects in JerryScript are optimized for size. JerryScript is using compressed pointers, bytecode blocks with fixed size, a pre-allocated object pool and multiple representation of Number objects to achieve both standard compliance and memory optimization. We will keep continue to reduce the memory footprint in various ways. You can check out the details of JerryScript internals at http://samsung.github.io/jerryscript/internals/.
InfoQ: What are the benefits of using JavaScript for IoT?
Samsung: JavaScript is the most famous programming language in the world today [1][2][3]. There is a large number of web developers familiar with it. So, using JavaScript for IoT easily enlarges the developer community for IoT. Java for Android is a good historical example.
On the other hand, JavaScript is well-suited for embedded device programming. It supports asynchronous function calls and I/O which are useful for event-driven hardware programming.
Finally, JavaScript is the most widely used language for the Web. Making IoT devices interact with the web ecosystem is a wise choice for building an IoT ecosystem. Accordingly, many web standards such as HTTP, JSON, REST are already at the center of IoT connectivity standardization. The only one missing is JavaScript. We think that JavaScript is the most important one for the application and service interoperability layer on top of connectivity.
InfoQ: What functionality is IoT.js providing?
Samsung: The core of IoT.js is backward compatible with Node.js. To do that, IoT.js obeys the CommonJS specification for modular programming and supports a core subset of Node.js’ API. For IoT purpose, we are defining standard modules specific to embedded device control and IoT programming. As of now, we defined the first candidate specification for GPIO access API and implemented a prototype of it. We think that these activities should be done in consensus with the IoT community, in order to be of real value. That’s why we open sourced IoT.js. Please refer to this issue discussions on GitHub (https://goo.gl/7V8jNp).
We will also provide the interfacing to IoT connectivity layers such as OIC/IoTivity, AllSeen Alliance, etc. It will be one of *.npm like modules.