JSPP is a modern, experimental transpiler that converts JavaScript and TypeScript code into high-performance, standard C++23.
The primary goal of this project is to achieve a near-perfect translation of JavaScript's dynamic nature and core features into the statically-typed, compiled world of C++, exploring modern C++ capabilities to bridge the gap between these two powerful languages.
JavaScript is flexible and dynamic; C++ is performant and type-safe. JSPP aims to offer the best of both worlds. By transpiling JS/TS to C++, we can potentially run JavaScript logic in environments where C++ is native, with significant performance gains and opportunities for low-level interoperability.
This project serves as a deep dive into compiler design, language semantics, and the expressive power of modern C++. The architecture is designed for performance, utilizing:
- Fast Runtime Library: Core JavaScript logic is implemented in a static C++ library (
libjspp.a), precompiled for speed. - Precompiled Headers (PCH): Common headers are precompiled to drastically reduce the front-end parsing time of the C++ compiler.
- NaN-Boxing: An efficient 64-bit value representation (NaN-boxing) is used to replicate JavaScript's dynamic typing with minimal overhead.
- Modern C++23: Leverages the latest language features, including coroutines for
async/awaitand generators.
JSPP currently supports a comprehensive set of JavaScript features:
- Languages: JavaScript (
.js) and TypeScript (.ts) support. - Dynamic Variables: Declaration (
let,const,var), assignment, and type changes at runtime. - Primitive Types:
undefined,null,boolean,number,string,symbol. - Functions:
- Function declarations, arrow functions, and function expressions.
- Generators:
function*andyieldsupport. - Async/Await:
async functionandawaitsupport (built on C++20 coroutines). - Closures with proper lexical scoping.
- Object Oriented:
- Classes: Class declarations, constructors, methods, getters/setters, and inheritance (
extends). - Prototypes: Prototype chain traversal and manipulation.
- Classes: Class declarations, constructors, methods, getters/setters, and inheritance (
- Control Flow:
if,else if,else.- Loops:
for,for-of,for-in,while,do-while. switchstatements.try,catch,finallyblocks.
- Operators: Full suite of arithmetic, assignment, comparison, logical, bitwise, and unary operators (including
typeof,delete,void,instanceof,in). - Standard Library:
- Console:
log,warn,error,time,timeEnd. - Math: Comprehensive
Mathobject implementation. - Timers:
setTimeout,clearTimeout,setInterval,clearInterval. - Promise: Full
Promiseimplementation with chaining. - Error: Standard
Errorclass and stack traces. - Arrays & Objects: Extensive methods support (
map,filter,reduce,push,pop,Object.keys, etc.).
- Console:
JSPP reserves certain keywords to avoid conflicts with the generated C++ code and internal mechanisms. The following keywords are reserved and cannot be used as variable names:
jspp: Reserved for internal use by the transpiler.std: Reserved to prevent conflicts with the C++ standard library.co_yield,co_return,co_await: Reserved for C++ coroutine mechanics.
Using these keywords as variable names will result in a SyntaxError.
To use JSPP as a command-line tool, install it globally via npm:
npm i @ugo-studio/jspp@latest -gTo contribute to JSPP or run its test suite, follow these steps:
- Bun: This project uses Bun for package management, script execution, and testing.
- C++ Compiler: A compiler with support for C++23 is required (e.g.,
g++13+ orclang17+).- Windows: MSYS2 with
mingw-w64-x86_64-gccis recommended. - Linux:
g++-14or equivalent. - macOS:
brew install gcc.
- Windows: MSYS2 with
- Clone the repo:
git clone https://github.com/ugo-studio/jspp.git
- Install dependencies and build the runtime:
Note: The
bun install
postinstallscript will automatically check for your C++ compiler and setup emsdk for wasm support.
The primary way to use JSPP is via its command-line interface. This will transpile your file to C++, compile it, and execute the resulting binary.
jspp <path-to-your-file>Example:
To run a sample TypeScript file located at my-code/test.ts:
jspp my-code/test.tsThe transpiled C++ file and executable will be generated in the same directory as the input file and cleaned up after execution (unless --keep-cpp is used).
This project is ambitious, and there is a long and exciting road ahead. Here is a high-level overview of the planned features and the project's current standing.
This phase focuses on building a solid foundation that correctly models JavaScript's core runtime behavior.
- Dynamic Variables & Primitives (NaN-boxing)
- Function Declarations & Arrow Functions
- Correct Hoisting for Variables and Functions
- Closures & Lexical Scoping
- Basic Control Flow (
if,loops) - Basic
consoleAPI
This phase broadens the range of supported JavaScript syntax and features.
- Error Handling:
try/catch/finallyblocks andthrow. - Objects & Classes: Classes, inheritance, literals, property access.
- Arrays: Literals, indexing, and core methods.
- Operators: Full suite of arithmetic, logical, and comparison operators.
- Advanced Control Flow:
switch,for-of,for-in, generators. - TypeScript Support: Compilation of
.tsfiles.
This phase focuses on building out the standard library and enabling modular code.
- JS Standard Library: Core implementation of
Math,Symbol,Error,String,Array,Object,Timer. - Expanded Library:
Date,Temporal,Map,Set,JSON,RegExp. - Asynchronous Operations: Event loop,
Promise,async/await. - Module System: Support for
importandexportto transpile multi-file projects.
With a feature-complete transpiler, the focus will shift to performance and advanced capabilities.
- Architecture Optimization: Static library runtime and Precompiled Headers.
- Performance Benchmarking: Create a suite to compare transpiled C++ performance against V8.
- Linker Optimization: Support for
LLDorMoldlinkers. - C++ Interoperability: Define a clear API for calling C++ functions from JavaScript.
Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Distributed under the MIT License. See LICENSE for more information.