njs icon indicating copy to clipboard operation
njs copied to clipboard

for-in parser issue

Open drsm opened this issue 5 years ago • 2 comments

// "in" <Expression> ")" <Statement>
>> var a = [1,2,3]
undefined
>> for (var x in a);
undefined
>> for (var x in 1,a);
undefined
>> for (x in a);
undefined
>> for (x in 1,a);
Thrown:
SyntaxError: Unexpected token ")" in shell:1

drsm avatar Oct 30 '20 22:10 drsm

@drsm

  1. The in priority is higher than ,.
  2. Take a look at the reference about for-in statement. https://tc39.es/ecma262/#sec-for-in-and-for-of-statements-static-semantics-early-errors
  3. Another example. It works well in v8.
var x, a, b
a = [1, 2, 3]
b = ['a', 'b']
for (x in a in b) console.log(b[x])

But throw error in njs.

ReferenceError: Invalid left-hand side "x" in for-in statement in 1.js:4

Deep into the source code in the case of for-in.

njs_parser_iteration_statement_for_map(...)
{
     ...
     default:
        njs_parser_next(parser, njs_parser_expression);
        break;
    }

    ...

    return njs_parser_after(parser, current, text, 1,
                            njs_parser_for_var_in_of_expression);
}

What I mean is that, the keyword in should be checked before parsing expression. But now it does not.

hongzhidao avatar Oct 31 '20 14:10 hongzhidao

@hongzhidao

found another weird case:

>> for (var x = x in [1,2]; !x; x = 1) console.log(x);
false
undefined

vs.

> for (var x = x in [1,2]; !x; x = 1) console.log(x);
for (var x = x in [1,2]; !x; x = 1) console.log(x);
                       ^

Uncaught SyntaxError: Unexpected token ';'

drsm avatar Oct 31 '20 15:10 drsm