annotate lib/js/urweb.js @ 692:09df0c85f306

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