When we set out to build JustJIT, we wanted the raw speed of C++ without losing the soul of Python. We didn't want a clunky separation where "Here is Python" and "There is C++," and never the twain shall meet. We wanted a seamless fusion.

But bridging these two worlds is dangerous. Python is a friendly, organic garden where you don't worry about memory or locks. C++ is a high-speed metallic machine where one wrong move crashes the whole system.

To connect them safely, we built two key technologies: the Inline C Compiler and RAII Wrappers.

The Bridge: Inline Compilation

Look at the Inline Compilation arrow in our sketch above. This isn't just about calling a pre-compiled library. In JustJIT, we embed a tiny, stripped-down Clang compiler right into the Python runtime.

When you write:


JustJIT takes that string, feeds it to the embedded LLVM/Clang engine, and compiles it to machine code on the fly. It captures your Python variables automatically and injects them into the C scope. It's a pipeline that turns your "loose" Python sketch into a "structured" C++ block in milliseconds.

The Shield: RAII Wrappers

But raw C++ in a Python process is a recipe for disaster. What about the Global Interpreter Lock (GIL)? What about object reference counting? If your C++ code runs too long, it freezes the UI. If it touches a Python object without the lock, it corrupts memory.

That's where the dashed box in the sketch comes in: the RAII Wrapper.

RAII (Resource Acquisition Is Initialization) is a C++ pattern where a resource's lifecycle is tied to a variable's scope. We use it to create a protective shield around the native code.

1. The GIL Guard

We implemented a class called

GILGuard. When your C++ execution enters a block that needs to talk to Python, it implicitly creates a guard:

{

GILGuard guard; // Acquires GIL

// ... safe to touch Python objects ...

} // Automatically releases GIL when 'guard' goes out of scope

In the sketch, this is the "GIL Guard" annotation. It ensures that no matter how the function exits—even if it crashes or throws an exception—the lock state is correctly restored.

2. Memory Safety (PyObjectPtr)

Managing PyObject* manually is a nightmare of Py_INCREF and Py_DECREF. Miss one, and you leak memory. Double-free, and you segfault.

We built

PyObjectPtr, a smart pointer specifically for Python objects. It acts like a "smart capsule" (as seen in our diagram).

PyObjectPtr obj = PyObjectPtr::steal(some_new_reference);

When

obj goes out of scope, it automatically decrements the reference count. This "Memory Safety" mechanism means our C++ code can be as exception-safe as Python itself.

Conclusion

By combining the dynamic Inline Compiler with strict RAII Wrappers, JustJIT creates a unique environment. You get the structured, high-performance world of "Native C++" protected by a safety shield, allowing it to exist harmlessly within the organic "Python Code" ecosystem.

It’s not just about speed; it’s about making systems programming feel human.

github:justjit

docs:Justjit