annotate lib/js/urweb.js @ 797:2c463eee89fa

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