Resource Limits

Untrusted or user-authored scripts must be bounded. Dogma provides two limits.

Operation limit🔗

Caps the total number of VM operations a script may execute. Every instruction (assignment, function call, comparison) counts as one operation.

Engine::builder()
    .with_max_operations(100_000)
    .build()

When the limit is reached, the script is terminated with an error. This prevents infinite loops and runaway computation.

Recommended values:

Use caseSuggested limit
Simple config scripts10_000
Game logic per frame100_000
Complex one-shot scripts1_000_000

Memory limit🔗

Caps the heap memory the script may allocate.

Engine::builder()
    .with_max_memory(4 * 1024 * 1024)   // 4 MB
    .build()

Handling limit errors🔗

Resource limit violations return Err from engine.run_file() / engine.run_source():

match engine.run_file(Path::new("script.dg")) {
    Ok(v) => { /* script returned v */ }
    Err(e) => eprintln!("Script error: {e}"),
}