refactor: consolidate RVM instruction variants and clean up VM internals (#651)

Merge the three separate Assert* instructions (AssertNot, AssertCondition,
AssertNotUndefined) into a single `Guard { register, mode }` instruction
with a GuardMode enum. This cuts duplicated match arms across display,
listing, parser, dispatch, and all compiler emit sites.

Drop the unnecessary `#[repr(C)]` from the Instruction enum. It was never
exposed across FFI, so the C-compatible 4-byte discriminant was pure waste.
Without it Rust picks a 1-byte discriminant, shrinking every instruction
from 8 bytes to 6. A new `instruction_size` unit test locks this at 6.

While touching these files, also clean up several long-standing issues:

- Deduplicate the iteration-state setup in loops.rs by extracting a shared
  resolve_iteration_state() helper -- the stack-based and stackless paths
  had near-identical 40-line blocks.
- Collapse the ExitWithSuccess / ExitWithFailure match arms into one.
- In rules.rs, stop cloning Arc<Program> just to borrow a RuleInfo -- clone
  the small RuleInfo struct directly and extract a get_rule_info() helper.
- Move the memory check into dispatch (runs per instruction) and remove the
  now-dead enforce_memory_check() entry-point calls.
- Apply map_or_else style throughout listing.rs for consistency.
This commit is contained in:
Anand Krishnamoorthi
2026-04-01 05:34:33 -05:00
committed by GitHub
parent 1a8fc08773
commit 126cc12eb5
17 changed files with 699 additions and 609 deletions
+2 -8
View File
@@ -161,7 +161,6 @@ impl RegoVM {
self.reset_execution_state();
self.reset_execution_timer_state();
self.execution_state = ExecutionState::Running;
self.enforce_memory_check()?;
match self.jump_to(0_u32) {
Ok(value) => {
self.execution_state = ExecutionState::Completed {
@@ -180,7 +179,6 @@ impl RegoVM {
self.reset_execution_state();
self.reset_execution_timer_state();
self.execution_state = ExecutionState::Running;
self.enforce_memory_check()?;
match self.run_stackless_from(0) {
Ok(result) => Ok(result),
Err(err) => {
@@ -193,7 +191,6 @@ impl RegoVM {
fn execute_suspendable_entry(&mut self, entry_point_pc: usize) -> Result<Value> {
self.execution_state = ExecutionState::Running;
self.reset_execution_timer_state();
self.enforce_memory_check()?;
match self.run_stackless_from(entry_point_pc) {
Ok(result) => Ok(result),
Err(err) => {
@@ -204,18 +201,15 @@ impl RegoVM {
}
pub fn resume(&mut self, resume_value: Option<Value>) -> Result<Value> {
let old_state = core::mem::replace(&mut self.execution_state, ExecutionState::Running);
let (reason, mut last_result) = match old_state {
let (reason, mut last_result) = match self.execution_state.clone() {
ExecutionState::Suspended {
reason,
last_result,
..
} => (reason, last_result),
current_state => {
let desc = alloc::format!("{:?}", current_state);
self.execution_state = current_state;
return Err(VmError::InvalidResumeState {
state: desc,
state: alloc::format!("{:?}", current_state),
pc: self.pc,
});
}