Expose loop transfers through versioned public observations
This commit is contained in:
1 parent
69c5dc8d20
commit
71d29261a8
21 files changed
+486
-35
No files matched your search
@@ -4,7 +4,8 @@ use std::collections::BTreeMap;
|
||||
|
||||
use marqdo_expression::{BinaryOp, Expr, RecordEntry};
|
||||
use marqdo_host::{
|
||||
ExecutionContext, FrameId, Host, HostOp, ParameterSource, ResourceRead, RuntimeObservation,
|
||||
ExecutionContext, FrameId, Host, HostOp, LoopTransfer, ParameterSource, ResourceRead,
|
||||
RuntimeObservation,
|
||||
};
|
||||
use marqdo_ir::{builtins, Function, FunctionId, ModuleId, PlaceTarget, Program, Stmt};
|
||||
use marqdo_span::Span;
|
||||
@@ -71,8 +72,7 @@ struct BoundArgs {
|
||||
enum Flow {
|
||||
Next,
|
||||
Return(Value),
|
||||
Break(Span),
|
||||
Continue(Span),
|
||||
LoopTransfer(LoopTransfer, Span),
|
||||
}
|
||||
|
||||
/// Marqdo tree-walk interpreter.
|
||||
@@ -232,7 +232,7 @@ impl<'a> Interpreter<'a> {
|
||||
.and_then(|flow| match flow {
|
||||
Flow::Next => Ok(None),
|
||||
Flow::Return(value) => Ok(Some(value)),
|
||||
Flow::Break(span) | Flow::Continue(span) => Err(RuntimeError::new(
|
||||
Flow::LoopTransfer(_, span) => Err(RuntimeError::new(
|
||||
ErrorCategory::Internal,
|
||||
Some(span),
|
||||
"loop control escaped its enclosing function",
|
||||
@@ -420,8 +420,31 @@ impl<'a> Interpreter<'a> {
|
||||
Stmt::Eval { expr, .. } => {
|
||||
self.eval_expr(expr, frame)?;
|
||||
}
|
||||
Stmt::Break { span } => return Ok(Flow::Break(*span)),
|
||||
Stmt::Continue { span } => return Ok(Flow::Continue(*span)),
|
||||
Stmt::Break { span } | Stmt::Continue { span } => {
|
||||
let transfer = if matches!(stmt, Stmt::Break { .. }) {
|
||||
LoopTransfer::Break
|
||||
} else {
|
||||
LoopTransfer::Continue
|
||||
};
|
||||
let module = frame.function.module.canonical();
|
||||
self.host.record_semantic(
|
||||
transfer.event_kind(),
|
||||
Some(&frame.function.to_string()),
|
||||
Some(&module),
|
||||
Some(*span),
|
||||
None,
|
||||
);
|
||||
if self.observe {
|
||||
self.host
|
||||
.observe_runtime(RuntimeObservation::LoopTransfer {
|
||||
frame_id: frame.id,
|
||||
transfer,
|
||||
module_id: &module,
|
||||
span: *span,
|
||||
})?;
|
||||
}
|
||||
return Ok(Flow::LoopTransfer(transfer, *span));
|
||||
}
|
||||
Stmt::Return { value, span } => {
|
||||
let value = self.eval_expr(value, frame)?;
|
||||
let module = frame.function.module.canonical();
|
||||
@@ -515,8 +538,8 @@ impl<'a> Interpreter<'a> {
|
||||
break;
|
||||
}
|
||||
match self.exec_stmts(program, body, frame)? {
|
||||
Flow::Next | Flow::Continue(_) => {}
|
||||
Flow::Break(_) => break,
|
||||
Flow::Next | Flow::LoopTransfer(LoopTransfer::Continue, _) => {}
|
||||
Flow::LoopTransfer(LoopTransfer::Break, _) => break,
|
||||
flow @ Flow::Return(_) => return Ok(flow),
|
||||
}
|
||||
},
|
||||
|
||||
@@ -249,3 +249,46 @@ fn memory_budget_is_enforced_without_changing_the_region() {
|
||||
.unwrap_err();
|
||||
assert_eq!(error.code(), "resource.exhausted");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn continue_obeys_fuel_and_wall_clock_budgets() {
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
let source = "## @exec main\n\n`while true`\n\n`continue`\n\n`end`\n";
|
||||
let program = build(source);
|
||||
let module = ModuleId::from("index.mq.md");
|
||||
|
||||
let mut fuel_host = host();
|
||||
fuel_host.budget.fuel = 20;
|
||||
let fuel = run(&program, &mut fuel_host, &module, "main", Vec::new()).unwrap_err();
|
||||
assert_eq!(fuel.code(), "resource.exhausted");
|
||||
assert!(fuel.message.contains("fuel exhausted"));
|
||||
|
||||
let mut deadline_host = host();
|
||||
deadline_host.budget.fuel = u64::MAX;
|
||||
deadline_host.budget.deadline = Some(Instant::now() + Duration::from_millis(25));
|
||||
let timeout = run(&program, &mut deadline_host, &module, "main", Vec::new()).unwrap_err();
|
||||
assert_eq!(timeout.code(), "resource.exhausted");
|
||||
assert!(timeout.message.contains("wall-clock time limit"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_ir_cannot_transfer_into_the_callers_loop() {
|
||||
let source =
|
||||
"## @exec child\n\n`return 1`\n\n## @exec main\n\n`while true`\n\n`child()`\n\n`end`\n";
|
||||
let mut program = build(source);
|
||||
let module = ModuleId::from("index.mq.md");
|
||||
let child = program
|
||||
.modules
|
||||
.get_mut(&module)
|
||||
.unwrap()
|
||||
.functions
|
||||
.iter_mut()
|
||||
.find(|function| function.id.name == "child")
|
||||
.unwrap();
|
||||
let span = child.body[0].span();
|
||||
child.body[0] = marqdo_ir::Stmt::Break { span };
|
||||
let error = run(&program, &mut host(), &module, "main", Vec::new()).unwrap_err();
|
||||
assert_eq!(error.category, marqdo_value::ErrorCategory::Internal);
|
||||
assert_eq!(error.span, Some(span));
|
||||
}
|
||||
@@ -93,6 +93,18 @@ impl ObservationSink for Recorder {
|
||||
source: None,
|
||||
decision: decision.to_string(),
|
||||
},
|
||||
ObservationEvent::LoopTransfer {
|
||||
frame_id, transfer, ..
|
||||
} => Seen {
|
||||
kind: transfer.event_kind(),
|
||||
frame: Some(frame_id.0),
|
||||
name: String::new(),
|
||||
value_type: String::new(),
|
||||
state: None,
|
||||
wire: None,
|
||||
source: None,
|
||||
decision: String::new(),
|
||||
},
|
||||
ObservationEvent::VariableAssigned {
|
||||
frame_id,
|
||||
name,
|
||||
|
||||
Reference in new issue
Block a user