duktape icon indicating copy to clipboard operation
duktape copied to clipboard

can't use "yield" in portable code because of parse error

Open lpkruger opened this issue 5 years ago • 2 comments

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.

  1. . Make function* a (trivial) alias for function
  2. . 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.

lpkruger avatar Oct 23 '19 21:10 lpkruger

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);");
}```

lpkruger avatar Oct 24 '19 16:10 lpkruger

Duktape doesn't yet support the yield keyword, so you'll in essence need transpiling.

svaarala avatar Oct 25 '19 14:10 svaarala