refactor: Overhaul Rhai scripting with multi-file hot reloading

This commit represents a major refactoring of our Rhai scripting system,
transforming it from a factory-based approach to a more robust system-based
architecture with improved hot reloading capabilities.

Key Changes:
- Renamed package from rhai_factory to rhai_system to better reflect its purpose
- Renamed system_factory.rs to factory.rs for consistency and clarity
- Implemented support for multiple script files in hot reloading
- Added cross-script function calls, allowing functions in one script to call functions in another
- Improved file watching to monitor all script files for changes
- Enhanced error handling for script compilation failures
- Simplified the API with a cleaner create_hot_reloadable_system function
- Removed unused modules (error.rs, factory.rs, hot_reload_old.rs, module_cache.rs, relative_resolver.rs)
- Updated all tests to work with the new architecture

The new architecture:
- Uses a System struct that holds references to script paths and provides a clean API
- Compiles and merges multiple Rhai script files into a single AST
- Automatically detects changes to any script file and recompiles them
- Maintains thread safety with proper synchronization primitives
- Provides better error messages when scripts fail to compile

This refactoring aligns with our BasePathModuleResolver approach for module imports,
making the resolution process more predictable and consistent. The hot reload example
has been updated to demonstrate the new capabilities, showing how to:
1. Load and execute multiple script files
2. Watch for changes to these files
3. Automatically reload scripts when they change
4. Call functions across different script files

All tests are passing, and the example demonstrates the improved functionality.
This commit is contained in:
Timur Gordon
2025-05-02 21:04:33 +02:00
parent 939b6b4e57
commit 372b7a2772
54 changed files with 5692 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
// Calendar controller module
// This demonstrates importing from a different directory level
// Import the common utils using a relative path
import "../../../utils/common" as common;
/// Returns data for a single calendar event
fn get_event_data() {
let event = #{
id: 1,
title: "Team Meeting",
date: "2025-04-15",
time: "10:00 AM",
location: "Conference Room A",
description: "Weekly team sync meeting"
};
// Use the common utils to check if the date is in the future
if common::is_future_date(event.date) {
event.status = "Upcoming";
} else {
event.status = "Past";
}
return event;
}
/// Returns a list of all calendar events
fn get_all_events() {
let events = [
#{
id: 1,
title: "Team Meeting",
date: "2025-04-15",
time: "10:00 AM",
location: "Conference Room A",
description: "Weekly team sync meeting"
},
#{
id: 2,
title: "Project Review",
date: "2025-04-17",
time: "2:00 PM",
location: "Meeting Room B",
description: "Review project progress and next steps"
},
#{
id: 3,
title: "Client Presentation",
date: "2025-04-20",
time: "11:30 AM",
location: "Main Auditorium",
description: "Present new features to the client"
}
];
// Update the status of each event using the common utils
for event in events {
if common::is_future_date(event.date) {
event.status = "Upcoming";
} else {
event.status = "Past";
}
}
return events;
}

View File

@@ -0,0 +1,23 @@
// Import modules using relative paths
import "utils/common" as common;
import "components/calendar/controller/calendar" as calendar;
// Main function that demonstrates the relative imports
fn main() {
// Print a message using the common utils
let greeting = common::get_greeting("User");
print(greeting);
// Get calendar events using the calendar controller
let events = calendar::get_all_events();
// Print the events using the common utils format function
print("\nCalendar Events:");
for event in events {
let formatted = common::format_event(event);
print(formatted);
}
// Return a success message
"All imports worked correctly!"
}

View File

@@ -0,0 +1,18 @@
// Common utility functions used across the application
/// Returns a greeting message for the given name
fn get_greeting(name) {
return `Hello, ${name}! Welcome to the Rhai relative imports example.`;
}
/// Formats an event object into a readable string
fn format_event(event) {
return `- ${event.title} on ${event.date} at ${event.time} (${event.location})`;
}
/// Utility function to check if a date is in the future
fn is_future_date(date_str) {
// Simple implementation for the example
// In a real application, you would parse the date and compare with current date
return true;
}