pep8analysis: do work async using workers
[nit.git] / contrib / pep8analysis / www / index.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Pep/8 Analysis</title>
5
6 <meta charset="utf-8">
7 <meta http-equiv="X-UA-Compatible" content="IE=edge">
8 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
9
10 <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
11 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
12 <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
13
14 <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/codemirror/4.2.0/codemirror.css">
15 <script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/4.2.0/codemirror.min.js"></script>
16 <script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/4.2.0/mode/z80/z80.js"></script>
17
18 <script>
19 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
20 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
21 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
22 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
23
24 ga('create', 'UA-574578-5', 'xymus.net');
25 ga('send', 'pageview');
26
27 // Both Viz and Pep8 use emscripten
28 // We put aside the module of each program and bring it back before each use
29 var VizModule = null
30
31 function ready() {
32 $("#loading").hide()
33 $("#analyze").prop('disabled', false)
34 }
35
36 function complete() {
37 // Show and set color of output console
38 $("#consolePanel").removeClass("panel-success panel-warning panel-danger")
39 var analysis = $("#console").text();
40 if (analysis.indexOf("Error") >= 0) {
41 $("#consolePanel").addClass("panel-danger")
42 ga('trackEvent', 'Result', 'Error')
43 }
44 else if (analysis.indexOf("Warning") >= 0) {
45 $("#consolePanel").addClass("panel-warning")
46 ga('trackEvent', 'Result', 'Warning')
47 }
48 else {
49 $("#consolePanel").addClass("panel-success")
50 ga('trackEvent', 'Result', 'Success')
51 }
52 $("#working").hide()
53 $("#consolePanel").show()
54 $("#analyze").prop('disabled', false)
55 }
56
57 function exception(ex) {
58 console.debug("Tool invocation failed, with:\n" + $("#console").text() + "\n" + ex )
59 ga('trackEvent', 'Crash', 'Analysis', $("#console").text())
60 $("#fatal-error").show()
61 $("#analyze").prop('disabled', true)
62 }
63
64 function stdout(text) {
65 $("#console").append(text + "<br>")
66 }
67
68 function exec() {
69 $("#working").show()
70 $("#consolePanel").hide()
71
72 // Clean output console
73 $("#console").text("")
74
75 // Get code
76 var input = editor.getValue()
77
78 // Update GA stats
79 ga('trackEvent', 'Action', 'Analyze')
80
81 // Invoke the full Nit program
82 if(typeof(Worker) !== "undefined") {
83 // Async version
84 $("#analyze").prop('disabled', true)
85 worker.postMessage(input)
86 } else {
87 // Sync version
88 ga('trackEvent', 'Crash', 'No Webworkers', $("#console").text())
89 Module = Pep8Module
90 try {
91 run_analysis = Module.cwrap('pep8analysis_web___NativeString_run_analysis', null, ['string'])
92 run_analysis(input)
93
94 complete()
95 } catch(e) {
96 exception(e)
97 }
98 }
99 }
100
101 function load(file) {
102 // Load a sample program
103 $.get('samples/' + file, function(data){
104 editor.setValue(data)
105 })
106 }
107
108 function show_graph(source) {
109 Module = VizModule
110 var res = Viz(source, "svg", "dot")
111 VizModule = Module
112 $("#graph").html(res)
113 }
114 </script>
115 </head>
116 <body>
117
118 <nav class="navbar navbar-default" role="navigation">
119 <div class="container-fluid">
120 <div class="navbar-header">
121 <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
122 <span class="sr-only">Toggle navigation</span>
123 <span class="icon-bar"></span>
124 <span class="icon-bar"></span>
125 <span class="icon-bar"></span>
126 </button>
127 <a class="navbar-brand">Pep/8 Analysis</a>
128 </div>
129
130 <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
131 <ul class="nav navbar-nav">
132 <li class="active"><a>Web interface</a></li>
133 <li><a href="https://github.com/privat/nit/tree/master/contrib/pep8analysis/">Tool source and manual</a></li>
134 </ul>
135 <ul class="nav navbar-nav navbar-right">
136 <li><a href="http://nitlanguage.org/">nitlanguage.org</a></li>
137 <li><a href="https://code.google.com/p/pep8-1/">Pep/8 project</a></li>
138 </ul>
139 </div>
140 </div>
141 </nav>
142
143 <div class="container-fluid">
144 <div class="alert alert-warning" id="loading">Loading the tool... This may take some time.</div>
145 <div class="alert alert-danger" id="fatal-error" style="display: none;">
146 <h2>The JavaScript tool crashed</h2>
147 <p>Please refresh the page to try again, or use Firefox. An error report has been sent to the developer</p>
148 </div>
149
150 <div class="row">
151 <div class="col-sm-12 col-lg-6">
152 <div class="panel panel-default" id="panelIntro">
153 <div class="panel-heading">Description and Usage
154 <button type="button" class="close" onClick="$('#panelIntro').hide()" aria-hidden="true">&times;</button>
155 </div>
156 <div class="panel-body" id="code-panel">
157 <p>This tools finds strange behavior in Pep/8 programs by exploring all detectable execution paths.</p>
158 <p>Upon analyzing a program, this tool reports dead code blocks and data within the execution path. It also detects wrongful use of data such as accessing uninitialized memory, printing a word and reading code.</p>
159 <p><strong>To use:</strong> copy-paste your code in the source code block and launch the analysis using the <em>Analyze program</em> button.</p>
160 </div>
161 </div>
162
163 <div class="panel panel-default">
164 <div class="panel-heading">Pep/8 Source Code</div>
165 <div class="panel-body" id="code-panel">
166 <textarea id="listing"></textarea>
167 <script>
168 var editor = CodeMirror.fromTextArea(listing, {
169 lineNumbers: true,
170 mode: "z80"
171 });
172 editor.setSize("100%", 500);
173 </script>
174
175 <div class="btn-group">
176 <button type="button" class="btn btn-default" id="analyze" onclick="exec();" disabled>Analyze program</button>
177 <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
178 <span class="glyphicon glyphicon-folder-open"></span>
179 Load sample program
180 <span class="caret"></span>
181 </button>
182 <ul class="dropdown-menu">
183 <li><a onclick="load('02-fibo.pep')">fibo.pep - clean program </a></li>
184 <li><a onclick="load('06-calc-non-pur.pep');">calc-non-pur.pep - self rewriting program </a></li>
185 <li><a onclick="load('types.pep');"/>types.pep - strange data manipulation </a></li>
186 <li><a onclick="load('directive-in-code.pep');">directive-in-code.pep - mixed directives and instructions</a></li>
187 </ul>
188 </div>
189 </div>
190 </div>
191 </div>
192
193 <div class="clearfix visible-xs"></div>
194 <div class="col-sm-12 col-lg-6">
195 <div class="alert alert-warning" id="working" style="display: none;">The tool is evaluating all possible execution paths... This may take some time.</div>
196 <div class="panel" id="consolePanel" style="display: none;">
197 <div class="panel-heading">Analysis Result</div>
198 <div class="panel-body">
199 <samp id="console"></samp>
200 <button type="button" class="btn btn-default" onclick="$('#panelGraph').show();$('#panelGraphExp').show();">
201 Show control flow graph</button>
202 <em class="pull-right" id="panelGraphExp" style="display: none;">The graph is at the bottom of the page...</em>
203 </div>
204 </div>
205 </div>
206
207 <div class="col-sm-12 col-lg-6">
208 <div class="panel panel-default" id="panelRef">
209 <div class="panel-heading">Output Documentation
210 <button type="button" class="close" onClick="$('#panelRef').hide()" aria-hidden="true">&times;</button>
211 </div>
212 <div class="panel-body" id="code-panel">
213 <h4>Line and memory references</h4>
214 <p>Shorthand are used in the error messages, some refer directly to the line numbers others to the address in the memory of the running program.</p>
215 <table class="table table-striped">
216 <tr>
217 <th>Shorthand</th>
218 <th>Description</th>
219 </tr><tr>
220 <td>web:5</td>
221 <td>Source line 5</td>
222 </tr><tr>
223 <td>L8</td>
224 <td>Source line 8</td>
225 </tr><tr>
226 <td>rA, rX</td>
227 <td>Registers A, X</td>
228 </tr><tr>
229 <td>m24</td>
230 <td>Memory at address 24 (bytes)</td>
231 </tr><tr>
232 <td>instr@m24</td>
233 <td>Intruction at address 24 (bytes). Usually, this would be the (24/4+1=) 7th instruction, if there is no data blocks. And the 7th line if there is no data blocks or comments.</td>
234 </tr>
235 </table>
236
237 <h4>Common errors and warnings</h4>
238
239 <p>Note that the tool reports <em>possible</em> errors and strange behaviors. For an advanced or expert user, it may raise errors on correct code.</p>
240
241 <table class="table table-striped">
242 <tr>
243 <th>Error</th>
244 <th>Meaning</th>
245 </tr><tr>
246 <td>... the CFG may be wrong</td>
247 <td>The program structure is too complex to analyze. This happens when jumping to dynamic addresses and when using switch statements.</td>
248 </tr><tr>
249 <td>unreachable instructions</td>
250 <td>There's dead code.</td>
251 </tr><tr>
252 <td>data in program flow</td>
253 <td>The program may execute raw data. There's probably a <code>BR</code> missing.</td>
254 </tr><tr>
255 <td>overwriting code at...</td>
256 <td>An instruction writes over an existing instruction (probably your code).</td>
257 </tr><tr>
258 <td>expected "type" in "address", got "something"</td>
259 <td>An instruction uses data with an unexepected type. It may be a <code>CHARO</code> using something other than a char. Or a copy of uninitialized data over an initialized word.<br>
260 </td>
261 </tr>
262 </table>
263 </div>
264 </div>
265 </div>
266 </div>
267
268 <div class="col-sm-12 col-lg-12">
269 <div class="panel panel-default" id="panelGraph" style="display: none;">
270 <div class="panel-heading">Control Flow Graph (CFG)
271 <button type="button" class="close" onClick="$('#panelGraph').hide()" aria-hidden="true">&times;</button>
272 </div>
273 <div class="panel-body">
274 <div id="graph"></div>
275 </div>
276 </div>
277 </div>
278
279 <script>
280 if(typeof(Worker) !== "undefined") {
281 // Async version
282 var worker = new Worker("worker.js")
283 worker.onmessage = function(event) {
284 if (event.data.type == "ready") {
285 ready()
286 } else if (event.data.type == "complete") {
287 complete()
288 } else if (event.data.type == "exception") {
289 exception(event.data.data)
290 } else if (event.data.type == "stdout") {
291 stdout(event.data.data)
292 } else if (event.data.type == "show_graph") {
293 show_graph(event.data.data)
294 } else {
295 console.out(event.data)
296 }
297 }
298 } else {
299 // Synced version
300 $(document).ready(function() {
301 // Report full loading
302 $("#loading").hide()
303 $("#analyze").prop('disabled', false)
304 });
305 var Module = {
306 'print': stdout,
307 'TOTAL_MEMORY': 512000000
308 }
309 importScripts("pep8analysis.js")
310 var Pep8Module = Module
311 }
312 </script>
313 <script src="viz.js"></script>
314 </div>
315
316 </body>
317 </html>