annotate lib/js/urweb.js @ 1037:83d8ddd09d95

Send RPCs with POST, to avoid caching
author Adam Chlipala <adamc@hcoop.net>
date Sat, 21 Nov 2009 14:29:35 -0500
parents e214ac93d73e
children dcc6093e9575
rev   line source
adamc@1036 1 // Detect browser quirks that we should be aware of.
adamc@1036 2
adamc@1036 3 function needsDynPrefix() {
adamc@1036 4 var span = document.createElement("span");
adamc@1036 5 span.innerHTML = "<script>alert('test');</script>";
adamc@1036 6 var scripts = span.getElementsByTagName("script");
adamc@1036 7 return scripts.length == 0;
adamc@1036 8 }
adamc@1036 9
adamc@1036 10 var dynPrefix = needsDynPrefix() ? "<span style=\"display:none\">A</span>" : "";
adamc@1036 11
adamc@970 12 // Function versions of operators
adamc@970 13
adamc@970 14 function not(x) { return !x; }
adamc@970 15 function neg(x) { return -x; }
adamc@970 16
adamc@970 17 function eq(x, y) { return x == y; }
adamc@970 18 function plus(x, y) { return x + y; }
adamc@970 19 function minus(x, y) { return x - y; }
adamc@970 20 function times(x, y) { return x * y; }
adamc@970 21 function div(x, y) { return x / y; }
adamc@970 22 function mod(x, y) { return x % y; }
adamc@970 23 function lt(x, y) { return x < y; }
adamc@970 24 function le(x, y) { return x <= y; }
adamc@970 25
adamc@692 26 // Lists
adamc@692 27
adamc@580 28 function cons(v, ls) {
adamc@690 29 return { next : ls, data : v };
adamc@690 30 }
adamc@969 31 function rev(ls) {
adamc@969 32 var acc = null;
adamc@969 33 for (; ls; ls = ls.next)
adamc@969 34 acc = cons(ls.data, acc);
adamc@969 35 return acc;
adamc@969 36 }
adamc@690 37 function concat(ls1, ls2) {
adamc@969 38 var acc = ls2;
adamc@969 39 ls1 = rev(ls1);
adamc@969 40 for (; ls1; ls1 = ls1.next)
adamc@969 41 acc = cons(ls1.data, acc);
adamc@969 42 return acc;
adamc@690 43 }
adamc@690 44 function member(x, ls) {
adamc@690 45 for (; ls; ls = ls.next)
adamc@690 46 if (ls.data == x)
adamc@690 47 return true;
adamc@690 48 return false;
adamc@690 49 }
adamc@690 50 function remove(x, ls) {
adamc@969 51 var acc = null;
adamc@969 52
adamc@969 53 for (; ls; ls = ls.next)
adamc@969 54 if (ls.data == x)
adamc@969 55 return concat(acc, ls.next);
adamc@969 56 else
adamc@969 57 acc = cons(ls.data, acc);
adamc@969 58
adamc@969 59 return ls;
adamc@690 60 }
adamc@690 61 function union(ls1, ls2) {
adamc@969 62 var acc = ls2;
adamc@969 63
adamc@969 64 for (; ls1; ls1 = ls1.next)
adamc@969 65 if (!member(ls1.data, ls2))
adamc@969 66 acc = cons(ls1.data, acc);
adamc@969 67
adamc@969 68 return acc;
adamc@580 69 }
adamc@703 70 function length(ls) {
adamc@969 71 var acc = 0;
adamc@969 72
adamc@969 73 for (; ls; ls = ls.next)
adamc@969 74 ++acc;
adamc@969 75
adamc@969 76 return acc;
adamc@703 77 }
adamc@670 78
adamc@690 79
adamc@729 80 // Error handling
adamc@729 81
adamc@729 82 function whine(msg) {
adamc@729 83 alert(msg);
adamc@729 84 throw msg;
adamc@729 85 }
adamc@729 86
adamc@810 87 function pf(loc) {
adamc@835 88 throw ("Pattern match failure (" + loc + ")");
adamc@729 89 }
adamc@729 90
adamc@798 91 function runHandlers(kind, ls, arg) {
adamc@798 92 if (ls == null)
adamc@798 93 alert(kind + ": " + arg);
adamc@729 94 for (; ls; ls = ls.next)
adamc@729 95 try {
adamc@976 96 exec({c:"a", f:{c:"a", f:ls.data, x:{c:"c", v:arg}}, x:{c:"c", v:null}});
adamc@729 97 } catch (v) { }
adamc@729 98 }
adamc@729 99
adamc@729 100 var errorHandlers = null;
adamc@729 101
adamc@729 102 function onError(f) {
adamc@729 103 errorHandlers = cons(f, errorHandlers);
adamc@729 104 }
adamc@729 105
adamc@729 106 function er(s) {
adamc@798 107 runHandlers("Error", errorHandlers, s);
adamc@729 108 throw {uw_error: s};
adamc@729 109 }
adamc@729 110
adamc@729 111 var failHandlers = null;
adamc@729 112
adamc@729 113 function onFail(f) {
adamc@729 114 failHandlers = cons(f, failHandlers);
adamc@729 115 }
adamc@729 116
adamc@729 117 function doExn(v) {
adamc@729 118 if (v == null || v.uw_error == null) {
adamc@729 119 var s = (v == null ? "null" : v.toString());
adamc@969 120 if (v != null && v.fileName && v.lineNumber)
adamc@969 121 s += " (" + v.fileName + ":" + v.lineNumber + ")";
adamc@798 122 runHandlers("Fail", failHandlers, s);
adamc@729 123 }
adamc@729 124 }
adamc@729 125
adamc@729 126 var disconnectHandlers = null;
adamc@729 127
adamc@976 128 function flift(f) {
adamc@977 129 return {env:cons(f,null), body:{c:"v", n:1}};
adamc@976 130 }
adamc@976 131
adamc@729 132 function onDisconnect(f) {
adamc@976 133 disconnectHandlers = cons(flift(f), disconnectHandlers);
adamc@729 134 }
adamc@729 135
adamc@729 136 function discon() {
adamc@798 137 runHandlers("Disconnect", disconnectHandlers, null);
adamc@729 138 }
adamc@729 139
adamc@729 140 var connectHandlers = null;
adamc@729 141
adamc@729 142 function onConnectFail(f) {
adamc@976 143 connectHandlers = cons(flift(f), connectHandlers);
adamc@729 144 }
adamc@729 145
adamc@729 146 function conn() {
adamc@798 147 runHandlers("Connect", connectHandlers, null);
adamc@729 148 }
adamc@729 149
adamc@729 150 var serverHandlers = null;
adamc@729 151
adamc@729 152 function onServerError(f) {
adamc@729 153 serverHandlers = cons(f, serverHandlers);
adamc@729 154 }
adamc@729 155
adamc@729 156 function servErr(s) {
adamc@824 157 window.setTimeout(function () { runHandlers("Server", serverHandlers, s); }, 0);
adamc@729 158 }
adamc@729 159
adamc@729 160
adamc@692 161 // Embedding closures in XML strings
adamc@692 162
adamc@693 163 function cs(f) {
adamc@693 164 return {closure: f};
adamc@693 165 }
adamc@693 166
adamc@693 167 function isWeird(v) {
adamc@693 168 return v.closure != null || v.cat1 != null;
adamc@693 169 }
adamc@693 170
adamc@692 171 function cat(s1, s2) {
adamc@693 172 if (isWeird(s1) || isWeird(s2))
adamc@693 173 return {cat1: s1, cat2: s2};
adamc@693 174 else
adamc@692 175 return s1 + s2;
adamc@692 176 }
adamc@692 177
adamc@692 178 var closures = [];
adamc@703 179 var freeClosures = null;
adamc@692 180
adamc@692 181 function newClosure(f) {
adamc@703 182 var n;
adamc@703 183 if (freeClosures == null) {
adamc@703 184 n = closures.length;
adamc@703 185 } else {
adamc@703 186 n = freeClosures.data;
adamc@703 187 freeClosures = freeClosures.next;
adamc@703 188 }
adamc@692 189 closures[n] = f;
adamc@692 190 return n;
adamc@692 191 }
adamc@692 192
adamc@703 193 function freeClosure(n) {
adamc@703 194 closures[n] = null;
adamc@703 195 freeClosures = cons(n, freeClosures);
adamc@703 196 }
adamc@703 197
adamc@692 198 function cr(n) {
adamc@975 199 return closures[n];
adamc@692 200 }
adamc@692 201
adamc@703 202 function flatten(cls, tr) {
adamc@693 203 if (tr.cat1 != null)
adamc@703 204 return flatten(cls, tr.cat1) + flatten(cls, tr.cat2);
adamc@703 205 else if (tr.closure != null) {
adamc@703 206 var cl = newClosure(tr.closure);
adamc@703 207 cls.v = cons(cl, cls.v);
adamc@703 208 return "cr(" + cl + ")";
adamc@703 209 } else
adamc@692 210 return tr;
adamc@692 211 }
adamc@692 212
adamc@728 213 function flattenLocal(s) {
adamc@728 214 var cls = {v : null};
adamc@728 215 var r = flatten(cls, s);
adamc@728 216 for (cl = cls.v; cl != null; cl = cl.next)
adamc@728 217 freeClosure(cl.data);
adamc@728 218 return r;
adamc@728 219 }
adamc@728 220
adamc@692 221
adamc@692 222
adamc@692 223 // Dynamic tree management
adamc@692 224
adamc@690 225 function populate(node) {
adamc@690 226 var s = node.signal;
adamc@690 227 var oldSources = node.sources;
adamc@729 228 try {
adamc@977 229 var sr = execF(s, null);
adamc@976 230 var newSources = sr._sources;
adamc@690 231
adamc@729 232 for (var sp = oldSources; sp; sp = sp.next)
adamc@729 233 if (!member(sp.data, newSources))
adamc@729 234 sp.data.dyns = remove(node, sp.data.dyns);
adamc@690 235
adamc@729 236 for (var sp = newSources; sp; sp = sp.next)
adamc@729 237 if (!member(sp.data, oldSources))
adamc@729 238 sp.data.dyns = cons(node, sp.data.dyns);
adamc@690 239
adamc@729 240 node.sources = newSources;
adamc@976 241 node.recreate(sr._data);
adamc@729 242 } catch (v) {
adamc@729 243 doExn(v);
adamc@729 244 }
adamc@579 245 }
adamc@574 246
adamc@580 247 function sc(v) {
adamc@690 248 return {data : v, dyns : null};
adamc@580 249 }
adamc@580 250 function sv(s, v) {
adamc@690 251 s.data = v;
adamc@967 252
adamc@690 253 for (var ls = s.dyns; ls; ls = ls.next)
adamc@690 254 if (!ls.dead)
adamc@690 255 populate(ls.data);
adamc@580 256 }
adamc@601 257 function sg(s) {
adamc@690 258 return s.data;
adamc@601 259 }
adamc@579 260
adamc@580 261 function ss(s) {
adamc@976 262 return {env:cons(s, null), body:{c:"r", l:
adamc@976 263 cons({n:"sources", v:{c:"c", v:cons(s, null)}},
adamc@976 264 cons({n:"data", v:{c:"f", f:sg, a:cons({c:"v", n:1}, null)}}, null))}};
adamc@580 265 }
adamc@580 266 function sr(v) {
adamc@976 267 return {env:null, body:{c:"c", v:{_sources : null, _data : v}}};
adamc@580 268 }
adamc@580 269 function sb(x,y) {
adamc@976 270 return {env:cons(y,cons(x,null)),
adamc@976 271 body:{c:"=",
adamc@977 272 e1:{c:"a", f:{c:"v", n:2}, x:{c:"c", v:null}},
adamc@976 273 e2:{c:"=",
adamc@976 274 e1:{c:"a",
adamc@977 275 f:{c:"a", f:{c:"v", n:2}, x:{c:".", r:{c:"v", n:0}, f:"data"}},
adamc@976 276 x:{c:"c", v:null}},
adamc@976 277 e2:{c:"r", l:cons(
adamc@976 278 {n:"sources", v:{c:"f", f:union, a:cons({c:".", r:{c:"v", n:1}, f:"sources"},
adamc@976 279 cons({c:".", r:{c:"v", n:0}, f:"sources"}, null))}},
adamc@976 280 cons({n:"data", v:{c:".", r:{c:"v", n:0}, f:"data"}}, null))}}}};
adamc@580 281 }
adamc@841 282 function scur(s) {
adamc@982 283 return execF(s, null)._data;
adamc@841 284 }
adamc@571 285
adamc@604 286 function lastParent() {
adamc@604 287 var pos = document;
adamc@604 288
adamc@600 289 while (pos.lastChild && pos.lastChild.nodeType == 1)
adamc@600 290 pos = pos.lastChild;
adamc@600 291
adamc@600 292 return pos.parentNode;
adamc@600 293 }
adamc@600 294
adamc@898 295 function parent() {
adamc@898 296 return thisScript ? thisScript.parentNode : lastParent();
adamc@898 297 }
adamc@898 298
adamc@604 299 function addNode(node) {
adamc@899 300 if (thisScript)
adamc@899 301 thisScript.parentNode.replaceChild(node, thisScript);
adamc@899 302 else
adamc@604 303 lastParent().appendChild(node);
adamc@603 304 }
adamc@603 305
adamc@690 306 var thisScript = null;
adamc@690 307
adamc@604 308 function runScripts(node) {
adamc@899 309 if (node.getElementsByTagName) {
adamc@899 310 var savedScript = thisScript;
adamc@603 311
adamc@899 312 var scripts = node.getElementsByTagName("script"), scriptsCopy = [];
adamc@899 313 var len = scripts.length;
adamc@899 314 for (var i = 0; i < len; ++i)
adamc@899 315 scriptsCopy[i] = scripts[i];
adamc@899 316 for (var i = 0; i < len; ++i) {
adamc@899 317 thisScript = scriptsCopy[i];
adamc@899 318 try {
adamc@1036 319 eval(thisScript.text);
adamc@899 320 } catch (v) {
adamc@899 321 doExn(v);
adamc@899 322 }
adamc@899 323 if (thisScript.parentNode)
adamc@901 324 thisScript.parentNode.removeChild(thisScript);
adamc@729 325 }
adamc@899 326
adamc@899 327 thisScript = savedScript;
adamc@604 328 }
adamc@603 329 }
adamc@603 330
adamc@603 331
adamc@692 332 // Dynamic tree entry points
adamc@692 333
adamc@967 334 function killScript(scr) {
adamc@967 335 scr.dead = true;
adamc@967 336 for (var ls = scr.sources; ls; ls = ls.next)
adamc@967 337 ls.data.dyns = remove(scr, ls.data.dyns);
adamc@967 338 for (var ls = scr.closures; ls; ls = ls.next)
adamc@967 339 freeClosure(ls.data);
adamc@967 340 }
adamc@967 341
adamc@901 342 function dyn(pnode, s) {
adamc@901 343 var x = document.createElement("script");
adamc@690 344 x.dead = false;
adamc@690 345 x.signal = s;
adamc@690 346 x.sources = null;
adamc@703 347 x.closures = null;
adamc@899 348
adamc@902 349 var firstChild = null;
adamc@899 350
adamc@690 351 x.recreate = function(v) {
adamc@703 352 for (var ls = x.closures; ls; ls = ls.next)
adamc@703 353 freeClosure(ls.data);
adamc@692 354
adamc@902 355 var next;
adamc@902 356 for (var child = firstChild; child && child != x; child = next) {
adamc@902 357 next = child.nextSibling;
adamc@967 358 killScript(child);
adamc@901 359 if (child.getElementsByTagName) {
adamc@901 360 var arr = child.getElementsByTagName("script");
adamc@967 361 for (var i = 0; i < arr.length; ++i)
adamc@967 362 killScript(arr[i]);
adamc@847 363 }
adamc@847 364
adamc@901 365 if (child.parentNode)
adamc@901 366 child.parentNode.removeChild(child);
adamc@899 367 }
adamc@690 368
adamc@703 369 var cls = {v : null};
adamc@1036 370 var html = dynPrefix + flatten(cls, v);
adamc@703 371 x.closures = cls.v;
adamc@899 372
adamc@901 373 if (pnode == "table") {
adamc@901 374 var dummy = document.createElement("body");
adamc@901 375 dummy.innerHTML = "<table>" + html + "</table>";
adamc@901 376 runScripts(dummy);
adamc@901 377 var table = x.parentNode;
adamc@901 378
adamc@946 379 if (table) {
adamc@946 380 var arr = dummy.getElementsByTagName("tbody");
adamc@946 381 firstChild = null;
adamc@946 382 var tbody;
adamc@946 383 if (arr.length > 0 && table != null)
adamc@946 384 tbody = arr[0];
adamc@946 385 else
adamc@946 386 tbody = dummy.firstChild;
adamc@945 387
adamc@946 388 var next;
adamc@946 389 firstChild = document.createElement("script");
adamc@946 390 table.insertBefore(firstChild, x);
adamc@946 391 for (var node = tbody.firstChild; node; node = next) {
adamc@946 392 next = node.nextSibling;
adamc@946 393 table.insertBefore(node, x);
adamc@946 394 }
adamc@901 395 }
adamc@903 396 } else if (pnode == "tr") {
adamc@903 397 var dummy = document.createElement("body");
adamc@903 398 dummy.innerHTML = "<table><tr>" + html + "</tr></table>";
adamc@903 399 runScripts(dummy);
adamc@903 400 var table = x.parentNode;
adamc@903 401
adamc@946 402 if (table) {
adamc@946 403 var arr = dummy.getElementsByTagName("tr");
adamc@946 404 firstChild = null;
adamc@946 405 var tbody;
adamc@946 406 if (arr.length > 0 && table != null)
adamc@946 407 tbody = arr[0];
adamc@946 408 else
adamc@946 409 tbody = dummy.firstChild;
adamc@946 410
adamc@946 411 var next;
adamc@903 412 firstChild = document.createElement("script");
adamc@903 413 table.insertBefore(firstChild, x);
adamc@903 414 for (var node = tbody.firstChild; node; node = next) {
adamc@903 415 next = node.nextSibling;
adamc@903 416 table.insertBefore(node, x);
adamc@903 417 }
adamc@903 418 }
adamc@901 419 } else {
adamc@902 420 firstChild = document.createElement("span");
adamc@902 421 firstChild.innerHTML = html;
adamc@902 422 runScripts(firstChild);
adamc@900 423 if (x.parentNode)
adamc@902 424 x.parentNode.insertBefore(firstChild, x);
adamc@899 425 }
adamc@690 426 };
adamc@899 427
adamc@899 428 addNode(x);
adamc@690 429 populate(x);
adamc@571 430 }
adamc@582 431
adamc@817 432 function input(t, s, recreate) {
adamc@598 433 var x = document.createElement(t);
adamc@690 434 x.dead = false;
adamc@690 435 x.signal = ss(s);
adamc@690 436 x.sources = null;
adamc@817 437 x.recreate = recreate(x);
adamc@899 438 addNode(x);
adamc@690 439 populate(x);
adamc@816 440
adamc@816 441 return x;
adamc@816 442 }
adamc@816 443
adamc@816 444 function inp(s) {
adamc@817 445 var x = input("input", s, function(x) { return function(v) { if (x.value != v) x.value = v; }; });
adamc@816 446 x.value = s.data;
adamc@816 447 x.onkeyup = function() { sv(s, x.value) };
adamc@816 448
adamc@816 449 return x;
adamc@816 450 }
adamc@816 451
adamc@816 452 function sel(s, content) {
adamc@817 453 var x = input("select", s, function(x) { return function(v) { if (x.value != v) x.value = v; }; });
adamc@816 454 x.innerHTML = content;
adamc@816 455 x.value = s.data;
adamc@835 456 if (x.value != s.data)
adamc@835 457 sv(s, x.value);
adamc@816 458 x.onchange = function() { sv(s, x.value) };
adamc@797 459
adamc@606 460 return x;
adamc@598 461 }
adamc@598 462
adamc@817 463 function chk(s) {
adamc@817 464 var x = input("input", s, function(x) { return function(v) { if (x.checked != v) x.checked = v; }; });
adamc@817 465 x.type = "checkbox";
adamc@817 466 x.checked = s.data;
adamc@817 467 x.onchange = function() { sv(s, x.checked) };
adamc@817 468
adamc@817 469 return x;
adamc@817 470 }
adamc@817 471
adamc@800 472 function addOnChange(x, f) {
adamc@800 473 var old = x.onchange;
adamc@800 474 x.onchange = function() { old(); f (); };
adamc@800 475 }
adamc@800 476
adamc@692 477
adamc@692 478 // Basic string operations
adamc@692 479
adamc@597 480 function eh(x) {
adamc@800 481 if (x == null)
adamc@800 482 return "NULL";
adamc@800 483 else
adamc@800 484 return x.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
adamc@597 485 }
adamc@597 486
adamc@582 487 function ts(x) { return x.toString() }
adamc@586 488 function bs(b) { return (b ? "True" : "False") }
adamc@586 489
adamc@828 490 function sub(s, i) { return s[i]; }
adamc@828 491 function suf(s, i) { return s.substring(i); }
adamc@828 492 function slen(s) { return s.length; }
adamc@829 493 function sidx(s, ch) {
adamc@829 494 var r = s.indexOf(ch);
adamc@829 495 if (r == -1)
adamc@829 496 return null;
adamc@829 497 else
adamc@829 498 return r;
adamc@829 499 }
adamc@831 500 function sspn(s, chs) {
adamc@831 501 for (var i = 0; i < s.length; ++i)
adamc@831 502 if (chs.indexOf(s[i]) != -1)
adamc@831 503 return i;
adamc@831 504
adamc@831 505 return null;
adamc@831 506 }
adamc@829 507 function schr(s, ch) {
adamc@829 508 var r = s.indexOf(ch);
adamc@829 509 if (r == -1)
adamc@829 510 return null;
adamc@829 511 else
adamc@829 512 return s.substring(r);
adamc@829 513 }
adamc@829 514 function ssub(s, start, len) {
adamc@829 515 return s.substring(start, start+len);
adamc@829 516 }
adamc@821 517
adamc@649 518 function pi(s) {
adamc@649 519 var r = parseInt(s);
adamc@649 520 if (r.toString() == s)
adamc@649 521 return r;
adamc@649 522 else
adamc@729 523 er("Can't parse int: " + s);
adamc@649 524 }
adamc@649 525
adamc@649 526 function pfl(s) {
adamc@649 527 var r = parseFloat(s);
adamc@649 528 if (r.toString() == s)
adamc@649 529 return r;
adamc@649 530 else
adamc@729 531 er("Can't parse float: " + s);
adamc@649 532 }
adamc@649 533
adamc@838 534 function pio(s) {
adamc@838 535 var r = parseInt(s);
adamc@838 536 if (r.toString() == s)
adamc@838 537 return r;
adamc@838 538 else
adamc@838 539 return null;
adamc@838 540 }
adamc@838 541
adamc@838 542 function pflo(s) {
adamc@838 543 var r = parseFloat(s);
adamc@838 544 if (r.toString() == s)
adamc@838 545 return r;
adamc@838 546 else
adamc@838 547 return null;
adamc@838 548 }
adamc@838 549
adamc@692 550 function uf(s) {
adamc@925 551 if (s.length == 0)
adamc@925 552 return "_";
adamc@925 553 return (s[0] == '_' ? "_" : "")
adamc@925 554 + escape(s).replace(new RegExp ("/", "g"), "%2F").replace(new RegExp ("\\+", "g"), "%2B");
adamc@691 555 }
adamc@691 556
adamc@692 557 function uu(s) {
adamc@925 558 if (s.length > 0 && s[0] == '_')
adamc@925 559 s = s.substring(1);
adamc@927 560 else if (s.length >= 3 && s[0] == '%' && s[1] == '5' && (s[2] == 'f' || s[2] == 'F'))
adamc@927 561 s = s.substring(3);
adamc@905 562 return unescape(s.replace(new RegExp ("\\+", "g"), " "));
adamc@905 563 }
adamc@905 564
adamc@912 565 function ub(b) {
adamc@912 566 return b ? "1" : "0";
adamc@912 567 }
adamc@912 568
adamc@905 569 function uul(getToken, getData) {
adamc@905 570 var tok = getToken();
adamc@905 571 if (tok == "Nil") {
adamc@905 572 return null;
adamc@905 573 } else if (tok == "Cons") {
adamc@905 574 var d = getData();
adamc@905 575 var l = uul(getToken, getData);
adamc@905 576 return {_1:d, _2:l};
adamc@905 577 } else
adamc@982 578 whine("Can't unmarshal list (" + tok + ")");
adamc@692 579 }
adamc@692 580
adamc@961 581 function strcmp(str1, str2) {
adamc@961 582 return ((str1 == str2) ? 0 : ((str1 > str2) ? 1 : -1));
adamc@961 583 }
adamc@692 584
adamc@603 585
adamc@692 586 // Remote calls
adamc@609 587
adamc@703 588 var client_id = null;
adamc@668 589 var client_pass = 0;
adamc@668 590 var url_prefix = "/";
adamc@673 591 var timeout = 60;
adamc@668 592
adamc@668 593 function getXHR(uri)
adamc@609 594 {
adamc@609 595 try {
adamc@609 596 return new XMLHttpRequest();
adamc@609 597 } catch (e) {
adamc@609 598 try {
adamc@609 599 return new ActiveXObject("Msxml2.XMLHTTP");
adamc@609 600 } catch (e) {
adamc@609 601 try {
adamc@609 602 return new ActiveXObject("Microsoft.XMLHTTP");
adamc@609 603 } catch (e) {
adamc@982 604 whine("Your browser doesn't seem to support AJAX.");
adamc@609 605 }
adamc@609 606 }
adamc@609 607 }
adamc@609 608 }
adamc@609 609
adamc@736 610 var sig = null;
adamc@736 611
adamc@736 612 function requestUri(xhr, uri, needsSig) {
adamc@1037 613 xhr.open("POST", uri, true);
adamc@668 614
adamc@703 615 if (client_id != null) {
adamc@668 616 xhr.setRequestHeader("UrWeb-Client", client_id.toString());
adamc@668 617 xhr.setRequestHeader("UrWeb-Pass", client_pass.toString());
adamc@668 618 }
adamc@668 619
adamc@736 620 if (needsSig) {
adamc@736 621 if (sig == null)
adamc@736 622 whine("Missing cookie signature!");
adamc@736 623
adamc@736 624 xhr.setRequestHeader("UrWeb-Sig", sig);
adamc@736 625 }
adamc@736 626
adamc@668 627 xhr.send(null);
adamc@668 628 }
adamc@668 629
adamc@978 630 function rc(prefix, uri, parse, k, needsSig) {
adamc@978 631 uri = cat(prefix, uri);
adamc@728 632 uri = flattenLocal(uri);
adamc@609 633 var xhr = getXHR();
adamc@609 634
adamc@609 635 xhr.onreadystatechange = function() {
adamc@612 636 if (xhr.readyState == 4) {
adamc@612 637 var isok = false;
adamc@612 638
adamc@612 639 try {
adamc@612 640 if (xhr.status == 200)
adamc@612 641 isok = true;
adamc@612 642 } catch (e) { }
adamc@612 643
adamc@729 644 if (isok) {
adamc@729 645 try {
adamc@1020 646 k(parse(xhr.responseText));
adamc@729 647 } catch (v) {
adamc@729 648 doExn(v);
adamc@729 649 }
adamc@729 650 } else {
adamc@729 651 conn();
adamc@649 652 }
adamc@612 653 }
adamc@609 654 };
adamc@609 655
adamc@736 656 requestUri(xhr, uri, needsSig);
adamc@609 657 }
adamc@667 658
adamc@667 659 function path_join(s1, s2) {
adamc@667 660 if (s1.length > 0 && s1[s1.length-1] == '/')
adamc@667 661 return s1 + s2;
adamc@667 662 else
adamc@667 663 return s1 + "/" + s2;
adamc@667 664 }
adamc@667 665
adamc@670 666 var channels = [];
adamc@670 667
adamc@670 668 function newQueue() {
adamc@670 669 return { front : null, back : null };
adamc@670 670 }
adamc@670 671 function enqueue(q, v) {
adamc@670 672 if (q.front == null) {
adamc@670 673 q.front = cons(v, null);
adamc@670 674 q.back = q.front;
adamc@670 675 } else {
adamc@670 676 var node = cons(v, null);
adamc@690 677 q.back.next = node;
adamc@670 678 q.back = node;
adamc@670 679 }
adamc@670 680 }
adamc@670 681 function dequeue(q) {
adamc@670 682 if (q.front == null)
adamc@670 683 return null;
adamc@670 684 else {
adamc@690 685 var r = q.front.data;
adamc@690 686 q.front = q.front.next;
adamc@670 687 if (q.front == null)
adamc@670 688 q.back = null;
adamc@670 689 return r;
adamc@670 690 }
adamc@670 691 }
adamc@670 692
adamc@670 693 function newChannel() {
adamc@670 694 return { msgs : newQueue(), listeners : newQueue() };
adamc@670 695 }
adamc@670 696
adamc@667 697 function listener() {
adamc@668 698 var uri = path_join(url_prefix, ".msgs");
adamc@667 699 var xhr = getXHR();
adamc@673 700 var tid, orsc, onTimeout;
adamc@673 701
adamc@673 702 var connect = function () {
adamc@673 703 xhr.onreadystatechange = orsc;
adamc@673 704 tid = window.setTimeout(onTimeout, timeout * 500);
adamc@736 705 requestUri(xhr, uri, false);
adamc@673 706 }
adamc@673 707
adamc@673 708 orsc = function() {
adamc@667 709 if (xhr.readyState == 4) {
adamc@673 710 window.clearTimeout(tid);
adamc@673 711
adamc@667 712 var isok = false;
adamc@667 713
adamc@667 714 try {
adamc@667 715 if (xhr.status == 200)
adamc@667 716 isok = true;
adamc@667 717 } catch (e) { }
adamc@667 718
adamc@668 719 if (isok) {
adamc@889 720 var text = xhr.responseText
adamc@889 721 if (text == "")
adamc@889 722 return;
adamc@889 723 var lines = text.split("\n");
adamc@889 724
adamc@729 725 if (lines.length < 2) {
adamc@729 726 discon();
adamc@729 727 return;
adamc@729 728 }
adamc@669 729
adamc@669 730 for (var i = 0; i+1 < lines.length; i += 2) {
adamc@670 731 var chn = lines[i];
adamc@670 732 var msg = lines[i+1];
adamc@670 733
adamc@670 734 if (chn < 0)
adamc@670 735 whine("Out-of-bounds channel in message from remote server");
adamc@670 736
adamc@670 737 var ch;
adamc@670 738
adamc@670 739 if (chn >= channels.length || channels[chn] == null) {
adamc@670 740 ch = newChannel();
adamc@670 741 channels[chn] = ch;
adamc@670 742 } else
adamc@670 743 ch = channels[chn];
adamc@670 744
adamc@670 745 var listener = dequeue(ch.listeners);
adamc@670 746 if (listener == null) {
adamc@670 747 enqueue(ch.msgs, msg);
adamc@670 748 } else {
adamc@728 749 try {
adamc@728 750 listener(msg);
adamc@728 751 } catch (v) {
adamc@728 752 doExn(v);
adamc@728 753 }
adamc@670 754 }
adamc@669 755 }
adamc@669 756
adamc@673 757 connect();
adamc@668 758 }
adamc@667 759 else {
adamc@729 760 try {
adamc@824 761 if (xhr.status != 0)
adamc@824 762 servErr("Error querying remote server for messages: " + xhr.status);
adamc@889 763 } catch (e) { }
adamc@667 764 }
adamc@667 765 }
adamc@667 766 };
adamc@667 767
adamc@673 768 onTimeout = function() {
adamc@673 769 xhr.abort();
adamc@673 770 connect();
adamc@673 771 };
adamc@673 772
adamc@673 773 connect();
adamc@667 774 }
adamc@670 775
adamc@670 776 function rv(chn, parse, k) {
adamc@682 777 if (chn == null)
adamc@682 778 return;
adamc@682 779
adamc@670 780 if (chn < 0)
adamc@670 781 whine("Out-of-bounds channel receive");
adamc@670 782
adamc@670 783 var ch;
adamc@670 784
adamc@670 785 if (chn >= channels.length || channels[chn] == null) {
adamc@670 786 ch = newChannel();
adamc@670 787 channels[chn] = ch;
adamc@670 788 } else
adamc@670 789 ch = channels[chn];
adamc@670 790
adamc@670 791 var msg = dequeue(ch.msgs);
adamc@670 792 if (msg == null) {
adamc@1021 793 enqueue(ch.listeners, function(msg) { k(parse(msg)); });
adamc@670 794 } else {
adamc@728 795 try {
adamc@1021 796 k(parse(msg));
adamc@728 797 } catch (v) {
adamc@728 798 doExn(v);
adamc@728 799 }
adamc@670 800 }
adamc@670 801 }
adamc@693 802
adamc@978 803 function sl(ms, k) {
adamc@1021 804 window.setTimeout(function() { k(null); }, ms);
adamc@1021 805 }
adamc@1021 806
adamc@1021 807 function sp(e) {
adamc@1021 808 execF(e, null);
adamc@978 809 }
adamc@978 810
adamc@693 811
adamc@895 812 // Key events
adamc@895 813
adamc@970 814 var uw_event = null;
adamc@970 815
adamc@970 816 function kc() {
adamc@970 817 return window.event ? uw_event.keyCode : uw_event.which;
adamc@970 818 }
adamc@970 819
adamc@970 820
adamc@970 821 // The Ur interpreter
adamc@970 822
adamc@970 823 var urfuncs = [];
adamc@970 824
adamc@970 825 function lookup(env, n) {
adamc@970 826 while (env != null) {
adamc@970 827 if (n == 0)
adamc@970 828 return env.data;
adamc@970 829 else {
adamc@970 830 --n;
adamc@970 831 env = env.next;
adamc@970 832 }
adamc@970 833 }
adamc@970 834
adamc@982 835 whine("Out-of-bounds Ur variable reference");
adamc@970 836 }
adamc@970 837
adamc@974 838 function execP(env, p, v) {
adamc@974 839 switch (p.c) {
adamc@974 840 case "w":
adamc@974 841 return env;
adamc@974 842 case "v":
adamc@974 843 return cons(v, env);
adamc@974 844 case "c":
adamc@974 845 if (v == p.v)
adamc@974 846 return env;
adamc@974 847 else
adamc@974 848 return false;
adamc@974 849 case "s":
adamc@974 850 if (v == null)
adamc@974 851 return false;
adamc@974 852 else
adamc@974 853 return execP(env, p.p, p.n ? v.v : v);
adamc@974 854 case "1":
adamc@974 855 if (v.n != p.n)
adamc@974 856 return false;
adamc@974 857 else
adamc@974 858 return execP(env, p.p, v.v);
adamc@974 859 case "r":
adamc@974 860 for (var fs = p.l; fs != null; fs = fs.next) {
adamc@974 861 env = execP(env, fs.data.p, v["_" + fs.data.n]);
adamc@974 862 if (env == false)
adamc@974 863 return false;
adamc@974 864 }
adamc@974 865 return env;
adamc@974 866 default:
adamc@982 867 whine("Unknown Ur pattern kind" + p.c);
adamc@974 868 }
adamc@974 869 }
adamc@974 870
adamc@970 871 function exec0(env, e) {
adamc@1020 872 return exec1(env, null, e);
adamc@1020 873 }
adamc@1020 874
adamc@1020 875 function exec1(env, stack, e) {
adamc@1020 876 var stack, usedK = false;
adamc@970 877
adamc@983 878 var saveEnv = function() {
adamc@983 879 if (stack.next != null && stack.next.data.c != "<")
adamc@983 880 stack = cons({c: "<", env: env}, stack.next);
adamc@983 881 else
adamc@983 882 stack = stack.next;
adamc@983 883 };
adamc@983 884
adamc@970 885 while (true) {
adamc@970 886 switch (e.c) {
adamc@970 887 case "c":
adamc@970 888 var v = e.v;
adamc@970 889 if (stack == null)
adamc@970 890 return v;
adamc@970 891 var fr = stack.data;
adamc@970 892
adamc@970 893 switch (fr.c) {
adamc@970 894 case "s":
adamc@970 895 e = {c: "c", v: {v: v}};
adamc@970 896 stack = stack.next;
adamc@970 897 break;
adamc@970 898 case "1":
adamc@970 899 e = {c: "c", v: {n: fr.n, v: v}};
adamc@970 900 stack = stack.next;
adamc@970 901 break;
adamc@970 902 case "f":
adamc@970 903 fr.args[fr.pos++] = v;
adamc@970 904 if (fr.a == null) {
adamc@1035 905 var res;
adamc@1020 906 stack = stack.next;
adamc@1035 907
adamc@1035 908 if (fr.f.apply)
adamc@1035 909 res = fr.f.apply(null, fr.args);
adamc@1035 910 else if (fr.args.length == 0)
adamc@1035 911 res = fr.f();
adamc@1035 912 else if (fr.args.length == 1)
adamc@1035 913 res = fr.f(fr.args[0]);
adamc@1035 914 else if (fr.args.length == 2)
adamc@1035 915 res = fr.f(fr.args[0], fr.args[1]);
adamc@1035 916 else if (fr.args.length == 3)
adamc@1035 917 res = fr.f(fr.args[0], fr.args[1], fr.args[2]);
adamc@1035 918 else if (fr.args.length == 4)
adamc@1035 919 res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3]);
adamc@1035 920 else if (fr.args.length == 5)
adamc@1035 921 res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3], fr.args[4]);
adamc@1035 922 else
adamc@1035 923 whine("Native function has " + fr.args.length + " args, but there is no special case for that count.");
adamc@1035 924
adamc@1035 925 e = {c: "c", v: res};
adamc@1020 926 if (usedK) return null;
adamc@970 927 } else {
adamc@970 928 e = fr.a.data;
adamc@970 929 fr.a = fr.a.next;
adamc@970 930 }
adamc@970 931 break;
adamc@970 932 case "a1":
adamc@970 933 e = fr.x;
adamc@971 934 stack = cons({c: "a2", f: v}, stack.next);
adamc@970 935 break;
adamc@970 936 case "a2":
adamc@971 937 if (fr.f == null)
adamc@982 938 whine("Ur: applying null function");
adamc@971 939 else if (fr.f.body) {
adamc@983 940 saveEnv();
adamc@971 941 env = cons(v, fr.f.env);
adamc@971 942 e = fr.f.body;
adamc@971 943 } else {
adamc@971 944 e = {c: "c", v: fr.f(v)};
adamc@971 945 stack = stack.next;
adamc@971 946 }
adamc@970 947 break;
adamc@978 948 case "<":
adamc@970 949 env = fr.env;
adamc@970 950 stack = stack.next;
adamc@970 951 break;
adamc@970 952 case "r":
adamc@970 953 fr.fs["_" + fr.n] = v;
adamc@970 954 if (fr.l == null) {
adamc@970 955 e = {c: "c", v: fr.fs};
adamc@970 956 stack = stack.next;
adamc@970 957 } else {
adamc@970 958 fr.n = fr.l.data.n;
adamc@970 959 e = fr.l.data.v;
adamc@970 960 fr.l = fr.l.next;
adamc@970 961 }
adamc@970 962 break;
adamc@970 963 case ".":
adamc@970 964 e = {c: "c", v: v["_" + fr.f]};
adamc@970 965 stack = stack.next;
adamc@970 966 break;
adamc@970 967 case ";":
adamc@970 968 e = fr.e2;
adamc@970 969 stack = stack.next;
adamc@970 970 break;
adamc@970 971 case "=":
adamc@983 972 saveEnv();
adamc@970 973 env = cons(v, env);
adamc@970 974 e = fr.e2;
adamc@970 975 break;
adamc@974 976 case "m":
adamc@974 977 var ps;
adamc@974 978 for (ps = fr.p; ps != null; ps = ps.next) {
adamc@974 979 var r = execP(env, ps.data.p, v);
adamc@974 980 if (r != false) {
adamc@983 981 saveEnv();
adamc@974 982 env = r;
adamc@974 983 e = ps.data.b;
adamc@974 984 break;
adamc@974 985 }
adamc@974 986 }
adamc@974 987 if (ps == null)
adamc@982 988 whine("Match failure in Ur interpretation");
adamc@974 989 break;
adamc@970 990 default:
adamc@982 991 whine("Unknown Ur continuation kind " + fr.c);
adamc@970 992 }
adamc@970 993
adamc@970 994 break;
adamc@970 995 case "v":
adamc@970 996 e = {c: "c", v: lookup(env, e.n)};
adamc@970 997 break;
adamc@970 998 case "n":
adamc@971 999 e = urfuncs[e.n];
adamc@970 1000 break;
adamc@970 1001 case "s":
adamc@970 1002 stack = cons({c: "s"}, stack);
adamc@970 1003 e = e.v;
adamc@970 1004 break;
adamc@970 1005 case "1":
adamc@970 1006 stack = cons({c: "1", n: e.n}, stack);
adamc@970 1007 e = e.v;
adamc@970 1008 break;
adamc@970 1009 case "f":
adamc@970 1010 if (e.a == null)
adamc@970 1011 e = {c: "c", v: e.f()};
adamc@970 1012 else {
adamc@970 1013 var args = [];
adamc@970 1014 stack = cons({c: "f", f: e.f, args: args, pos: 0, a: e.a.next}, stack);
adamc@982 1015 if (!e.a.data.c) alert("[2] fr.f = " + e.f + "; 0 = " + e.a.data);
adamc@970 1016 e = e.a.data;
adamc@970 1017 }
adamc@970 1018 break;
adamc@970 1019 case "l":
adamc@970 1020 e = {c: "c", v: {env: env, body: e.b}};
adamc@970 1021 break;
adamc@970 1022 case "a":
adamc@970 1023 stack = cons({c: "a1", x: e.x}, stack);
adamc@970 1024 e = e.f;
adamc@970 1025 break;
adamc@970 1026 case "r":
adamc@970 1027 if (e.l == null)
adamc@982 1028 whine("Empty Ur record in interpretation");
adamc@970 1029 var fs = {};
adamc@970 1030 stack = cons({c: "r", n: e.l.data.n, fs: fs, l: e.l.next}, stack);
adamc@973 1031 e = e.l.data.v;
adamc@970 1032 break;
adamc@970 1033 case ".":
adamc@970 1034 stack = cons({c: ".", f: e.f}, stack);
adamc@970 1035 e = e.r;
adamc@970 1036 break;
adamc@970 1037 case ";":
adamc@970 1038 stack = cons({c: ";", e2: e.e2}, stack);
adamc@970 1039 e = e.e1;
adamc@970 1040 break;
adamc@970 1041 case "=":
adamc@970 1042 stack = cons({c: "=", e2: e.e2}, stack);
adamc@970 1043 e = e.e1;
adamc@970 1044 break;
adamc@974 1045 case "m":
adamc@974 1046 stack = cons({c: "m", p: e.p}, stack);
adamc@974 1047 e = e.e;
adamc@974 1048 break;
adamc@970 1049 case "e":
adamc@975 1050 e = {c: "c", v: cs({c: "wc", env: env, body: e.e})};
adamc@975 1051 break;
adamc@975 1052 case "wc":
adamc@975 1053 env = e.env;
adamc@975 1054 e = e.body;
adamc@970 1055 break;
adamc@1020 1056 case "K":
adamc@1020 1057 { var savedStack = stack.next, savedEnv = env;
adamc@1020 1058 e = {c: "c", v: function(v) { return exec1(savedEnv, savedStack, {c: "c", v: v}); } };}
adamc@1020 1059 usedK = true;
adamc@1020 1060 break;
adamc@970 1061 default:
adamc@982 1062 whine("Unknown Ur expression kind " + e.c);
adamc@970 1063 }
adamc@970 1064 }
adamc@970 1065 }
adamc@970 1066
adamc@976 1067 function execD(e) {
adamc@976 1068 return exec0(null, e);
adamc@976 1069 }
adamc@976 1070
adamc@970 1071 function exec(e) {
adamc@970 1072 var r = exec0(null, e);
adamc@970 1073
adamc@975 1074 if (r != null && r.body != null)
adamc@970 1075 return function(v) { return exec0(cons(v, r.env), r.body); };
adamc@970 1076 else
adamc@970 1077 return r;
adamc@895 1078 }
adamc@895 1079
adamc@977 1080 function execF(f, x) {
adamc@977 1081 return exec0(cons(x, f.env), f.body);
adamc@977 1082 }
adamc@977 1083
adamc@895 1084
adamc@693 1085 // App-specific code
adamc@693 1086