annotate lib/js/urweb.js @ 1202:509a6d7b60fb

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