Search code examples
javascriptgeneratorv8destructuring

implement infinite lists in v8


I'm a computer science student and as part of a school projet I have been asked to either find an exploit in the v8 engine, make some really good optimisation or add a new feature.

I chose to add a new feature and here it is:

function* numbers() {
  i = 1;
  while (true) {
    yield i++;
  }
}

var gen = numbers();

var l = [...gen];

var n = l[42];

Putting it in words I want to have the possibility to use the destructuring syntax to create a list that can hold an infinite number of objects and access them.

It's possible to do it in Haskell and I want to try and do the same with JavaScript.

If of the developers at v8 could point me in the right direction it would be so great.

I already have a working environment, can compile the engine, read the source code, and run the debugger on the d8 binary file with symbols.


Solution

  • V8 developer here.

    First off: just to be clear, stackoverflow is not a machine that does your homework. (You're only asking for "the right direction", that's okay.)

    Secondly: V8 implements JavaScript as spec'ed, so any arbitrary "new feature" is not going to land in our repository, please be aware of that.

    Thirdly: Keith has several good points. In particular, the syntax you propose is already valid JavaScript and eagerly evaluates the generator. Was your idea to switch to lazy evaluation iff the generator produces an infinite stream of values? Take a step back and think about the implications of that idea for a minute.

    Finally, if you come up with workable syntax/semantics, then it'll still be a chunk of work to do this in V8, because there's no precedent of something similar. You'd probably want to use an elements interceptor, and store the generator in a private property. I think it would be much easier to polyfill the whole thing in pure JavaScript using a Proxy.

    (It might be a good idea to reconsider your choice of project, but that's up to you. It's also quite a funky project description to begin with... what do they think how hard it is to "find an exploit or make some really good optimisation"? Do let us know if you find an exploit though!)