diff --git a/client/constants.js b/client/constants.js index 72f8948c..3c22e79c 100644 --- a/client/constants.js +++ b/client/constants.js @@ -85,5 +85,8 @@ export const SET_THEME = 'SET_THEME'; export const SET_UNSAVED_CHANGES = 'SET_UNSAVED_CHANGES'; +export const DETECT_INFINITE_LOOPS = 'DETECT_INFINITE_LOOPS'; +export const RESET_INFINITE_LOOPS = 'RESET_INFINITE_LOOPS'; + // eventually, handle errors more specifically and better export const ERROR = 'ERROR'; diff --git a/client/modules/IDE/actions/ide.js b/client/modules/IDE/actions/ide.js index 05725fca..a2915232 100644 --- a/client/modules/IDE/actions/ide.js +++ b/client/modules/IDE/actions/ide.js @@ -170,3 +170,14 @@ export function setUnsavedChanges(value) { }; } +export function detectInfiniteLoops() { + return { + type: ActionTypes.DETECT_INFINITE_LOOPS + }; +} + +export function resetInfiniteLoops() { + return { + type: ActionTypes.RESET_INFINITE_LOOPS + }; +} diff --git a/client/modules/IDE/components/Editor.js b/client/modules/IDE/components/Editor.js index 93508ed2..a52c3751 100644 --- a/client/modules/IDE/components/Editor.js +++ b/client/modules/IDE/components/Editor.js @@ -15,6 +15,7 @@ import 'codemirror/addon/lint/html-lint'; import 'codemirror/addon/comment/comment'; import 'codemirror/keymap/sublime'; import 'codemirror/addon/search/jump-to-line'; + import { JSHINT } from 'jshint'; window.JSHINT = JSHINT; import { CSSLint } from 'csslint'; @@ -27,15 +28,16 @@ const downArrowUrl = require('../../../images/down-arrow.svg'); import classNames from 'classnames'; import { debounce } from 'throttle-debounce'; +import loopProtect from 'loop-protect'; class Editor extends React.Component { constructor(props) { super(props); this.tidyCode = this.tidyCode.bind(this); } - componentDidMount() { this.beep = new Audio(beepUrl); + this.widgets = []; this._cm = CodeMirror(this.refs.container, { // eslint-disable-line theme: `p5-${this.props.theme}`, value: this.props.file.content, @@ -47,23 +49,30 @@ class Editor extends React.Component { gutters: ['CodeMirror-lint-markers'], keyMap: 'sublime', lint: { - onUpdateLinting: debounce(2000, (annotations) => { - this.props.clearLintMessage(); - annotations.forEach((x) => { - if (x.from.line > -1) { - this.props.updateLintMessage(x.severity, (x.from.line + 1), x.message); + onUpdateLinting: () => { + debounce(2000, (annotations) => { + this.props.clearLintMessage(); + annotations.forEach((x) => { + if (x.from.line > -1) { + this.props.updateLintMessage(x.severity, (x.from.line + 1), x.message); + } + }); + if (this.props.lintMessages.length > 0 && this.props.lintWarning) { + this.beep.play(); } }); - if (this.props.lintMessages.length > 0 && this.props.lintWarning) { - this.beep.play(); - } - }) + } } }); - this._cm.on('change', debounce(200, () => { + this._cm.on('change', debounce(1000, () => { this.props.setUnsavedChanges(true); this.props.updateFileContent(this.props.file.name, this._cm.getValue()); + this.checkForInfiniteLoop((infiniteLoop, prevs) => { + if (!infiniteLoop && prevs) { + this.props.startSketch(); + } + }); })); this._cm.on('keyup', () => { @@ -132,6 +141,69 @@ class Editor extends React.Component { } } + checkForInfiniteLoop(callback) { + const prevIsplaying = this.props.isPlaying; + let infiniteLoop = false; + let prevLine; + this.props.stopSketch(); + this.props.resetInfiniteLoops(); + let iframe; + + for (let i = 0; i < this.widgets.length; ++i) { + this._cm.removeLineWidget(this.widgets[i]); + } + this.widgets.length = 0; + + loopProtect.alias = 'protect'; + + loopProtect.hit = (line) => { + if (line !== prevLine) { + this.props.detectInfiniteLoops(); + infiniteLoop = true; + callback(infiniteLoop, prevIsplaying); + const msg = document.createElement('div'); + const loopError = `line ${line}: This loop is taking too long to run. This might be an infinite loop.`; + msg.appendChild(document.createTextNode(loopError)); + msg.className = 'lint-error'; + this.widgets.push(this._cm.addLineWidget(line - 1, msg, { coverGutter: false, noHScroll: true })); + prevLine = line; + } + }; + + const processed = loopProtect(this.props.file.content); + + const iframeForLoop = document.getElementById('iframeForLoop'); + if (iframeForLoop === null) { + iframe = document.createElement('iframe'); + iframe.id = 'iframeForLoop'; + iframe.style.display = 'none'; + document.body.appendChild(iframe); + } else { + iframeForLoop.srcdoc = ''; + const win = iframeForLoop.contentWindow; + const doc = win.document; + doc.open(); + + win.protect = loopProtect; + + doc.write(` + + + + + + + + + `); + win.onerror = () => true; + doc.close(); + } + callback(infiniteLoop, prevIsplaying, prevLine); + } + _cm: CodeMirror.Editor render() { @@ -197,7 +269,13 @@ Editor.propTypes = { closeEditorOptions: PropTypes.func.isRequired, showKeyboardShortcutModal: PropTypes.func.isRequired, setUnsavedChanges: PropTypes.func.isRequired, - theme: PropTypes.string.isRequired, + infiniteLoop: PropTypes.bool.isRequired, + detectInfiniteLoops: PropTypes.func.isRequired, + resetInfiniteLoops: PropTypes.func.isRequired, + stopSketch: PropTypes.func.isRequired, + startSketch: PropTypes.func.isRequired, + isPlaying: PropTypes.bool.isRequired, + theme: PropTypes.string.isRequired }; export default Editor; diff --git a/client/modules/IDE/components/PreviewFrame.js b/client/modules/IDE/components/PreviewFrame.js index 16545376..6f858bab 100644 --- a/client/modules/IDE/components/PreviewFrame.js +++ b/client/modules/IDE/components/PreviewFrame.js @@ -205,7 +205,12 @@ class PreviewFrame extends React.Component { renderSketch() { const doc = ReactDOM.findDOMNode(this); - if (this.props.isPlaying) { + if (this.props.infiniteLoop) { + this.props.resetInfiniteLoops(); + doc.srcdoc = ''; + srcDoc.set(doc, ' '); + } + if (this.props.isPlaying && !this.props.infiniteLoop) { srcDoc.set(doc, this.injectLocalFiles()); } else { doc.srcdoc = ''; @@ -250,7 +255,9 @@ PreviewFrame.propTypes = { cssFiles: PropTypes.array.isRequired, files: PropTypes.array.isRequired, dispatchConsoleEvent: PropTypes.func, - children: PropTypes.element + children: PropTypes.element, + infiniteLoop: PropTypes.bool.isRequired, + resetInfiniteLoops: PropTypes.func.isRequired }; export default PreviewFrame; diff --git a/client/modules/IDE/components/Toolbar.js b/client/modules/IDE/components/Toolbar.js index 74e78b6a..0d9dee5b 100644 --- a/client/modules/IDE/components/Toolbar.js +++ b/client/modules/IDE/components/Toolbar.js @@ -55,10 +55,11 @@ class Toolbar extends React.Component { className="toolbar__play-sketch-button" onClick={() => { this.props.startTextOutput(); this.props.startSketch(); }} aria-label="play sketch" + disabled={this.props.infiniteLoop} > -