|
| 1 | +// ECMAScript 6 Backwards compatability |
| 2 | +if (typeof String.prototype.startsWith != 'function') { |
| 3 | + String.prototype.startsWith = function(str) { |
| 4 | + return this.slice(0, str.length) == str; |
| 5 | + }; |
| 6 | +} |
| 7 | + |
| 8 | +// Fetching DOM items |
| 9 | +var activeCode = document.getElementById("active-code"); |
| 10 | +var staticCode = document.getElementById("static-code"); |
| 11 | +var runButton = document.getElementById("run-code"); |
| 12 | +var resultDiv = document.getElementById("result"); |
| 13 | + |
| 14 | +// Background colors for program result on success/error |
| 15 | +var successColor = "#E2EEF6"; |
| 16 | +var errorColor = "#F6E2E2"; |
| 17 | + |
| 18 | +// Error message to return when there's a server failure |
| 19 | +var errMsg = "The server encountered an error while running the program."; |
| 20 | + |
| 21 | +// Stores ACE editor markers (highights) for errors |
| 22 | +var markers = []; |
| 23 | + |
| 24 | +// JS exists, display ACE editor |
| 25 | +staticCode.style.display = "none"; |
| 26 | +activeCode.style.display = "block"; |
| 27 | + |
| 28 | +// Setting up ace editor |
| 29 | +var editor = ace.edit("editor"); |
| 30 | +var Range = ace.require('ace/range').Range; |
| 31 | +editor.setTheme("ace/theme/chrome"); |
| 32 | +editor.getSession().setMode("ace/mode/rust"); |
| 33 | +editor.setShowPrintMargin(false); |
| 34 | +editor.renderer.setShowGutter(false); |
| 35 | + |
| 36 | +// Dispatches a XMLHttpRequest to the Rust playpen, running the program, and |
| 37 | +// issues a callback to `callback` with the result (or null on error) |
| 38 | +function runProgram(program, callback) { |
| 39 | + var req = new XMLHttpRequest(); |
| 40 | + var data = JSON.stringify({ |
| 41 | + version: "master", |
| 42 | + optimize: "2", |
| 43 | + code: program |
| 44 | + }); |
| 45 | + |
| 46 | + <!-- console.log("Sending", data); --> |
| 47 | + req.open('POST', "http://playtest.rust-lang.org/evaluate.json", true); |
| 48 | + req.onload = function(e) { |
| 49 | + if (req.readyState === 4 && req.status === 200) { |
| 50 | + var result = JSON.parse(req.response).result; |
| 51 | + // Need server support to get an accurate version of this. |
| 52 | + var isSuccess = (result.indexOf("error:") === -1); |
| 53 | + callback(isSuccess, result); |
| 54 | + } else { |
| 55 | + callback(false, null); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + req.onerror = function(e) { |
| 60 | + callback(false, null); |
| 61 | + } |
| 62 | + |
| 63 | + req.setRequestHeader("Content-Type", "application/json"); |
| 64 | + req.send(data); |
| 65 | +} |
| 66 | + |
| 67 | +// The callback to runProgram |
| 68 | +function handleResult(success, message) { |
| 69 | + var message = message.replace(/<anon>/g, ''); |
| 70 | + var message = message.replace(/(?:\r\n|\r|\n)/g, '<br />'); |
| 71 | + |
| 72 | + // Dispatch depending on result type |
| 73 | + if (result == null) { |
| 74 | + resultDiv.style.backgroundColor = errorColor; |
| 75 | + resultDiv.innerHTML = errMsg; |
| 76 | + } else if (success) { |
| 77 | + handleSuccess(message); |
| 78 | + } else { |
| 79 | + handleError(message); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// Called on successful program run |
| 84 | +function handleSuccess(message) { |
| 85 | + resultDiv.style.backgroundColor = successColor; |
| 86 | + resultDiv.innerHTML = message; |
| 87 | +} |
| 88 | + |
| 89 | +// Called on unsuccessful program run. Detects and prints errors in program |
| 90 | +// output and highlights relevant lines and text in the code. |
| 91 | +function handleError(message) { |
| 92 | + resultDiv.style.backgroundColor = errorColor; |
| 93 | + |
| 94 | + // Getting list of ranges with errors |
| 95 | + var lines = message.split("<br />"); |
| 96 | + var ranges = parseError(lines); |
| 97 | + |
| 98 | + // Cleaning up the message: keeps only relevant error output |
| 99 | + var cleanMessage = lines.map(function(line) { |
| 100 | + var errIndex = line.indexOf("error: "); |
| 101 | + if (errIndex !== -1) { |
| 102 | + return line.slice(errIndex); |
| 103 | + } |
| 104 | + return ""; |
| 105 | + }).filter(function(line) { |
| 106 | + return line !== ""; |
| 107 | + }).join("<br />"); |
| 108 | + |
| 109 | + // Setting message |
| 110 | + resultDiv.innerHTML = cleanMessage; |
| 111 | + |
| 112 | + // Highlighting the lines |
| 113 | + markers = ranges.map(function(range) { |
| 114 | + return editor.getSession().addMarker(range, "ace-error-line", "fullLine", false); |
| 115 | + }); |
| 116 | + |
| 117 | + // Highlighting the specific text |
| 118 | + markers = markers.concat(ranges.map(function(range) { |
| 119 | + return editor.getSession().addMarker(range, "ace-error-text", "text", false); |
| 120 | + })); |
| 121 | +} |
| 122 | + |
| 123 | +// Parses an error message returning a list of ranges (row:col, row:col) where |
| 124 | +// erors in the code have occured. |
| 125 | +function parseError(lines) { |
| 126 | + var ranges = []; |
| 127 | + for (var i in lines) { |
| 128 | + var line = lines[i]; |
| 129 | + if (line.startsWith(":") && line.indexOf(": ") !== -1) { |
| 130 | + var parts = line.split(/:\s?|\s+/, 5).slice(1, 5); |
| 131 | + var ip = parts.map(function(p) { return parseInt(p, 10) - 1; }); |
| 132 | + <!-- console.log("line:", line, parts, ip); --> |
| 133 | + ranges.push(new Range(ip[0], ip[1], ip[2], ip[3])); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return ranges; |
| 138 | +} |
| 139 | + |
| 140 | +// Registering handler for run button click |
| 141 | +runButton.addEventListener("click", function(ev) { |
| 142 | + resultDiv.style.display = "block"; |
| 143 | + resultDiv.innerHTML = "Running..."; |
| 144 | + |
| 145 | + // clear previous markers, if any |
| 146 | + markers.map(function(id) { editor.getSession().removeMarker(id); }); |
| 147 | + |
| 148 | + // Get the code, run the program |
| 149 | + var program = editor.getValue(); |
| 150 | + runProgram(program, handleResult); |
| 151 | +}); |
0 commit comments