svaarala / duktape

Duktape - embeddable Javascript engine with a focus on portability and compact footprint
MIT License
5.96k stars 516 forks source link

Implement ES2015 generator functions #1012

Open Type1J opened 8 years ago

Type1J commented 8 years ago

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*

DukTape.Thread supports everything to make this work already, so it would seem to just be a matter of syntax sugar, but TypeScript and Babel can generate very readable async/await code with generators as the backend.

svaarala commented 8 years ago

Like other ES6 features this will be implemented at some point so no objection there :)

One interesting possibility would be to actually remove Duktape.Thread binding and use e.g. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/GeneratorFunction instead. It's not 1:1, but maybe Duktape threads could be implemented on top of the standard bindings as extension rather than providing two similar but different bindings.

A similar change was recently done for Duktape.Buffer which has been replaced by ArrayBuffer and the custom plain buffer type mimics ArrayBuffer rather than Duktape.Buffer.

Type1J commented 8 years ago

Sounds good. I'm not sure how a call to .yield() will make the calling function yield, though.

Type1J commented 8 years ago

Oh. After looking at the Mozilla GeneratorFunction object, I see what you mean. Is there a TypeScript or Babel backend that can target that?

kphillisjr commented 7 years ago

I Just figured I would add a few tasks to this bug report to be able to hopefully get this implemented...

for (let i of range(0, 10, 2)) { console.log(i); // 0, 2, 4, 6, 8 }


``` JavaScript
// ES5 Old Style
function range (start, end, step) {
    var list = [];
    while (start < end) {
        list.push(start);
        start += step;
    }
    return list;
}

var r = range(0, 10, 2);
for (var i = 0; i < r.length; i++) {
    console.log(r[i]); // 0, 2, 4, 6, 8
}
// Alternative approach using static function
var fibonacci = {
    next: (function () {
        var pre = 0, cur = 1;
        return function () {
            tmp = pre;
            pre = cur;
            cur += tmp;
            return cur;
        };
    })()
};

var n;
for (;;) {
    n = fibonacci.next();
    if (n > 1000)
        break;
    console.log(n);
}

// Iterate a loop using Generator Functions. for (let n of fibonacci(1000)) console.log(n);

// Initialize a variable using Geneator Functions. let [ n1, n2, n3, ...others ] = fibonacci(1000); // Fill array with Generator Content. let numbers = [ ...fibonacci(1000) ];

**Note:** This task depends on issue #1739

- [ ] Add Support for Generator functions in Objects
```Javascript
let Obj = {
    * foo () { /* See above */ }
};

Note: This is a repeat of the last task in issue #271

svaarala commented 7 years ago

I Just figured I would add a few tasks to this bug report to be able to hopefully get this implemented...

Thanks. The bottleneck in implementing the full ES2015 feature set is not that there aren't github issues tracking the feature gap, but simply finding the time to work on the features. So rest assured the ES2015 features are all targeted to be implemented at some point :)