annotate lib/js/urweb.js @ 1559:df6a7a22760a

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