annotate lib/js/urweb.js @ 728:2197f0e24a9f

Avoid thread death via message receive
author Adam Chlipala <adamc@hcoop.net>
date Thu, 16 Apr 2009 13:00:40 -0400
parents 6fc633d990e7
children 7c6b6c3c7b79
rev   line source
adamc@692 1 // Lists
adamc@692 2
adamc@580 3 function cons(v, ls) {
adamc@690 4 return { next : ls, data : v };
adamc@690 5 }
adamc@690 6 function concat(ls1, ls2) {
adamc@690 7 return (ls1 ? cons(ls1.data, concat(ls1.next, ls2)) : ls2);
adamc@690 8 }
adamc@690 9 function member(x, ls) {
adamc@690 10 for (; ls; ls = ls.next)
adamc@690 11 if (ls.data == x)
adamc@690 12 return true;
adamc@690 13 return false;
adamc@690 14 }
adamc@690 15 function remove(x, ls) {
adamc@690 16 return (ls ? (ls.data == x ? ls.next : cons(ls.data, remove(x, ls.next))) : null);
adamc@690 17 }
adamc@690 18 function union(ls1, ls2) {
adamc@690 19 return (ls1 ? (member(ls1.data, ls2) ? union(ls1.next, ls2) : cons(ls1.data, union(ls1.next, ls2))) : ls2);
adamc@580 20 }
adamc@703 21 function length(ls) {
adamc@703 22 return (ls ? 1 + length(ls.next) : 0);
adamc@703 23 }
adamc@670 24
adamc@690 25
adamc@692 26 // Embedding closures in XML strings
adamc@692 27
adamc@693 28 function cs(f) {
adamc@693 29 return {closure: f};
adamc@693 30 }
adamc@693 31
adamc@693 32 function isWeird(v) {
adamc@693 33 return v.closure != null || v.cat1 != null;
adamc@693 34 }
adamc@693 35
adamc@692 36 function cat(s1, s2) {
adamc@693 37 if (isWeird(s1) || isWeird(s2))
adamc@693 38 return {cat1: s1, cat2: s2};
adamc@693 39 else
adamc@692 40 return s1 + s2;
adamc@692 41 }
adamc@692 42
adamc@692 43 var closures = [];
adamc@703 44 var freeClosures = null;
adamc@692 45
adamc@692 46 function newClosure(f) {
adamc@703 47 var n;
adamc@703 48 if (freeClosures == null) {
adamc@703 49 n = closures.length;
adamc@703 50 } else {
adamc@703 51 n = freeClosures.data;
adamc@703 52 freeClosures = freeClosures.next;
adamc@703 53 }
adamc@692 54 closures[n] = f;
adamc@692 55 return n;
adamc@692 56 }
adamc@692 57
adamc@703 58 function freeClosure(n) {
adamc@703 59 closures[n] = null;
adamc@703 60 freeClosures = cons(n, freeClosures);
adamc@703 61 }
adamc@703 62
adamc@692 63 function cr(n) {
adamc@692 64 return closures[n]();
adamc@692 65 }
adamc@692 66
adamc@703 67 function flatten(cls, tr) {
adamc@693 68 if (tr.cat1 != null)
adamc@703 69 return flatten(cls, tr.cat1) + flatten(cls, tr.cat2);
adamc@703 70 else if (tr.closure != null) {
adamc@703 71 var cl = newClosure(tr.closure);
adamc@703 72 cls.v = cons(cl, cls.v);
adamc@703 73 return "cr(" + cl + ")";
adamc@703 74 } else
adamc@692 75 return tr;
adamc@692 76 }
adamc@692 77
adamc@728 78 function flattenLocal(s) {
adamc@728 79 var cls = {v : null};
adamc@728 80 var r = flatten(cls, s);
adamc@728 81 for (cl = cls.v; cl != null; cl = cl.next)
adamc@728 82 freeClosure(cl.data);
adamc@728 83 return r;
adamc@728 84 }
adamc@728 85
adamc@692 86
adamc@692 87
adamc@692 88 // Dynamic tree management
adamc@692 89
adamc@690 90 function populate(node) {
adamc@690 91 var s = node.signal;
adamc@690 92 var oldSources = node.sources;
adamc@690 93 var sr = s();
adamc@690 94 var newSources = sr.sources;
adamc@690 95
adamc@690 96 for (var sp = oldSources; sp; sp = sp.next)
adamc@690 97 if (!member(sp.data, newSources))
adamc@690 98 sp.data.dyns = remove(node, sp.data.dyns);
adamc@690 99
adamc@690 100 for (var sp = newSources; sp; sp = sp.next)
adamc@690 101 if (!member(sp.data, oldSources))
adamc@690 102 sp.data.dyns = cons(node, sp.data.dyns);
adamc@690 103
adamc@690 104 node.sources = newSources;
adamc@690 105 node.recreate(sr.data);
adamc@579 106 }
adamc@574 107
adamc@580 108 function sc(v) {
adamc@690 109 return {data : v, dyns : null};
adamc@580 110 }
adamc@580 111 function sv(s, v) {
adamc@690 112 s.data = v;
adamc@690 113 for (var ls = s.dyns; ls; ls = ls.next)
adamc@690 114 if (!ls.dead)
adamc@690 115 populate(ls.data);
adamc@580 116 }
adamc@601 117 function sg(s) {
adamc@690 118 return s.data;
adamc@601 119 }
adamc@579 120
adamc@580 121 function ss(s) {
adamc@690 122 return function() { return {sources : cons(s, null), data : s.data } };
adamc@580 123 }
adamc@580 124 function sr(v) {
adamc@690 125 return function() { return {sources : null, data : v } };
adamc@580 126 }
adamc@580 127 function sb(x,y) {
adamc@690 128 return function() {
adamc@690 129 var xr = x();
adamc@690 130 var yr = y(xr.data)();
adamc@690 131 return {sources : union(xr.sources, yr.sources), data : yr.data};
adamc@690 132 };
adamc@580 133 }
adamc@571 134
adamc@604 135 function lastParent() {
adamc@604 136 var pos = document;
adamc@604 137
adamc@600 138 while (pos.lastChild && pos.lastChild.nodeType == 1)
adamc@600 139 pos = pos.lastChild;
adamc@600 140
adamc@600 141 return pos.parentNode;
adamc@600 142 }
adamc@600 143
adamc@604 144 function addNode(node) {
adamc@604 145 if (thisScript) {
adamc@604 146 thisScript.parentNode.appendChild(node);
adamc@604 147 thisScript.parentNode.removeChild(thisScript);
adamc@604 148 } else
adamc@604 149 lastParent().appendChild(node);
adamc@603 150 }
adamc@603 151
adamc@690 152 var thisScript = null;
adamc@690 153
adamc@604 154 function runScripts(node) {
adamc@604 155 var savedScript = thisScript;
adamc@603 156
adamc@692 157 var scripts = node.getElementsByTagName("script"), scriptsCopy = [];
adamc@604 158 var len = scripts.length;
adamc@646 159 for (var i = 0; i < len; ++i)
adamc@646 160 scriptsCopy[i] = scripts[i];
adamc@604 161 for (var i = 0; i < len; ++i) {
adamc@646 162 thisScript = scriptsCopy[i];
adamc@604 163 eval(thisScript.textContent);
adamc@604 164 }
adamc@604 165
adamc@604 166 thisScript = savedScript;
adamc@603 167 }
adamc@603 168
adamc@603 169
adamc@692 170 // Dynamic tree entry points
adamc@692 171
adamc@692 172 var dynDepth = 0;
adamc@692 173
adamc@571 174 function dyn(s) {
adamc@571 175 var x = document.createElement("span");
adamc@690 176 x.dead = false;
adamc@690 177 x.signal = s;
adamc@690 178 x.sources = null;
adamc@703 179 x.closures = null;
adamc@690 180 x.recreate = function(v) {
adamc@703 181 for (var ls = x.closures; ls; ls = ls.next)
adamc@703 182 freeClosure(ls.data);
adamc@692 183
adamc@690 184 var spans = x.getElementsByTagName("span");
adamc@690 185 for (var i = 0; i < spans.length; ++i) {
adamc@690 186 var span = spans[i];
adamc@690 187 span.dead = true;
adamc@690 188 for (var ls = span.sources; ls; ls = ls.next)
adamc@690 189 ls.data.dyns = remove(span, ls.data.dyns);
adamc@703 190 for (var ls = span.closures; ls; ls = ls.next)
adamc@703 191 freeClosure(ls.data);
adamc@690 192 }
adamc@690 193
adamc@703 194 var cls = {v : null};
adamc@703 195 x.innerHTML = flatten(cls, v);
adamc@703 196 x.closures = cls.v;
adamc@690 197 runScripts(x);
adamc@690 198 };
adamc@703 199 addNode(x);
adamc@690 200 populate(x);
adamc@571 201 }
adamc@582 202
adamc@598 203 function inp(t, s) {
adamc@598 204 var x = document.createElement(t);
adamc@690 205 x.dead = false;
adamc@690 206 x.signal = ss(s);
adamc@690 207 x.sources = null;
adamc@690 208 x.recreate = function(v) { if (x.value != v) x.value = v; };
adamc@690 209 populate(x);
adamc@604 210 addNode(x);
adamc@598 211 x.onkeyup = function() { sv(s, x.value) };
adamc@606 212 return x;
adamc@598 213 }
adamc@598 214
adamc@692 215
adamc@692 216 // Basic string operations
adamc@692 217
adamc@597 218 function eh(x) {
adamc@597 219 return x.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
adamc@597 220 }
adamc@597 221
adamc@582 222 function ts(x) { return x.toString() }
adamc@586 223 function bs(b) { return (b ? "True" : "False") }
adamc@586 224
adamc@649 225 function pi(s) {
adamc@649 226 var r = parseInt(s);
adamc@649 227 if (r.toString() == s)
adamc@649 228 return r;
adamc@649 229 else
adamc@649 230 throw "Can't parse int: " + s;
adamc@649 231 }
adamc@649 232
adamc@649 233 function pfl(s) {
adamc@649 234 var r = parseFloat(s);
adamc@649 235 if (r.toString() == s)
adamc@649 236 return r;
adamc@649 237 else
adamc@649 238 throw "Can't parse float: " + s;
adamc@649 239 }
adamc@649 240
adamc@692 241 function uf(s) {
adamc@692 242 return escape(s).replace(new RegExp ("/", "g"), "%2F");
adamc@691 243 }
adamc@691 244
adamc@692 245 function uu(s) {
adamc@692 246 return unescape(s).replace(new RegExp ("\\+", "g"), " ");
adamc@692 247 }
adamc@692 248
adamc@692 249
adamc@692 250 // Error handling
adamc@692 251
adamc@669 252 function whine(msg) {
adamc@669 253 alert(msg);
adamc@669 254 throw msg;
adamc@669 255 }
adamc@669 256
adamc@649 257 function pf() {
adamc@669 258 whine("Pattern match failure");
adamc@649 259 }
adamc@589 260
adamc@726 261 var errorHandlers = null;
adamc@726 262
adamc@726 263 function onError(f) {
adamc@726 264 errorHandlers = cons(f, errorHandlers);
adamc@726 265 }
adamc@726 266
adamc@726 267 function er(s) {
adamc@726 268 for (var ls = errorHandlers; ls; ls = ls.next)
adamc@726 269 ls.data(s)(null);
adamc@728 270 throw {uw_error: s};
adamc@728 271 }
adamc@728 272
adamc@728 273 var failHandlers = null;
adamc@728 274
adamc@728 275 function onFail(f) {
adamc@728 276 failHandlers = cons(f, failHandlers);
adamc@728 277 }
adamc@728 278
adamc@728 279 function doExn(v) {
adamc@728 280 if (v == null || v.uw_error == null) {
adamc@728 281 var s = (v == null ? "null" : v.toString());
adamc@728 282 for (var ls = failHandlers; ls; ls = ls.next)
adamc@728 283 ls.data(s)(null);
adamc@728 284 }
adamc@726 285 }
adamc@726 286
adamc@603 287
adamc@692 288 // Remote calls
adamc@609 289
adamc@703 290 var client_id = null;
adamc@668 291 var client_pass = 0;
adamc@668 292 var url_prefix = "/";
adamc@673 293 var timeout = 60;
adamc@668 294
adamc@668 295 function getXHR(uri)
adamc@609 296 {
adamc@609 297 try {
adamc@609 298 return new XMLHttpRequest();
adamc@609 299 } catch (e) {
adamc@609 300 try {
adamc@609 301 return new ActiveXObject("Msxml2.XMLHTTP");
adamc@609 302 } catch (e) {
adamc@609 303 try {
adamc@609 304 return new ActiveXObject("Microsoft.XMLHTTP");
adamc@609 305 } catch (e) {
adamc@609 306 throw "Your browser doesn't seem to support AJAX.";
adamc@609 307 }
adamc@609 308 }
adamc@609 309 }
adamc@609 310 }
adamc@609 311
adamc@668 312 function requestUri(xhr, uri) {
adamc@668 313 xhr.open("GET", uri, true);
adamc@668 314
adamc@703 315 if (client_id != null) {
adamc@668 316 xhr.setRequestHeader("UrWeb-Client", client_id.toString());
adamc@668 317 xhr.setRequestHeader("UrWeb-Pass", client_pass.toString());
adamc@668 318 }
adamc@668 319
adamc@668 320 xhr.send(null);
adamc@668 321 }
adamc@668 322
adamc@613 323 function rc(uri, parse, k) {
adamc@728 324 uri = flattenLocal(uri);
adamc@609 325 var xhr = getXHR();
adamc@609 326
adamc@609 327 xhr.onreadystatechange = function() {
adamc@612 328 if (xhr.readyState == 4) {
adamc@612 329 var isok = false;
adamc@612 330
adamc@612 331 try {
adamc@612 332 if (xhr.status == 200)
adamc@612 333 isok = true;
adamc@612 334 } catch (e) { }
adamc@612 335
adamc@612 336 if (isok)
adamc@613 337 k(parse(xhr.responseText));
adamc@649 338 else {
adamc@669 339 whine("Error querying remote server!");
adamc@649 340 }
adamc@612 341 }
adamc@609 342 };
adamc@609 343
adamc@668 344 requestUri(xhr, uri);
adamc@609 345 }
adamc@667 346
adamc@667 347 function path_join(s1, s2) {
adamc@667 348 if (s1.length > 0 && s1[s1.length-1] == '/')
adamc@667 349 return s1 + s2;
adamc@667 350 else
adamc@667 351 return s1 + "/" + s2;
adamc@667 352 }
adamc@667 353
adamc@670 354 var channels = [];
adamc@670 355
adamc@670 356 function newQueue() {
adamc@670 357 return { front : null, back : null };
adamc@670 358 }
adamc@670 359 function enqueue(q, v) {
adamc@670 360 if (q.front == null) {
adamc@670 361 q.front = cons(v, null);
adamc@670 362 q.back = q.front;
adamc@670 363 } else {
adamc@670 364 var node = cons(v, null);
adamc@690 365 q.back.next = node;
adamc@670 366 q.back = node;
adamc@670 367 }
adamc@670 368 }
adamc@670 369 function dequeue(q) {
adamc@670 370 if (q.front == null)
adamc@670 371 return null;
adamc@670 372 else {
adamc@690 373 var r = q.front.data;
adamc@690 374 q.front = q.front.next;
adamc@670 375 if (q.front == null)
adamc@670 376 q.back = null;
adamc@670 377 return r;
adamc@670 378 }
adamc@670 379 }
adamc@670 380
adamc@670 381 function newChannel() {
adamc@670 382 return { msgs : newQueue(), listeners : newQueue() };
adamc@670 383 }
adamc@670 384
adamc@667 385 function listener() {
adamc@668 386 var uri = path_join(url_prefix, ".msgs");
adamc@667 387 var xhr = getXHR();
adamc@673 388 var tid, orsc, onTimeout;
adamc@673 389
adamc@673 390 var connect = function () {
adamc@673 391 xhr.onreadystatechange = orsc;
adamc@673 392 tid = window.setTimeout(onTimeout, timeout * 500);
adamc@673 393 requestUri(xhr, uri);
adamc@673 394 }
adamc@673 395
adamc@673 396 orsc = function() {
adamc@667 397 if (xhr.readyState == 4) {
adamc@673 398 window.clearTimeout(tid);
adamc@673 399
adamc@667 400 var isok = false;
adamc@667 401
adamc@667 402 try {
adamc@667 403 if (xhr.status == 200)
adamc@667 404 isok = true;
adamc@667 405 } catch (e) { }
adamc@667 406
adamc@668 407 if (isok) {
adamc@669 408 var lines = xhr.responseText.split("\n");
adamc@669 409 if (lines.length < 2)
adamc@690 410 return; // throw "Empty message from remote server";
adamc@669 411
adamc@669 412 for (var i = 0; i+1 < lines.length; i += 2) {
adamc@670 413 var chn = lines[i];
adamc@670 414 var msg = lines[i+1];
adamc@670 415
adamc@670 416 if (chn < 0)
adamc@670 417 whine("Out-of-bounds channel in message from remote server");
adamc@670 418
adamc@670 419 var ch;
adamc@670 420
adamc@670 421 if (chn >= channels.length || channels[chn] == null) {
adamc@670 422 ch = newChannel();
adamc@670 423 channels[chn] = ch;
adamc@670 424 } else
adamc@670 425 ch = channels[chn];
adamc@670 426
adamc@670 427 var listener = dequeue(ch.listeners);
adamc@670 428 if (listener == null) {
adamc@670 429 enqueue(ch.msgs, msg);
adamc@670 430 } else {
adamc@728 431 try {
adamc@728 432 listener(msg);
adamc@728 433 } catch (v) {
adamc@728 434 doExn(v);
adamc@728 435 }
adamc@670 436 }
adamc@669 437 }
adamc@669 438
adamc@673 439 connect();
adamc@668 440 }
adamc@667 441 else {
adamc@679 442 /*try {
adamc@672 443 whine("Error querying remote server for messages! " + xhr.status);
adamc@679 444 } catch (e) { }*/
adamc@667 445 }
adamc@667 446 }
adamc@667 447 };
adamc@667 448
adamc@673 449 onTimeout = function() {
adamc@673 450 xhr.abort();
adamc@673 451 connect();
adamc@673 452 };
adamc@673 453
adamc@673 454 connect();
adamc@667 455 }
adamc@670 456
adamc@670 457 function rv(chn, parse, k) {
adamc@682 458 if (chn == null)
adamc@682 459 return;
adamc@682 460
adamc@670 461 if (chn < 0)
adamc@670 462 whine("Out-of-bounds channel receive");
adamc@670 463
adamc@670 464 var ch;
adamc@670 465
adamc@670 466 if (chn >= channels.length || channels[chn] == null) {
adamc@670 467 ch = newChannel();
adamc@670 468 channels[chn] = ch;
adamc@670 469 } else
adamc@670 470 ch = channels[chn];
adamc@670 471
adamc@670 472 var msg = dequeue(ch.msgs);
adamc@670 473 if (msg == null) {
adamc@670 474 enqueue(ch.listeners, function(msg) { k(parse(msg))(null); });
adamc@670 475 } else {
adamc@728 476 try {
adamc@728 477 k(parse(msg))(null);
adamc@728 478 } catch (v) {
adamc@728 479 doExn(v);
adamc@728 480 }
adamc@670 481 }
adamc@670 482 }
adamc@693 483
adamc@693 484
adamc@693 485 // App-specific code
adamc@693 486