Open Type1J opened 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
.
Sounds good. I'm not sure how a call to .yield() will make the calling function yield, though.
Oh. After looking at the Mozilla GeneratorFunction object, I see what you mean. Is there a TypeScript or Babel backend that can target that?
I Just figured I would add a few tasks to this bug report to be able to hopefully get this implemented...
[x] Rename this issue to Implement ES2015 Generator Functions.
[ ] Add support for Generator Functions ( Direct Use)
// ES2015 Style:
function* range (start, end, step) {
while (start < end) {
yield start;
start += step;
}
}
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
}
// ES2015
let fibonacci = {
*[Symbol.iterator]() {
let pre = 0, cur = 1;
for (;;) {
[ pre, cur ] = [ cur, pre + cur ];
yield cur;
}
}
for (let n of fibonacci) {
if (n > 1000) break;
console.log(n);
}
// 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);
}
let fibonacci = function* (numbers) {
let pre = 0, cur = 1;
while (numbers-- > 0) {
[ pre, cur ] = [ cur, pre + cur ];
yield cur;
}
};
// 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
class Clz {
* bar () { /* Add Generator Code Here*/}
};
Note This Task depends on issue #276
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 :)
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.