duktape
duktape copied to clipboard
can't use "yield" in portable code because of parse error
I am trying to create some portable code that uses either ES6 generators or Duktape coroutines, depending on runtime tests to determine which method to use.
However, there are 2 syntax errors in the following, which prevents Duktape from even compiling the code that will not be executed. It seems there are 2 simple changes in the parser that could allow this to work.
- . Make function* a (trivial) alias for function
- . Make yield x an alias for either (Duktape.Thread.yield(x)) - or just throw an exception if invoked. But allowing it to compile without parse errors would help with this abstraction.
Any suggestions?
"use strict";
var mygen;
if (global.Duktape) {
mygen = function() {
Duktape.Thread.yield(5);
}
} else {
mygen = function*() {
yield 5;
}
}
Elsewhere I can do a similar abstraction over Duktape.Thread.resume and the ES6 iterator interface. But this does not cause compile errors and can be done already.
Well. this is a horrible hack but I inserted a regular expression replacer which runs over the string source code when loading a file. The duktape_preprocess function is invoked by the C code that loads in a new source file.
global.yield0 = Duktape.Thread.yield;
function duktape_preprocess(source) {
source = source.replace(/function\*/g, "function");
source = source.replace(/yield\*/g, "");
var re = new RegExp("yield\ (.+);", "gm");
return source.replace(re, "yield0($1);");
}```
Duktape doesn't yet support the yield
keyword, so you'll in essence need transpiling.