Generator functions in Sciter and tiscript.

Generator function is a function that produce sequence of values. Each call of such function returns next value of some sequence.

Here is one possible implementation of generator function that can be used in Sciter. Idea of this implementation is borrowed from LUA programming language.

Let’s say we would like to enumerate some array in backward direction using for( .. in .. ) statement:

var a = [0,1,2,3,4,5,6,7,8,9];

for(var n in backward(a)) 
    stdout.printf("%d\n", n);

Here is an implementation of such backward() function:

    function backward(a)
      var n = a.length;
      return function() { if(--n >= 0) return a[n]; }

As you may see this function initializes n variable and returns reference to the inner function.
This inner function will supply next value on each its invocation. In this example it will return a[–n] (previous element of the array) or nothing.
This implementation employs the fact that inner function has access to local variables of outer function – closure in other words.

To be able to support this in the language I have extended for(var el in collection) statement.
So collection object here can be function, array and object.