Files
ritz/runtime/ritz.js
T
Aaron Sinclair 635d466874 Implement Python → LLVM IR → WASM reactive framework
MAJOR ACHIEVEMENT: Full working reactive framework!

## What's Working

✅ Python → AST → Ritz IR → LLVM IR → WASM pipeline
✅ Reactive signals stored in WASM memory (@signal_name = global i32)
✅ Event handlers exported from WASM and callable from JS
✅ Clean runtime with RitzLoader and debug tools
✅ Two working examples: HelloWorld and HighFive counter

## Core Components

### Parser (ritz/parser.py)
- Uses Python's ast module to parse components
- Handles @component decorator, signals, event handlers
- Supports yield, with statements, f-strings, augmented assignment (+=)
- Clean syntax: from ritz.syntax import *

### LLVM IR Code Generator (ritz/llvm_codegen_signals.py)
- Generates LLVM IR directly (not C!)
- Signals as global variables in WASM memory
- Exports getter/setter functions for each signal
- Exports event handler functions for JS to call
- UTF-8 string handling

### Build System (ritz/build.py)
- Python → AST → IR → LLVM IR → clang --target=wasm32 → WASM
- Saves intermediate .ll files for inspection
- Debug symbols support (-g flag)

### Runtime (runtime/ritz-runtime.js)
- RitzRuntime class: DOM bridge for WASM
- RitzLoader class: Simplified component loading
- Debug bar with Ctrl+` toggle
- Signal updates trigger DOM updates

## Examples

### HelloWorld.py
Simple static content - renders <p>Hello World</p>

### HighFive.py
Reactive counter with:
- Signal in WASM memory (high_fives = signal(0))
- Event handler (increment function)
- DOM updates on signal change

## Test Coverage

- 4 parser tests (all passing)
- 4 LLVM codegen tests (all passing)
- 4 signal parsing tests (all passing)
- Code formatted with black and ruff

## Next Steps

- <script type="text/ritz"> in-browser compilation
- Better signal-to-DOM binding tracking
- More complex examples (TodoMVC, forms, lists)
- Playwright browser tests

---

This is a genuinely innovative framework: write Python, compile to WASM,
run with reactive signals in WASM memory. No virtual DOM, no transpilation,
just compiled Python with fine-grained reactivity.

🎉 Python → WASM → Reactive UI is REAL!
2025-11-15 01:50:40 -08:00

113 lines
2.6 KiB
JavaScript

/**
* Ritz Runtime - DOM Bridge for WASM
*
* Provides DOM manipulation functions called by WASM code.
*/
class RitzRuntime {
constructor(rootElement) {
this.rootElement = rootElement || document.body;
this.nodes = new Map(); // Map of node IDs to DOM nodes
this.nextNodeId = 1;
this.rootNodeId = null;
}
/**
* Create a DOM element and return its ID
*/
createElement(tag) {
const element = document.createElement(tag);
const id = this.nextNodeId++;
this.nodes.set(id, element);
console.log(`[Ritz] Created element <${tag}> with ID ${id}`);
return id;
}
/**
* Create a text node and return its ID
*/
createTextNode(text) {
const textNode = document.createTextNode(text);
const id = this.nextNodeId++;
this.nodes.set(id, textNode);
console.log(`[Ritz] Created text node "${text}" with ID ${id}`);
return id;
}
/**
* Append child to parent
*/
appendChild(parentId, childId) {
const parent = this.nodes.get(parentId);
const child = this.nodes.get(childId);
if (!parent || !child) {
console.error(`[Ritz] appendChild failed: parent=${parentId}, child=${childId}`);
return;
}
parent.appendChild(child);
console.log(`[Ritz] Appended node ${childId} to node ${parentId}`);
}
/**
* Set an attribute on an element
*/
setAttribute(elemId, name, value) {
const element = this.nodes.get(elemId);
if (!element) {
console.error(`[Ritz] setAttribute failed: element=${elemId}`);
return;
}
// Handle special cases
if (name === 'class') {
element.className = value;
} else {
element.setAttribute(name, value);
}
console.log(`[Ritz] Set attribute ${name}="${value}" on node ${elemId}`);
}
/**
* Set the root element and mount it to DOM
*/
setRoot(elemId) {
const element = this.nodes.get(elemId);
if (!element) {
console.error(`[Ritz] setRoot failed: element=${elemId}`);
return;
}
this.rootNodeId = elemId;
// Clear the root element
this.rootElement.innerHTML = '';
// Mount the component
this.rootElement.appendChild(element);
console.log(`[Ritz] Mounted component (node ${elemId}) to DOM`);
}
/**
* Clear all nodes (for hot reload)
*/
clear() {
this.nodes.clear();
this.nextNodeId = 1;
this.rootNodeId = null;
this.rootElement.innerHTML = '';
}
}
// Export for use in module or global scope
if (typeof module !== 'undefined' && module.exports) {
module.exports = RitzRuntime;
} else {
window.RitzRuntime = RitzRuntime;
}