annotate lib/js/urweb.js @ 1597:e283ca05c829

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