During the development of Tessel, unique challenges were encountered, particularly regarding the constraints typical of a developer platform. Most microcontrollers are limited to a few dozen kilobytes of RAM and a slightly larger amount of Flash memory. This capacity is dwarfed by the average size of JavaScript used in a single webpage, making the Tessel’s Cortex-M3, with only 200kb of memory on-chip, appear significantly underpowered.

The goal wasn’t to create a full-fledged PC but to make Tessel an appealing prototype platform for web developers. Various methods allow JavaScript to coexist and communicate with embedded devices, such as Firmata or the GPIO ports on a Raspberry Pi. However, few products offered the ability to use familiar software development tools for direct interfacing at the microcontroller level or with other ICs’ signal interfaces. (This ambition was extended further with Fractal, as discussed in more detail.)

To accommodate JavaScript as a viable programming language for this context, the primary obstacle was to enable an embedded platform to execute code efficiently. Despite JavaScript’s superficial resemblance to C, dynamic languages like JavaScript, Python, or Ruby necessitate considerable overhead for basic execution. This typically involves a virtual machine, which provides standard libraries, manages dynamic typing, and oversees memory allocation through mechanisms like garbage collection. Naturally, emulating a language in this manner can demand more CPU and memory resources than direct compilation.

The challenge lay in the fact that most widely-used JavaScript engines were optimized for desktop PCs, which boast gigabytes of RAM and fast internet connections. However, virtual machines vary greatly in design, with some specifically tailored for embedded systems.

Lua emerged as a particularly suitable starting point due to its dynamic, lightweight nature and its broad application across various domains, from video games to servers. Its simplicity and lack of external dependencies made Lua an attractive option for embedding into projects. Given its similar properties to JavaScript, including first-class functions and dynamic data types, Lua provided an excellent foundation for developing a JavaScript-compatible runtime for embedded platforms.

Despite the potential of heavyweight JavaScript engines like V8 and Spidermonkey for embedding, Lua’s minimal memory footprint and simplicity were more aligned with Tessel’s requirements. The initial demonstration of Tessel’s capabilities involved running Lua on the LPC1830, Tessel’s CPU, in the summer of 2013. Subsequently, efforts focused on bridging the gap between Lua and JavaScript.

Lua’s extensibility facilitated these efforts, enabling adaptations from basic operators to complex object behaviors, culminating in a runtime that simulated Node.js features with minimal resource usage.

A year into development, the landscape had shifted, with several alternative JavaScript engines becoming available, all open source and licensed for free use. Among them were Espruino VM and Duktape, optimized for embedded use. Tessel’s engine, named “Colony,” utilized Lua’s VM to create a Node-like environment compatible across both PCs and embedded platforms. However, Tessel’s limitation was not memory but rather the speed at which JavaScript executed. This led to experiments with LuaJIT, known for its efficiency and small codebase.

LuaJIT combines a parser/compiler, an interpreter, and a JIT compiler to optimize bytecode into machine code. This process, involving real-time optimization based on code execution patterns, promised significant speed improvements for Tessel.

The transition to LuaJIT did not initially include just-in-time compilation due to ongoing porting efforts. However, the switch to LuaJIT’s interpreter alone resulted in a 2-3x speed boost. Future work aimed to integrate the JIT compiler fully, promising even more dramatic performance increases.

For Tessel users, this meant an immediate, albeit modest, speed enhancement, with the promise of more substantial improvements on the horizon. The shift towards LuaJIT also indicated a rigorous approach to resolving JavaScript compatibility issues on Tessel, leveraging LuaJIT’s speed and efficiency for a better developer experience. Additionally, it positioned Tessel’s Colony engine as a contender in the realm of embeddable and high-performance JavaScript engines.

Outside of Tessel, this move highlighted Colony’s potential as a powerful tool for web programmers, enabling high-level prototyping in a standardized language across all technology stacks. As of 2014, the advancement of JavaScript into the realm of embedded systems presented an exciting frontier for web development.

Further technical insights into the LuaJIT porting process were promised in subsequent blog entries, offering a deeper dive into the technical underpinnings of this significant transition.