@ugo-studio/jspp
v0.2.5
Published
A modern transpiler that converts JavaScript code into high-performance, standard C++23.
Readme
JSPP (JavaScript++)
JSPP is a modern, experimental transpiler that converts JavaScript 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.
About The Project
JavaScript is flexible and dynamic; C++ is performant and type-safe. JSPP aims to offer the best of both worlds. By transpiling JS 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 current implementation translates an entire JavaScript file into a single C++ main function, cleverly using std::shared_ptr<std::any> to replicate JavaScript's dynamic typing, garbage-collection-like memory management, and complex features like closures.
Features
JSPP currently supports a foundational set of JavaScript features:
- Dynamic Variables: Declaration (
let,constandvar), assignment, and type changes at runtime. - Primitive Types:
undefined,null,boolean,number,string,objectandarray. - Functions:
- Function declarations and arrow functions.
- Correct hoisting for both variables and functions.
- Closures with proper lexical scoping and lifetime management.
- Operators: All arithmetic (
+,-,*) and assignment (=) operators. - Control Flow:
voidoperator. - Built-in APIs: A
consoleobject withlog(),warn(),error(), andtime()methods.
Reserved Keywords
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:
std: Reserved to prevent conflicts with the C++ standard library namespace.jspp: Reserved for internal use by the JSPP transpiler.co_yield,co_return,co_await: Reserved to prevent conflicts with the c++ couroutine keywords.
Using these keywords as variable names will result in a SyntaxError.
Installation
To use JSPP as a command-line tool, install it globally via npm:
npm i @ugo-studio/jspp -gFor Developers
To contribute to JSPP or run its test suite, follow these steps:
Prerequisites
- Node.js: This project uses Node.js for package management and script execution.
- C++ Compiler: A compiler with support for C++23 is required. This project is tested with
g++.g++(MinGW on Windows, or available via build-essentials on Linux)
Setup
- Clone the repo:
git clone https://github.com/ugo-studio/jspp.git - Install dependencies:
npm install
Usage
The primary way to use JSPP is via its command-line interface. This will transpile your .js file to C++, compile it, and execute the resulting binary.
jspp <path-to-your-js-file>Example:
To run a sample JavaScript file located at my-code/hello.js:
jspp my-code/hello.jsThe transpiled C++ file and executable will be generated in the same directory as the input file and cleaned up after execution.
You can also run the test suite, which will transpile all the JavaScript test cases in test/cases/, build the resulting C++ files, and run them.
npm testRoadmap
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.
Phase 1: Core Language Features
This phase focuses on building a solid foundation that correctly models JavaScript's core runtime behavior.
- [x] Dynamic Variables & Primitives (
let,const,var,undefined,null,string,number,boolean, etc.) - [x] Function Declarations & Arrow Functions
- [x] Correct Hoisting for Variables and Functions
- [x] Closures & Lexical Scoping
- [x]
if-elseif-elseconditions - [x] Basic
consoleAPI
Phase 2: Expanded Language Support
This phase will broaden the range of supported JavaScript syntax and features.
- [x] Error Handling:
try/catch/finallyblocks andthrow. - [x] Objects: Literals, property access (dot and bracket notation), methods.
- [x] Arrays: Literals, indexing, and core methods (
.push,.pop,.length, etc.). - [x] Operators: Full suite of arithmetic, logical, and comparison operators.
- [x] Control Flow: tenary operators,
forloops,whileloops,switch.
Phase 3: Interoperability & Standard Library
This phase will focus on building out the standard library and enabling modular code.
- [ ] JS Standard Library: Implementation of common built-in objects and functions (
Math,Date,String.prototype.*,Array.prototype.*). - [x] Asynchronous Operations: A C++ implementation of the JavaScript event loop,
Promise, andasync/await. - [ ] Module System: Support for
importandexportto transpile multi-file projects.
Phase 4: Optimization & Advanced Features
With a feature-complete transpiler, the focus will shift to performance and advanced capabilities.
- [ ] Performance Benchmarking: Create a suite to compare transpiled C++ performance against V8 and other JS engines.
- [ ] C++ Interoperability: Define a clear API for calling C++ functions from the transpiled JavaScript and vice-versa.
Contributing
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
License
Distributed under the MIT License. See LICENSE for more information.
