-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong stack trace in errors shown in combination of for..of loops and JsSetException #6314
Comments
See #3685. |
Yeah the problem is the internal workings of for...of which also cause #3685 - though the fix may not be the same. In the bytecode emitter for of loops are re-written as follows: Input JS: for (let x of [1,2,3]) {
crash();
} Re-written version (roughly - may have missed a condition somewhere): const iterator = [1,2,3][Symbol.iterator]();
const next = iterator.next;
let shouldCallReturn = false;
try {
while(true)
{
shouldCallReturn = false;
const result = next.call(iterator);
if (! (result instanceof Object))
throw new TypeError("Iterator should return an object");
if (result.done)
break;
let x = result.value;
shouldCallReturn = true;
crash();
}
} catch (e) {
if (shouldCallReturn) {
try { iterator.return(); shouldCallReturn = false; } catch (e) {}
}
throw e;
} finally {
if (shouldCallReturn) {
if (typeof iterator.return === "function") {
const result = iterator.return();
if (! (result instanceof Object))
throw new TypeError("Iterator should return an object");
}
} Yes that is what the spec wants for a for...of implemented using simpler operations - chakracore does it this way as it's easier for the JIT to optimise than using a more complex operation. The problem is that the exception handling gets a little confused - as it thinks there's a try/catch/finally block when the original code didn't contain it. |
@rhuanjl |
@goozie81 That's right. The bytecode for
|
Thinking about this - the only issue here is that the stack trace is wrong - so just need some way to capture the stack trace from the initial throw rather than re-generating it after calling .return I figure that that should not be too hard. |
Note however that will fix this issue but won’t fix #3685. |
When throwing an error using JsSetException inside a for..of loop error.stack does point to the last line of the for loop instead of the line which has caused the real exception.
for..in however does work properly
throwing inside JavaScript works properly even inside for..of loop
try..catch and rethrow works properly
Example "for..of":
error.stack:
Example "for..in":
error.stack:
Example "rethrow":
error.stack:
throwing the error in C#:
The text was updated successfully, but these errors were encountered: