annotate lib/js/urweb.js @ 1950:b3c8860508ea

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