annotate lib/js/urweb.js @ 1579:e1f4ac9ca34b

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