annotate lib/js/urweb.js @ 717:e28637743279

URLs
author Adam Chlipala <adamc@hcoop.net>
date Thu, 09 Apr 2009 16:36:50 -0400
parents a5d8b470d7ca
children 6fc633d990e7
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@603 253
adamc@692 254 // Remote calls
adamc@609 255
adamc@703 256 var client_id = null;
adamc@668 257 var client_pass = 0;
adamc@668 258 var url_prefix = "/";
adamc@673 259 var timeout = 60;
adamc@668 260
adamc@668 261 function getXHR(uri)
adamc@609 262 {
adamc@609 263 try {
adamc@609 264 return new XMLHttpRequest();
adamc@609 265 } catch (e) {
adamc@609 266 try {
adamc@609 267 return new ActiveXObject("Msxml2.XMLHTTP");
adamc@609 268 } catch (e) {
adamc@609 269 try {
adamc@609 270 return new ActiveXObject("Microsoft.XMLHTTP");
adamc@609 271 } catch (e) {
adamc@609 272 throw "Your browser doesn't seem to support AJAX.";
adamc@609 273 }
adamc@609 274 }
adamc@609 275 }
adamc@609 276 }
adamc@609 277
adamc@668 278 function requestUri(xhr, uri) {
adamc@668 279 xhr.open("GET", uri, true);
adamc@668 280
adamc@703 281 if (client_id != null) {
adamc@668 282 xhr.setRequestHeader("UrWeb-Client", client_id.toString());
adamc@668 283 xhr.setRequestHeader("UrWeb-Pass", client_pass.toString());
adamc@668 284 }
adamc@668 285
adamc@668 286 xhr.send(null);
adamc@668 287 }
adamc@668 288
adamc@613 289 function rc(uri, parse, k) {
adamc@703 290 var cls = {v : null};
adamc@703 291 uri = flatten(cls, uri);
adamc@703 292 for (cl = cls.v; cl != null; cl = cl.next)
adamc@703 293 freeClosure(cl.data);
adamc@703 294
adamc@609 295 var xhr = getXHR();
adamc@609 296
adamc@609 297 xhr.onreadystatechange = function() {
adamc@612 298 if (xhr.readyState == 4) {
adamc@612 299 var isok = false;
adamc@612 300
adamc@612 301 try {
adamc@612 302 if (xhr.status == 200)
adamc@612 303 isok = true;
adamc@612 304 } catch (e) { }
adamc@612 305
adamc@612 306 if (isok)
adamc@613 307 k(parse(xhr.responseText));
adamc@649 308 else {
adamc@669 309 whine("Error querying remote server!");
adamc@649 310 }
adamc@612 311 }
adamc@609 312 };
adamc@609 313
adamc@668 314 requestUri(xhr, uri);
adamc@609 315 }
adamc@667 316
adamc@667 317 function path_join(s1, s2) {
adamc@667 318 if (s1.length > 0 && s1[s1.length-1] == '/')
adamc@667 319 return s1 + s2;
adamc@667 320 else
adamc@667 321 return s1 + "/" + s2;
adamc@667 322 }
adamc@667 323
adamc@670 324 var channels = [];
adamc@670 325
adamc@670 326 function newQueue() {
adamc@670 327 return { front : null, back : null };
adamc@670 328 }
adamc@670 329 function enqueue(q, v) {
adamc@670 330 if (q.front == null) {
adamc@670 331 q.front = cons(v, null);
adamc@670 332 q.back = q.front;
adamc@670 333 } else {
adamc@670 334 var node = cons(v, null);
adamc@690 335 q.back.next = node;
adamc@670 336 q.back = node;
adamc@670 337 }
adamc@670 338 }
adamc@670 339 function dequeue(q) {
adamc@670 340 if (q.front == null)
adamc@670 341 return null;
adamc@670 342 else {
adamc@690 343 var r = q.front.data;
adamc@690 344 q.front = q.front.next;
adamc@670 345 if (q.front == null)
adamc@670 346 q.back = null;
adamc@670 347 return r;
adamc@670 348 }
adamc@670 349 }
adamc@670 350
adamc@670 351 function newChannel() {
adamc@670 352 return { msgs : newQueue(), listeners : newQueue() };
adamc@670 353 }
adamc@670 354
adamc@667 355 function listener() {
adamc@668 356 var uri = path_join(url_prefix, ".msgs");
adamc@667 357 var xhr = getXHR();
adamc@673 358 var tid, orsc, onTimeout;
adamc@673 359
adamc@673 360 var connect = function () {
adamc@673 361 xhr.onreadystatechange = orsc;
adamc@673 362 tid = window.setTimeout(onTimeout, timeout * 500);
adamc@673 363 requestUri(xhr, uri);
adamc@673 364 }
adamc@673 365
adamc@673 366 orsc = function() {
adamc@667 367 if (xhr.readyState == 4) {
adamc@673 368 window.clearTimeout(tid);
adamc@673 369
adamc@667 370 var isok = false;
adamc@667 371
adamc@667 372 try {
adamc@667 373 if (xhr.status == 200)
adamc@667 374 isok = true;
adamc@667 375 } catch (e) { }
adamc@667 376
adamc@668 377 if (isok) {
adamc@669 378 var lines = xhr.responseText.split("\n");
adamc@669 379 if (lines.length < 2)
adamc@690 380 return; // throw "Empty message from remote server";
adamc@669 381
adamc@669 382 for (var i = 0; i+1 < lines.length; i += 2) {
adamc@670 383 var chn = lines[i];
adamc@670 384 var msg = lines[i+1];
adamc@670 385
adamc@670 386 if (chn < 0)
adamc@670 387 whine("Out-of-bounds channel in message from remote server");
adamc@670 388
adamc@670 389 var ch;
adamc@670 390
adamc@670 391 if (chn >= channels.length || channels[chn] == null) {
adamc@670 392 ch = newChannel();
adamc@670 393 channels[chn] = ch;
adamc@670 394 } else
adamc@670 395 ch = channels[chn];
adamc@670 396
adamc@670 397 var listener = dequeue(ch.listeners);
adamc@670 398 if (listener == null) {
adamc@670 399 enqueue(ch.msgs, msg);
adamc@670 400 } else {
adamc@670 401 listener(msg);
adamc@670 402 }
adamc@669 403 }
adamc@669 404
adamc@673 405 connect();
adamc@668 406 }
adamc@667 407 else {
adamc@679 408 /*try {
adamc@672 409 whine("Error querying remote server for messages! " + xhr.status);
adamc@679 410 } catch (e) { }*/
adamc@667 411 }
adamc@667 412 }
adamc@667 413 };
adamc@667 414
adamc@673 415 onTimeout = function() {
adamc@673 416 xhr.abort();
adamc@673 417 connect();
adamc@673 418 };
adamc@673 419
adamc@673 420 connect();
adamc@667 421 }
adamc@670 422
adamc@670 423 function rv(chn, parse, k) {
adamc@682 424 if (chn == null)
adamc@682 425 return;
adamc@682 426
adamc@670 427 if (chn < 0)
adamc@670 428 whine("Out-of-bounds channel receive");
adamc@670 429
adamc@670 430 var ch;
adamc@670 431
adamc@670 432 if (chn >= channels.length || channels[chn] == null) {
adamc@670 433 ch = newChannel();
adamc@670 434 channels[chn] = ch;
adamc@670 435 } else
adamc@670 436 ch = channels[chn];
adamc@670 437
adamc@670 438 var msg = dequeue(ch.msgs);
adamc@670 439 if (msg == null) {
adamc@670 440 enqueue(ch.listeners, function(msg) { k(parse(msg))(null); });
adamc@670 441 } else {
adamc@670 442 k(parse(msg))(null);
adamc@670 443 }
adamc@670 444 }
adamc@693 445
adamc@693 446
adamc@693 447 // App-specific code
adamc@693 448