git @ Cat's Eye Technologies Matchbox / master
Load example programs from JSONP file instead of XHR'ing each one. Chris Pressey a month ago
4 changed file(s) with 35 addition(s) and 37 deletion(s). Raw diff Collapse all Expand all
99 <div id="container"></div>
1010
1111 </body>
12 <script src="../eg/examplePrograms.jsonp.js"></script>
1213 <script src="../src/matchbox-launcher.js"></script>
1314 <script>
1415 launch('../src/', 'container', {});
0 examplePrograms = [
1 {
2 "contents": "DESC\n\n; Example of a pair of programs which trivially do not\n; have a race condition, because they do not share memory.\n;\n;\n\nPROG 0\nMOV M0, R0\nINC R0\nMOV R0, M0\n\nPROG 1\nMOV M1, R0\nINC R0\nMOV R0, M1\n",
3 "filename": "trivial-independent.mbox"
4 },
5 {
6 "contents": "DESC\n\n; Basic example of a pair of programs which have a\n; race condition, because their use of shared memory\n; is not atomic. See 'petersons-no-race' for a cure.\n;\n\nPROG 0\nMOV M0, R0\nINC R0\nMOV R0, M0\n\nPROG 1\nMOV M0, R0\nINC R0\nMOV R0, M0\n",
7 "filename": "basic-race.mbox"
8 },
9 {
10 "contents": "DESC\n\n; Basic example of a pair of programs which do not\n; have a race condition, because they use atomic\n; operations on their shared memory.\n;\n\nPROG 0\nINC M0\n\nPROG 1\nINC M0\n",
11 "filename": "basic-no-race.mbox"
12 },
13 {
14 "contents": "DESC\n\n; This is a demonstration of Peterson's algorithm for mutual exclusion,\n; which can be used to prevent race conditions.\n; See http://en.wikipedia.org/wiki/Peterson%27s_algorithm for details.\n; M0 is \"flag[0]\"; M1 is \"flag[1]\"; M2 is \"turn\"; M3 is the target location\n\nPROG 0\nMOV 1, M0\nMOV 1, M2\nWAIT M1, 0\nWAIT M2, 0\n\n; begin c.s.\nMOV M3, R0\nINC R0\nMOV R0, M3\n; end c.s.\n\nMOV 0, M0\n\nPROG 1\nMOV 1, M1\nMOV 0, M2\nWAIT M0, 0\nWAIT M2, 0\n\n; begin c.s.\nMOV M3, R0\nINC R0\nMOV R0, M3\n; end c.s.\n\nMOV 0, M1\n",
15 "filename": "petersons-no-race.mbox"
16 }
17 ];
103103 status.style.minWidth = '20em';
104104 status.style.overflow = 'auto';
105105
106 var sourceRoot = config.sourceRoot || '../eg/';
106 var getExampleProgram = function(n) {
107 for (var i = 0; i < examplePrograms.length; i++) {
108 if (examplePrograms[i].filename === n) {
109 return examplePrograms[i].contents;
110 }
111 }
112 return "";
113 }
107114 var p = new yoob.PresetManager();
108115 p.init({
109116 'selectElem': presetSelect,
110117 'setPreset': function(n) {
111118 matchbox.reset();
112 matchbox.loadSourceFromURL(sourceRoot + n, function(texts) {
113 description.innerHTML = texts[0];
114 prog1ta.value = texts[1];
115 prog2ta.value = texts[2];
116 });
119 var source = getExampleProgram(n);
120 var texts = matchbox.splitIntoProgramTexts(source);
121 description.innerHTML = texts[0];
122 prog1ta.value = texts[1];
123 prog2ta.value = texts[2];
117124 }
118125 });
119 p.add('trivial-independent.mbox');
120 p.add('basic-race.mbox');
121 p.add('basic-no-race.mbox');
122 p.add('petersons-no-race.mbox');
126 for (var i = 0; i < examplePrograms.length; i++) {
127 p.add(examplePrograms[i].filename);
128 }
123129 p.select('basic-race.mbox');
124130
125131 status.innerHTML = 'Ready.<br/>';
372372 return [texts[0].join("\n"), texts[1].join("\n"), texts[2].join("\n")];
373373 };
374374
375 /*
376 * Cribbed from yoob.SourceManager
377 */
378 this.loadSourceFromURL = function(url, successCallback, errorCallback) {
379 var http = new XMLHttpRequest();
380 var $this = this;
381 if (!errorCallback) {
382 errorCallback = function(http) {
383 alert(
384 "Error: could not load " + url + ": " + http.statusText
385 );
386 }
387 }
388 http.open("get", url, true);
389 http.onload = function(e) {
390 if (http.readyState === 4 && http.responseText) {
391 if (http.status === 200) {
392 var texts = $this.splitIntoProgramTexts(http.responseText);
393 successCallback(texts);
394 } else {
395 errorCallback(http);
396 }
397 }
398 };
399 http.send(null);
400 };
401
402375 /* -------------- recording results -------------- */
403376
404377 this.clearResults = function() {