annotate lib/js/urweb.js @ 1622:a20daedfd1d0

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