cesanta/mjs
FreeEmbedded JavaScript engine for C/C++
FreeFree tier
About cesanta/mjs
mJS is a restricted JavaScript engine designed for microcontrollers and other resource-constrained devices. It implements a strict subset of ES6, ensuring that any valid mJS code is also valid ES6 code. The engine prioritizes a small footprint and simple C/C++ interoperability, requiring approximately 50KB of flash memory and less than 1KB of RAM on a 32-bit ARM system. mJS is part of the MongooseOS ecosystem, enabling scripting for IoT devices. It lacks a standard library, closures, exceptions, and many ES6 features, but provides a built-in API for common operations like JSON parsing, string manipulation, and C function integration via FFI.
Key Features
Implements a strict subset of ES6 (any valid mJS code is valid ES6)
Tiny footprint: ~50KB flash memory and <1KB RAM on 32-bit ARM
Simple C/C++ interoperability and FFI (Foreign Function Interface)
Built-in functions: print, load, die, JSON.parse, JSON.stringify, Object.create, string/array methods, mkstr, ffi, gc
Byte strings for binary data handling
Part of MongooseOS, enabling scripting for IoT devices
Pros & Cons
Pros
- Extremely small memory footprint suitable for devices with limited RAM/ROM
- Easy to integrate into existing C/C++ codebases
- Allows dynamic scripting in environments where full JS engines are too heavy
- Strict subset encourages clean, predictable code
Cons
- Very limited subset of JavaScript: no closures, exceptions, standard library, new operator, or many ES6 features
- No support for Unicode strings outside of byte-level manipulation
- Not suitable for general-purpose JavaScript development or complex web scripting
- Lacks debugging tools and extensive ecosystem compared to mainstream engines
Best For
Scripting in resource-constrained microcontrollers and embedded systemsIoT device firmware customization and dynamic behaviorReplacing hardcoded logic with flexible JavaScript in C/C++ projectsEducational projects requiring a minimal JS engine
FAQ
What is mJS?
mJS is a restricted JavaScript engine designed for microcontrollers and embedded systems. It implements a strict subset of ES6 and focuses on small footprint and easy C/C++ integration.
How much memory does mJS require?
On a 32-bit ARM platform, mJS takes about 50KB of flash memory and less than 1KB of RAM.
Can I use standard JavaScript libraries with mJS?
No, mJS has no standard library. It provides only a small set of built-in functions like JSON.parse, JSON.stringify, print, load, and string/array methods.
Does mJS support closures or exceptions?
No, mJS does not support closures (only lexical scoping), exceptions, or many other ES6 features such as generators, proxies, promises, and classes.
How do I call C functions from mJS?
Use the FFI function: let f = ffi('int foo(int)'); then call f(123). This allows importing and calling C functions from within mJS scripts.