1

Is there a way to do continuations in V8? If not, is there another JavaScript engine for C++ that can do so?

I want to write an application that makes use of a JavaScript interpreter that supports continuations, much like Rhino does in Java.

This is primarily for game development, where it's incredibly useful (or even essential?) that a script be able to "pause" until the game engine gives it control again-- for example, waiting for a player's input before a cutscene resumes its chain of events.

V8 sounds like the most promising JavaScript engine for C++ applications right now, but from the resources I was able to find, V8 had no plans to support continuations. These posts were from 4-6 years ago, though, so they may or may not be up to date.

Community
  • 1
  • 1
Josh1billion
  • 14,837
  • 8
  • 38
  • 48

1 Answers1

4

The new version of JavaScript, ES6, has continuation-like features under the name of generators. I believe V8 should have generator support by now. If not, you can use a JavaScript transcompiler like babeljs.

  • I'd never heard of generators before, but whoa, they sound perfect! It looks like V8 does support generators now too, as a simple example ran fine for me in a Chrome console. – Josh1billion Apr 06 '15 at 04:56
  • 2
    Note however that generators intentionally only provide _shallow_ _one-shot_ continuations. AFAICT, there is no JS implementation that would give you general continuations. – Andreas Rossberg Apr 06 '15 at 08:58
  • @AndreasRossberg What do you mean when saying the continuations are shallow? Do you mean certain aspects of the script's state won't persist after the script is continued? – Josh1billion Apr 06 '15 at 15:38
  • 1
    @Josh1billion, shallow means that they only capture the continuation for the current function (rather, generator). In other words, just the top frame of the call stack, not the whole stack. – Andreas Rossberg Apr 06 '15 at 16:24
  • 1
    @Josh1billion For practical advice on how to use generators, I suggest you look at the design of http://taskjs.org/ (Taskjs is no longer compatable with the current generator api, but the same general architecture applies). The basic idea is that you yield promises. The main loop then resumes the function when the promise is resolved. –  Apr 06 '15 at 20:37