annotate lib/js/urweb.js @ 810:c1f8963ebb18

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