annotate lib/js/urweb.js @ 1987:fa044101ad99

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