annotate lib/js/urweb.js @ 1487:290de2dcecf6

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