[app] Transpiling ES6 generator functions
gnh1201 opened this issue · comments
Summary
WelsonJS supports various types of transpilers (e.g., TypeScript, CoffeeScript, etc.). Especially, TypeScript, which is still actively updated, can be used along with WelsonJS.
However, if the transpilation results in ES6 generators (function*), WelsonJS cannot interpret them directly. Additional transpilation steps are required to handle ES6 generators effectively.
Related Issues
Related links
I have implemented a GeneratorFunction
available at the ES3 syntax. Currently, this code does not track the number of times yield
is used, so done
is always false. After outputting all values, it restarts from the initial value.
function GeneratorFunction(f) {
var _lastState = 0;
var _state = 0;
var _yield = function(value) {
_state++;
if (_state > _lastState) {
throw new GeneratorFunction.Yield(value);
}
};
this.next = function() {
var go = true;
var value = undefined;
_state = 0;
while (go) {
try {
f(_yield);
} catch (e) {
if (e instanceof GeneratorFunction.Yield) {
value = e.message;
go = false;
_lastState = _state;
} else {
console.error(e.message);
}
}
}
return {
"value": value,
"done": false
}
};
}
GeneratorFunction.Yield = function(message) {
this.name = "GeneratorFunction.Yield";
this.message = message;
};
GeneratorFunction.Yield.prototype = new Error();
GeneratorFunction.Yield.prototype.constructor = GeneratorFunction.Yield;
var a = new GeneratorFunction(function(_yield) {
_yield("a");
_yield("b");
_yield("c");
});
console.log(a.next().value);
console.log(a.next().value);
console.log(a.next().value);
I have confirmed that my implementation is working correctly. I will close this issue.