1
0
mirror of https://github.com/chinchang/web-maker.git synced 2025-07-12 17:46:26 +02:00

add setting to change infinite loop detection timeout. fixes #188;

This commit is contained in:
Kushagra Gour
2018-05-05 23:08:03 +05:30
parent 7b255ceca8
commit dd4a11d059
3 changed files with 28 additions and 8 deletions

View File

@ -535,6 +535,16 @@
<input type="checkbox" d-change="updateSetting" data-setting="isCodeBlastOn"> Code blast! <input type="checkbox" d-change="updateSetting" data-setting="isCodeBlastOn"> Code blast!
</label> </label>
</p> </p>
<hr>
<h3>Advanced</h3>
<p>
<label class="line" title="This timeout is used to indentify a possible infinite loop and prevent it.">
Maximum time allowed in a loop iteration
<input type="number" data-setting="infiniteLoopTimeout" d-change="updateSetting"> ms
</label>
</p>
</div> </div>
</div> </div>

View File

@ -40,7 +40,8 @@ loginModal, profileModal, profileAvatarImg, profileUserName, openItemsBtn, askTo
autoComplete: true, autoComplete: true,
preserveConsoleLogs: true, preserveConsoleLogs: true,
lightVersion: false, lightVersion: false,
lineWrap: true lineWrap: true,
infiniteLoopTimeout: 1000
}; };
var HtmlModes = { var HtmlModes = {
HTML: 'html', HTML: 'html',
@ -914,7 +915,9 @@ loginModal, profileModal, profileAvatarImg, profileUserName, openItemsBtn, askTo
]); ]);
} finally { } finally {
if (shouldPreventInfiniteLoops !== false) { if (shouldPreventInfiniteLoops !== false) {
code = utils.addInfiniteLoopProtection(code); code = utils.addInfiniteLoopProtection(code, {
timeout: prefs.infiniteLoopTimeout
});
} }
d.resolve(code); d.resolve(code);
} }
@ -931,7 +934,9 @@ loginModal, profileModal, profileAvatarImg, profileUserName, openItemsBtn, askTo
]); ]);
} finally { } finally {
if (shouldPreventInfiniteLoops !== false) { if (shouldPreventInfiniteLoops !== false) {
code = utils.addInfiniteLoopProtection(code); code = utils.addInfiniteLoopProtection(code, {
timeout: prefs.infiniteLoopTimeout
});
} }
d.resolve(code); d.resolve(code);
} }
@ -954,7 +959,9 @@ loginModal, profileModal, profileAvatarImg, profileUserName, openItemsBtn, askTo
presets: ['latest', 'stage-2', 'react'] presets: ['latest', 'stage-2', 'react']
}).code; }).code;
if (shouldPreventInfiniteLoops !== false) { if (shouldPreventInfiniteLoops !== false) {
code = utils.addInfiniteLoopProtection(code); code = utils.addInfiniteLoopProtection(code, {
timeout: prefs.infiniteLoopTimeout
});
} }
d.resolve(code); d.resolve(code);
} }
@ -983,7 +990,9 @@ loginModal, profileModal, profileAvatarImg, profileUserName, openItemsBtn, askTo
}; };
} }
if (shouldPreventInfiniteLoops !== false) { if (shouldPreventInfiniteLoops !== false) {
code = utils.addInfiniteLoopProtection(code.outputText); code = utils.addInfiniteLoopProtection(code.outputText, {
timeout: prefs.infiniteLoopTimeout
});
} }
d.resolve(code); d.resolve(code);
} catch (e) { } catch (e) {
@ -1753,6 +1762,8 @@ loginModal, profileModal, profileAvatarImg, profileUserName, openItemsBtn, askTo
$('[data-setting=preserveConsoleLogs]').checked = prefs.preserveConsoleLogs; $('[data-setting=preserveConsoleLogs]').checked = prefs.preserveConsoleLogs;
$('[data-setting=lightVersion]').checked = prefs.lightVersion; $('[data-setting=lightVersion]').checked = prefs.lightVersion;
$('[data-setting=lineWrap]').checked = prefs.lineWrap; $('[data-setting=lineWrap]').checked = prefs.lineWrap;
$('[data-setting=infiniteLoopTimeout]').value =
prefs.infiniteLoopTimeout;
} }
/** /**

View File

@ -87,13 +87,12 @@
* Contributed by Ariya Hidayat! * Contributed by Ariya Hidayat!
* @param code {string} Code to be protected from infinite loops. * @param code {string} Code to be protected from infinite loops.
*/ */
function addInfiniteLoopProtection(code) { function addInfiniteLoopProtection(code, { timeout }) {
var loopId = 1; var loopId = 1;
var patches = []; var patches = [];
var varPrefix = '_wmloopvar'; var varPrefix = '_wmloopvar';
var varStr = 'var %d = Date.now();\n'; var varStr = 'var %d = Date.now();\n';
var checkStr = var checkStr = `\nif (Date.now() - %d > ${timeout}) { window.top.previewException(new Error("Infinite loop")); break;}\n`;
'\nif (Date.now() - %d > 1000) { window.top.previewException(new Error("Infinite loop")); break;}\n';
esprima.parse(code, { tolerant: true, range: true, jsx: true }, function( esprima.parse(code, { tolerant: true, range: true, jsx: true }, function(
node node