annotate lib/js/urweb.js @ 1555:d5c961c709f9

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