Porting mujs to Rust with c2rust
I recently embarked on a project to convert mujs, a lightweight JavaScript interpreter, from C to Rust using the c2rust tool.
Conversion Process
To streamline the conversion, I first consolidated mujs into a single C file, resulting in a 20,000-line source file. Using c2rust, I translated this into a 30,000-line Rust file. The conversion process wasn't flawless; c2rust occasionally omitted semicolons, which I had to manually insert.
It's important to note that both c2rust and the generated Rust code are compatible only with the nightly-2022-08-08 version of the Rust compiler. Additionally, tests have been successful exclusively on Ubuntu 24.04.
Performance Comparison
For benchmarking, I utilized the v7-v8 test suite. Surprisingly, the performance of the converted mujs-one surpassed that of the original C version. This suggests that Rust's optimizations and safety features can enhance performance without compromising security.
| Engine | mujs-one | mujs |
|---|---|---|
| Version | 0 | 1.3.5 |
| Total size | 688K | 416K |
| Exe size | 688K | 416K |
| Dll size | 0 | 0 |
| Richards | 178 | 187 |
| DeltaBlue | 263 | 231 |
| Crypto | 149 | 117 |
| RayTrace | 406 | 398 |
| EarleyBoyer | 386 | 374 |
| RegExp | 165 | 153 |
| Splay | 1221 | 664 |
| NavierStokes | 330 | 408 |
| Score | 304 | 273 |
Pointer Bug Discovery
During testing, I encountered a potential pointer-related bug. Rust's strict safety checks and ownership model brought this issue to light, underscoring how Rust can make C codebases safer by design.
If you're interested in exploring this project further or running your own benchmarks, feel free to check out the repositories:
Your feedback and contributions are welcome!