Open Thomas-Langlais opened 2 years ago
If i want to stop the interpret loop. It needs to be aware of a stop flag that we can set outside of the async loop to trigger it to stop the execution.
It needs to have a condition in here
impl<'a> Interpret<'a> for SyntaxNode {
fn interpret(&self, context: &mut ExecutionContext) -> Result {
// add the exit condition here
match self {
Self::If(node) => node.interpret(context),
Self::Statements(node) => node.interpret(context),
Self::Statement(node) => node.interpret(context),
Self::For(node) => node.interpret(context),
Self::While(node) => node.interpret(context),
Self::Continue(_) => Ok(InterpretedType::Continue),
Self::Break(_) => Ok(InterpretedType::Break),
Self::Variable(node) => node.interpret(context),
Self::Factor(node) => node.interpret(context),
Self::Unary(node) => node.interpret(context),
Self::Term(node) => node.interpret(context),
}
}
}
This way, whenever the interpret loop hits a syntax node, we can check the exit state and exit early if requested.
Scratch that, it won't matter because running wasm in the main thread (e.g. executing the tlang code) will always block input. in order to not block the main thread, I will need to run the interpreter on a web worker. This way, we can send messages to execute and stop the interpreter from the main thread.
Unfortunately even running the interpreter would not solve the problem.
If I did implement the interpreter in a webworker, and executed an infinite loop in a promise. The web worker thread wouldn't be able to respond to a new postmessage event because it would still be looping infinitely.
There is no way around this problem in a wasm environment. However, we could detect that the webworker is not responsive, and could add a feature to reset the terminal.
https://rustwasm.github.io/wasm-bindgen/reference/js-promises-and-rust-futures.html
The wasm build needs to be asynchronous so that the demo can force the program to exit. This is for the case when the program loops forever.