rhaiscript / rhaiscript/rhai

How do I checkpoint and resume Rhai evaluation?

Open
#769 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Rust
Stars
5.7k
Forks
249
Avg merge
1d 14h
Merged PRs (30d)
23

Description

I want something like this

use rhai::{Engine, EvalAltResult, Position};

struct MyContext {
    e: Engine,
}

impl MyContext {
    fn new() -> MyContext {
        let mut e = Engine::new();
        fn suspend() -> Result<i32, Box<EvalAltResult>> {
            let resumed = true; // ?
            if resumed {
                Ok(3 /* assuming getting from some channel */)
            } else {
                // How to I save execution context to be able to return twice?
                Err(Box::new(EvalAltResult::ErrorTerminated(().into(), Position::NONE)))
            }
        }
        
        e.register_fn("suspend", suspend);
        MyContext { e }
    }
    fn start(&self) {
        let _ = self.e.eval::<()>(r#"
            print(1);
            print(suspend());
            print(4);
        "#);
    }
    fn resume(&self, _x: i32) {
        // (Assuming sending _x to the channel)
        
        // ?
    }
}

fn main()
{
    let state = MyContext::new();
    state.start();
    // Rhai evaluation stack frames should not be absent. It should be in inert, "frozen" state.
    // N suspended evaluations should not occupy N operation system threads.
    println!("2");
    state.resume(3);
}

It should print 1\n2\n3\n4\n.

Is there some example (e.g. using internals feature) to attain this?

Alternatively, is there something like "call with current continuation" in Rhai, to avoid each suspension point becoming a closure in Rhai code (i.e. callback hell)?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source files, tests, or entry points are named. Start by examining Rhai's Engine evaluation internals and the internals feature, then determine whether suspended evaluation state can be preserved without occupying an operating-system thread; done means a working checkpoint/resume example producing 1\n2\n3\n4\n.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.