Skip to content
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

Fixed issue #95. #176

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@
var varStr = 'var %d = Date.now();\n';
var checkStr =
'\nif (Date.now() - %d > 1000) { window.top.previewException(new Error("Infinite loop")); break;}\n';
var infoForEachLoop = [];
var popupStatements = [];

esprima.parse(code, { tolerant: true, range: true, jsx: true }, function(
node
Expand All @@ -109,6 +111,8 @@
--start;
}

infoForEachLoop.push({ start: start, end: end, varName: varPrefix + loopId });

patches.push({ pos: start, str: prolog });
patches.push({ pos: end, str: epilog });
patches.push({
Expand All @@ -118,11 +122,34 @@
++loopId;
break;

case 'ExpressionStatement':
[
"alert",
"confirm",
"prompt"
].forEach(function(functionName){
if (node.expression.callee.name === functionName || (Object.prototype.hasOwnProperty.call(node.expression.callee, "object") && node.expression.callee.object.name === 'window' && node.expression.callee.property.name === functionName)){
popupStatements.push(node);
}
});
break;

default:
break;
}
});

popupStatements.forEach(function(node){
infoForEachLoop.forEach(function(loop){
if (node.range[0]>=loop.start && node.range[0]<=loop.end){
patches.push({
pos: node.range[1],
str: '\n%d = Date.now();\n'.replace('%d', loop.varName)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PythonCow this is basically resetting the loop timer after the alert statement. This means if there is a real infinite loop that has alert statements in it, it would run forever because of this reset. Right?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible solution:

  • Just before alert statement we record time in a variable.
  • After alert, we subtract the time taken by alert from the loop timer variable _wmloopvar.
    That way time taken by alert statement won't ever be counted in the loop's timer.

});
}
});
});

/* eslint-disable no-param-reassign */
patches
.sort(function(a, b) {
Expand Down