Skip to main content

Generators and Time-Sharing

· 8 min read

The ability to pause, resume, and timeshare evaluation in StaticJs was a key design decision from its inception. The original impetus was to create a sandboxed language that could run reasonably well with untrusted code, and part of that meant not hanging forever if the code enters an infinite loop.

Beyond infinite loops, being able to suspend and resume whole call stacks carried several benefits:

  • Step-through debugging support.
  • Time-sharing evaluation (sharing time between running the sandbox and running the host browser).
  • Tracking and limiting the number of operations; "Script budgets".
  • Ensuring support for Async / Generator functions, that suspend as a matter of course.
  • The ability to interrupt and cancel script evaluations.

While the idea of being able to arbitrarily pause an entire call stack seems daunting, there is in fact a rather simple JavaScript feature that makes it quite an easy task, provided you design your system around it ahead of time.