Skip to content

Line numbers are off in for/of #1266

@bnoordhuis

Description

@bnoordhuis

Observable when an iterator's .return method produces a stack trace.

var iter = {
    next() {
        return {value:"."[this.i], done:!!this.i++}
    },
    return() {
        print(new Error().stack)
        return {value:undefined, done:true}
    },
    i: 0,
}
var iterable = {[Symbol.iterator](){ return iter }}
for (var _ of iterable) break

Prints:

    at return (u.js:6:19)      // okay
    at <anonymous> (u.js:1:1)  // WRONG

The relevant bytecode with qjs --script -D2:

  111:  get_var         iterable
        for_of_start   
        goto            2:152
        label           5:127
  127:  put_var         _
        goto            3:142
        label           3:142
  142:  goto            4:165
        label           2:152
  152:  for_of_next     0
        if_false        5:127
        drop           
        label           4:165
  165:  iterator_close 
        get_loc         0    ; "<ret>"
        return         

Note how there are no source_loc opcodes anywhere to be seen. There should be one around iterator_close.

Change the body of the loop so it's not functionally empty...

for (var _ of iterable) { Math; break }

...and the right line numbers appear:

    at return (u.js:6:19)
    at <anonymous> (u.js:12:27)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions