annotate lib/js/urweb.js @ 1691:ea292bf9431f

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