The current compiler builds AST nodes into callable execution nodes. For async support, the same general idea could be kept, but each compiled async node/frame would need enough state to resume from the exact substep it stopped on instead of restarting the parent block from the beginning.
Each async node would return a status instead of directly finishing execution:
enum ASYNC_STATUS {
DONE,
WAIT,
CALL,
ERROR
}
Each async step would track runtime state:
current node/step
locals
arguments
return value
flow mask
wait state
Execution would be driven by a scheduler each Step event. The scheduler runs a thread until it finishes, yields, errors, or hits a per-frame work budget. When a node yields, the frame keeps its current step. On the next scheduler update, execution resumes directly from that step.
Example behavior:
block statement:
step 0 -> run statement 0
step 1 -> run statement 1
step 2 -> run statement 2
If statement 1 yields, the frame stores step = 1. The next frame resumes from statement 1, not statement 0.
This should allow the compiler to support green-thread-style execution without requiring a bytecode backend.
A lot of this code could be pulled from my async promise library.
The current compiler builds AST nodes into callable execution nodes. For async support, the same general idea could be kept, but each compiled async node/frame would need enough state to resume from the exact substep it stopped on instead of restarting the parent block from the beginning.
Each async node would return a status instead of directly finishing execution:
Each async step would track runtime state:
Execution would be driven by a scheduler each Step event. The scheduler runs a thread until it finishes, yields, errors, or hits a per-frame work budget. When a node yields, the frame keeps its current step. On the next scheduler update, execution resumes directly from that step.
Example behavior:
If statement 1 yields, the frame stores
step = 1. The next frame resumes from statement 1, not statement 0.This should allow the compiler to support green-thread-style execution without requiring a bytecode backend.
A lot of this code could be pulled from my async promise library.