rvirding / luerl

Lua in Erlang
Apache License 2.0
1.02k stars 140 forks source link

get_stacktrace overwrites line number when inside a for loop: #163

Open markmeeus opened 9 months ago

markmeeus commented 9 months ago

Give this script

print("line1")
for i=1,10 do    
    print("line3")
    noSuchFunction()
end

The call to nuSuchFunction() throws an error, at which point the luerl stack looks like this:

[
   {:call_frame, nil, [], [{1}, {[]}], [:not_used, :not_used],
    [:pop, :block_close],
    [
      [
        {:nfor_loop, 2, 10, 1,
         [
           {:block_open, 1, 0},
           {:store_lvar, 1, 1},
           {:current_line, 3, "-no-file-"},
           {:push_gvar, "print"},
           {:push_last_lit, "line3"},
           {:pop_args, 1},
           :fcall,
           :pop,
           {:current_line, 4, "-no-file-"},
           {:push_gvar, "noSuchFunction"},
           {:pop_args, 0},
           :fcall,
           :pop,
           :block_close
         ]},
        {:return, 0}
      ]
    ]},
   {:current_line, 4, "-no-file-"},
   {:loop_frame, [{[]}], [], [:not_used], [return: 0], []},
   {:current_line, 2, "-no-file-"},
   {:call_frame, {:funref, 0, []}, [], [], [], [], []}
 ]

however, the call to get_stacktrace returns:

[
  {nil, [], [file: "-no-file-", line: 1]},
  {"-no-name-", [], [file: "-no-file-", line: 2]}
]

I was expecting the second stack trace line number to be 4 (where the error is thrown), not 2

I think this behavior is caused by the implementation of do_stackframe. The loop_frame is ignored there, I think they should either become a stack trace line because otherwise one of the current_line instructions is lost.