annotate lib/js/urweb.js @ 1821:e8149592990e

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