annotate lib/js/urweb.js @ 726:6fc633d990e7

onError
author Adam Chlipala <adamc@hcoop.net>
date Thu, 16 Apr 2009 12:36:01 -0400
parents a5d8b470d7ca
children 2197f0e24a9f
rev   line source
adamc@692 1 // Lists
adamc@692 2
adamc@580 3 function cons(v, ls) {
adamc@690 4 return { next : ls, data : v };
adamc@690 5 }
adamc@690 6 function concat(ls1, ls2) {
adamc@690 7 return (ls1 ? cons(ls1.data, concat(ls1.next, ls2)) : ls2);
adamc@690 8 }
adamc@690 9 function member(x, ls) {
adamc@690 10 for (; ls; ls = ls.next)
adamc@690 11 if (ls.data == x)
adamc@690 12 return true;
adamc@690 13 return false;
adamc@690 14 }
adamc@690 15 function remove(x, ls) {
adamc@690 16 return (ls ? (ls.data == x ? ls.next : cons(ls.data, remove(x, ls.next))) : null);
adamc@690 17 }
adamc@690 18 function union(ls1, ls2) {
adamc@690 19 return (ls1 ? (member(ls1.data, ls2) ? union(ls1.next, ls2) : cons(ls1.data, union(ls1.next, ls2))) : ls2);
adamc@580 20 }
adamc@703 21 function length(ls) {
adamc@703 22 return (ls ? 1 + length(ls.next) : 0);
adamc@703 23 }
adamc@670 24
adamc@690 25
adamc@692 26 // Embedding closures in XML strings
adamc@692 27
adamc@693 28 function cs(f) {
adamc@693 29 return {closure: f};
adamc@693 30 }
adamc@693 31
adamc@693 32 function isWeird(v) {
adamc@693 33 return v.closure != null || v.cat1 != null;
adamc@693 34 }
adamc@693 35
adamc@692 36 function cat(s1, s2) {
adamc@693 37 if (isWeird(s1) || isWeird(s2))
adamc@693 38 return {cat1: s1, cat2: s2};
adamc@693 39 else
adamc@692 40 return s1 + s2;
adamc@692 41 }
adamc@692 42
adamc@692 43 var closures = [];
adamc@703 44 var freeClosures = null;
adamc@692 45
adamc@692 46 function newClosure(f) {
adamc@703 47 var n;
adamc@703 48 if (freeClosures == null) {
adamc@703 49 n = closures.length;
adamc@703 50 } else {
adamc@703 51 n = freeClosures.data;
adamc@703 52 freeClosures = freeClosures.next;
adamc@703 53 }
adamc@692 54 closures[n] = f;
adamc@692 55 return n;
adamc@692 56 }
adamc@692 57
adamc@703 58 function freeClosure(n) {
adamc@703 59 closures[n] = null;
adamc@703 60 freeClosures = cons(n, freeClosures);
adamc@703 61 }
adamc@703 62
adamc@692 63 function cr(n) {
adamc@692 64 return closures[n]();
adamc@692 65 }
adamc@692 66
adamc@703 67 function flatten(cls, tr) {
adamc@693 68 if (tr.cat1 != null)
adamc@703 69 return flatten(cls, tr.cat1) + flatten(cls, tr.cat2);
adamc@703 70 else if (tr.closure != null) {
adamc@703 71 var cl = newClosure(tr.closure);
adamc@703 72 cls.v = cons(cl, cls.v);
adamc@703 73 return "cr(" + cl + ")";
adamc@703 74 } else
adamc@692 75 return tr;
adamc@692 76 }
adamc@692 77
adamc@692 78
adamc@692 79
adamc@692 80 // Dynamic tree management
adamc@692 81
adamc@690 82 function populate(node) {
adamc@690 83 var s = node.signal;
adamc@690 84 var oldSources = node.sources;
adamc@690 85 var sr = s();
adamc@690 86 var newSources = sr.sources;
adamc@690 87
adamc@690 88 for (var sp = oldSources; sp; sp = sp.next)
adamc@690 89 if (!member(sp.data, newSources))
adamc@690 90 sp.data.dyns = remove(node, sp.data.dyns);
adamc@690 91
adamc@690 92 for (var sp = newSources; sp; sp = sp.next)
adamc@690 93 if (!member(sp.data, oldSources))
adamc@690 94 sp.data.dyns = cons(node, sp.data.dyns);
adamc@690 95
adamc@690 96 node.sources = newSources;
adamc@690 97 node.recreate(sr.data);
adamc@579 98 }
adamc@574 99
adamc@580 100 function sc(v) {
adamc@690 101 return {data : v, dyns : null};
adamc@580 102 }
adamc@580 103 function sv(s, v) {
adamc@690 104 s.data = v;
adamc@690 105 for (var ls = s.dyns; ls; ls = ls.next)
adamc@690 106 if (!ls.dead)
adamc@690 107 populate(ls.data);
adamc@580 108 }
adamc@601 109 function sg(s) {
adamc@690 110 return s.data;
adamc@601 111 }
adamc@579 112
adamc@580 113 function ss(s) {
adamc@690 114 return function() { return {sources : cons(s, null), data : s.data } };
adamc@580 115 }
adamc@580 116 function sr(v) {
adamc@690 117 return function() { return {sources : null, data : v } };
adamc@580 118 }
adamc@580 119 function sb(x,y) {
adamc@690 120 return function() {
adamc@690 121 var xr = x();
adamc@690 122 var yr = y(xr.data)();
adamc@690 123 return {sources : union(xr.sources, yr.sources), data : yr.data};
adamc@690 124 };
adamc@580 125 }
adamc@571 126
adamc@604 127 function lastParent() {
adamc@604 128 var pos = document;
adamc@604 129
adamc@600 130 while (pos.lastChild && pos.lastChild.nodeType == 1)
adamc@600 131 pos = pos.lastChild;
adamc@600 132
adamc@600 133 return pos.parentNode;
adamc@600 134 }
adamc@600 135
adamc@604 136 function addNode(node) {
adamc@604 137 if (thisScript) {
adamc@604 138 thisScript.parentNode.appendChild(node);
adamc@604 139 thisScript.parentNode.removeChild(thisScript);
adamc@604 140 } else
adamc@604 141 lastParent().appendChild(node);
adamc@603 142 }
adamc@603 143
adamc@690 144 var thisScript = null;
adamc@690 145
adamc@604 146 function runScripts(node) {
adamc@604 147 var savedScript = thisScript;
adamc@603 148
adamc@692 149 var scripts = node.getElementsByTagName("script"), scriptsCopy = [];
adamc@604 150 var len = scripts.length;
adamc@646 151 for (var i = 0; i < len; ++i)
adamc@646 152 scriptsCopy[i] = scripts[i];
adamc@604 153 for (var i = 0; i < len; ++i) {
adamc@646 154 thisScript = scriptsCopy[i];
adamc@604 155 eval(thisScript.textContent);
adamc@604 156 }
adamc@604 157
adamc@604 158 thisScript = savedScript;
adamc@603 159 }
adamc@603 160
adamc@603 161
adamc@692 162 // Dynamic tree entry points
adamc@692 163
adamc@692 164 var dynDepth = 0;
adamc@692 165
adamc@571 166 function dyn(s) {
adamc@571 167 var x = document.createElement("span");
adamc@690 168 x.dead = false;
adamc@690 169 x.signal = s;
adamc@690 170 x.sources = null;
adamc@703 171 x.closures = null;
adamc@690 172 x.recreate = function(v) {
adamc@703 173 for (var ls = x.closures; ls; ls = ls.next)
adamc@703 174 freeClosure(ls.data);
adamc@692 175
adamc@690 176 var spans = x.getElementsByTagName("span");
adamc@690 177 for (var i = 0; i < spans.length; ++i) {
adamc@690 178 var span = spans[i];
adamc@690 179 span.dead = true;
adamc@690 180 for (var ls = span.sources; ls; ls = ls.next)
adamc@690 181 ls.data.dyns = remove(span, ls.data.dyns);
adamc@703 182 for (var ls = span.closures; ls; ls = ls.next)
adamc@703 183 freeClosure(ls.data);
adamc@690 184 }
adamc@690 185
adamc@703 186 var cls = {v : null};
adamc@703 187 x.innerHTML = flatten(cls, v);
adamc@703 188 x.closures = cls.v;
adamc@690 189 runScripts(x);
adamc@690 190 };
adamc@703 191 addNode(x);
adamc@690 192 populate(x);
adamc@571 193 }
adamc@582 194
adamc@598 195 function inp(t, s) {
adamc@598 196 var x = document.createElement(t);
adamc@690 197 x.dead = false;
adamc@690 198 x.signal = ss(s);
adamc@690 199 x.sources = null;
adamc@690 200 x.recreate = function(v) { if (x.value != v) x.value = v; };
adamc@690 201 populate(x);
adamc@604 202 addNode(x);
adamc@598 203 x.onkeyup = function() { sv(s, x.value) };
adamc@606 204 return x;
adamc@598 205 }
adamc@598 206
adamc@692 207
adamc@692 208 // Basic string operations
adamc@692 209
adamc@597 210 function eh(x) {
adamc@597 211 return x.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
adamc@597 212 }
adamc@597 213
adamc@582 214 function ts(x) { return x.toString() }
adamc@586 215 function bs(b) { return (b ? "True" : "False") }
adamc@586 216
adamc@649 217 function pi(s) {
adamc@649 218 var r = parseInt(s);
adamc@649 219 if (r.toString() == s)
adamc@649 220 return r;
adamc@649 221 else
adamc@649 222 throw "Can't parse int: " + s;
adamc@649 223 }
adamc@649 224
adamc@649 225 function pfl(s) {
adamc@649 226 var r = parseFloat(s);
adamc@649 227 if (r.toString() == s)
adamc@649 228 return r;
adamc@649 229 else
adamc@649 230 throw "Can't parse float: " + s;
adamc@649 231 }
adamc@649 232
adamc@692 233 function uf(s) {
adamc@692 234 return escape(s).replace(new RegExp ("/", "g"), "%2F");
adamc@691 235 }
adamc@691 236
adamc@692 237 function uu(s) {
adamc@692 238 return unescape(s).replace(new RegExp ("\\+", "g"), " ");
adamc@692 239 }
adamc@692 240
adamc@692 241
adamc@692 242 // Error handling
adamc@692 243
adamc@669 244 function whine(msg) {
adamc@669 245 alert(msg);
adamc@669 246 throw msg;
adamc@669 247 }
adamc@669 248
adamc@649 249 function pf() {
adamc@669 250 whine("Pattern match failure");
adamc@649 251 }
adamc@589 252
adamc@726 253 var errorHandlers = null;
adamc@726 254
adamc@726 255 function onError(f) {
adamc@726 256 errorHandlers = cons(f, errorHandlers);
adamc@726 257 }
adamc@726 258
adamc@726 259 function er(s) {
adamc@726 260 for (var ls = errorHandlers; ls; ls = ls.next)
adamc@726 261 ls.data(s)(null);
adamc@726 262 throw s;
adamc@726 263 }
adamc@726 264
adamc@603 265
adamc@692 266 // Remote calls
adamc@609 267
adamc@703 268 var client_id = null;
adamc@668 269 var client_pass = 0;
adamc@668 270 var url_prefix = "/";
adamc@673 271 var timeout = 60;
adamc@668 272
adamc@668 273 function getXHR(uri)
adamc@609 274 {
adamc@609 275 try {
adamc@609 276 return new XMLHttpRequest();
adamc@609 277 } catch (e) {
adamc@609 278 try {
adamc@609 279 return new ActiveXObject("Msxml2.XMLHTTP");
adamc@609 280 } catch (e) {
adamc@609 281 try {
adamc@609 282 return new ActiveXObject("Microsoft.XMLHTTP");
adamc@609 283 } catch (e) {
adamc@609 284 throw "Your browser doesn't seem to support AJAX.";
adamc@609 285 }
adamc@609 286 }
adamc@609 287 }
adamc@609 288 }
adamc@609 289
adamc@668 290 function requestUri(xhr, uri) {
adamc@668 291 xhr.open("GET", uri, true);
adamc@668 292
adamc@703 293 if (client_id != null) {
adamc@668 294 xhr.setRequestHeader("UrWeb-Client", client_id.toString());
adamc@668 295 xhr.setRequestHeader("UrWeb-Pass", client_pass.toString());
adamc@668 296 }
adamc@668 297
adamc@668 298 xhr.send(null);
adamc@668 299 }
adamc@668 300
adamc@613 301 function rc(uri, parse, k) {
adamc@703 302 var cls = {v : null};
adamc@703 303 uri = flatten(cls, uri);
adamc@703 304 for (cl = cls.v; cl != null; cl = cl.next)
adamc@703 305 freeClosure(cl.data);
adamc@703 306
adamc@609 307 var xhr = getXHR();
adamc@609 308
adamc@609 309 xhr.onreadystatechange = function() {
adamc@612 310 if (xhr.readyState == 4) {
adamc@612 311 var isok = false;
adamc@612 312
adamc@612 313 try {
adamc@612 314 if (xhr.status == 200)
adamc@612 315 isok = true;
adamc@612 316 } catch (e) { }
adamc@612 317
adamc@612 318 if (isok)
adamc@613 319 k(parse(xhr.responseText));
adamc@649 320 else {
adamc@669 321 whine("Error querying remote server!");
adamc@649 322 }
adamc@612 323 }
adamc@609 324 };
adamc@609 325
adamc@668 326 requestUri(xhr, uri);
adamc@609 327 }
adamc@667 328
adamc@667 329 function path_join(s1, s2) {
adamc@667 330 if (s1.length > 0 && s1[s1.length-1] == '/')
adamc@667 331 return s1 + s2;
adamc@667 332 else
adamc@667 333 return s1 + "/" + s2;
adamc@667 334 }
adamc@667 335
adamc@670 336 var channels = [];
adamc@670 337
adamc@670 338 function newQueue() {
adamc@670 339 return { front : null, back : null };
adamc@670 340 }
adamc@670 341 function enqueue(q, v) {
adamc@670 342 if (q.front == null) {
adamc@670 343 q.front = cons(v, null);
adamc@670 344 q.back = q.front;
adamc@670 345 } else {
adamc@670 346 var node = cons(v, null);
adamc@690 347 q.back.next = node;
adamc@670 348 q.back = node;
adamc@670 349 }
adamc@670 350 }
adamc@670 351 function dequeue(q) {
adamc@670 352 if (q.front == null)
adamc@670 353 return null;
adamc@670 354 else {
adamc@690 355 var r = q.front.data;
adamc@690 356 q.front = q.front.next;
adamc@670 357 if (q.front == null)
adamc@670 358 q.back = null;
adamc@670 359 return r;
adamc@670 360 }
adamc@670 361 }
adamc@670 362
adamc@670 363 function newChannel() {
adamc@670 364 return { msgs : newQueue(), listeners : newQueue() };
adamc@670 365 }
adamc@670 366
adamc@667 367 function listener() {
adamc@668 368 var uri = path_join(url_prefix, ".msgs");
adamc@667 369 var xhr = getXHR();
adamc@673 370 var tid, orsc, onTimeout;
adamc@673 371
adamc@673 372 var connect = function () {
adamc@673 373 xhr.onreadystatechange = orsc;
adamc@673 374 tid = window.setTimeout(onTimeout, timeout * 500);
adamc@673 375 requestUri(xhr, uri);
adamc@673 376 }
adamc@673 377
adamc@673 378 orsc = function() {
adamc@667 379 if (xhr.readyState == 4) {
adamc@673 380 window.clearTimeout(tid);
adamc@673 381
adamc@667 382 var isok = false;
adamc@667 383
adamc@667 384 try {
adamc@667 385 if (xhr.status == 200)
adamc@667 386 isok = true;
adamc@667 387 } catch (e) { }
adamc@667 388
adamc@668 389 if (isok) {
adamc@669 390 var lines = xhr.responseText.split("\n");
adamc@669 391 if (lines.length < 2)
adamc@690 392 return; // throw "Empty message from remote server";
adamc@669 393
adamc@669 394 for (var i = 0; i+1 < lines.length; i += 2) {
adamc@670 395 var chn = lines[i];
adamc@670 396 var msg = lines[i+1];
adamc@670 397
adamc@670 398 if (chn < 0)
adamc@670 399 whine("Out-of-bounds channel in message from remote server");
adamc@670 400
adamc@670 401 var ch;
adamc@670 402
adamc@670 403 if (chn >= channels.length || channels[chn] == null) {
adamc@670 404 ch = newChannel();
adamc@670 405 channels[chn] = ch;
adamc@670 406 } else
adamc@670 407 ch = channels[chn];
adamc@670 408
adamc@670 409 var listener = dequeue(ch.listeners);
adamc@670 410 if (listener == null) {
adamc@670 411 enqueue(ch.msgs, msg);
adamc@670 412 } else {
adamc@670 413 listener(msg);
adamc@670 414 }
adamc@669 415 }
adamc@669 416
adamc@673 417 connect();
adamc@668 418 }
adamc@667 419 else {
adamc@679 420 /*try {
adamc@672 421 whine("Error querying remote server for messages! " + xhr.status);
adamc@679 422 } catch (e) { }*/
adamc@667 423 }
adamc@667 424 }
adamc@667 425 };
adamc@667 426
adamc@673 427 onTimeout = function() {
adamc@673 428 xhr.abort();
adamc@673 429 connect();
adamc@673 430 };
adamc@673 431
adamc@673 432 connect();
adamc@667 433 }
adamc@670 434
adamc@670 435 function rv(chn, parse, k) {
adamc@682 436 if (chn == null)
adamc@682 437 return;
adamc@682 438
adamc@670 439 if (chn < 0)
adamc@670 440 whine("Out-of-bounds channel receive");
adamc@670 441
adamc@670 442 var ch;
adamc@670 443
adamc@670 444 if (chn >= channels.length || channels[chn] == null) {
adamc@670 445 ch = newChannel();
adamc@670 446 channels[chn] = ch;
adamc@670 447 } else
adamc@670 448 ch = channels[chn];
adamc@670 449
adamc@670 450 var msg = dequeue(ch.msgs);
adamc@670 451 if (msg == null) {
adamc@670 452 enqueue(ch.listeners, function(msg) { k(parse(msg))(null); });
adamc@670 453 } else {
adamc@670 454 k(parse(msg))(null);
adamc@670 455 }
adamc@670 456 }
adamc@693 457
adamc@693 458
adamc@693 459 // App-specific code
adamc@693 460