annotate lib/js/urweb.js @ 1625:bd34a4af516a

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