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@1360
|
22 function divInt(x, y) { var n = x / y; return n < 0 ? Math.ceil(n) : Math.floor(n); }
|
adamc@970
|
23 function mod(x, y) { return x % y; }
|
adam@1360
|
24 function modInt(x, y) { 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)
|
adamc@1260
|
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@1487
|
96 // Time
|
adam@1487
|
97
|
adam@1487
|
98 function showTime(tm) {
|
adam@1487
|
99 var newDate = new Date();
|
adam@1487
|
100 newDate.setTime(tm / 1000);
|
adam@1487
|
101 return newDate.toUTCString();
|
adam@1487
|
102 }
|
adam@1487
|
103
|
adam@1487
|
104 function now() {
|
adam@1487
|
105 return (new Date()).getTime() * 1000;
|
adam@1487
|
106 }
|
adam@1487
|
107
|
adam@1514
|
108 function diffInSeconds(tm1, tm2) {
|
adam@1514
|
109 return Math.round((tm2 - tm1) / 1000000);
|
adam@1514
|
110 }
|
adam@1514
|
111
|
adam@1514
|
112 function toSeconds(tm) {
|
adam@1514
|
113 return Math.round(tm / 1000000);
|
adam@1514
|
114 }
|
adam@1514
|
115
|
adam@1518
|
116 function addSeconds(tm, n) {
|
adam@1518
|
117 return tm + n * 1000000;
|
adam@1518
|
118 }
|
adam@1518
|
119
|
adam@1487
|
120
|
adamc@729
|
121 // Error handling
|
adamc@729
|
122
|
adamc@729
|
123 function whine(msg) {
|
adamc@1260
|
124 alert(msg);
|
adamc@1260
|
125 throw msg;
|
adamc@729
|
126 }
|
adamc@729
|
127
|
adamc@810
|
128 function pf(loc) {
|
adamc@1260
|
129 throw ("Pattern match failure (" + loc + ")");
|
adamc@729
|
130 }
|
adamc@729
|
131
|
adamc@798
|
132 function runHandlers(kind, ls, arg) {
|
adamc@1260
|
133 if (ls == null)
|
adamc@1260
|
134 alert(kind + ": " + arg);
|
adamc@1260
|
135 for (; ls; ls = ls.next)
|
adamc@1260
|
136 try {
|
adamc@1260
|
137 exec({c:"a", f:{c:"a", f:ls.data, x:{c:"c", v:arg}}, x:{c:"c", v:null}});
|
adamc@1260
|
138 } catch (v) { }
|
adamc@729
|
139 }
|
adamc@729
|
140
|
adamc@729
|
141 var errorHandlers = null;
|
adamc@729
|
142
|
adamc@729
|
143 function onError(f) {
|
adamc@1260
|
144 errorHandlers = cons(f, errorHandlers);
|
adamc@729
|
145 }
|
adamc@729
|
146
|
adamc@729
|
147 function er(s) {
|
adamc@1260
|
148 runHandlers("Error", errorHandlers, s);
|
adamc@1260
|
149 throw {uw_error: s};
|
adamc@729
|
150 }
|
adamc@729
|
151
|
adamc@729
|
152 var failHandlers = null;
|
adamc@729
|
153
|
adamc@729
|
154 function onFail(f) {
|
adamc@1260
|
155 failHandlers = cons(f, failHandlers);
|
adamc@729
|
156 }
|
adamc@729
|
157
|
adamc@729
|
158 function doExn(v) {
|
adamc@1260
|
159 if (v == null || v.uw_error == null) {
|
adamc@1260
|
160 var s = (v == null ? "null" : v.message ? v.message : v.toString());
|
adamc@1260
|
161 if (v != null && v.fileName && v.lineNumber)
|
adamc@1260
|
162 s += " (" + v.fileName + ":" + v.lineNumber + ")";
|
adamc@1260
|
163 runHandlers("Fail", failHandlers, s);
|
adamc@1260
|
164 }
|
adamc@729
|
165 }
|
adamc@729
|
166
|
adamc@729
|
167 var disconnectHandlers = null;
|
adamc@729
|
168
|
adamc@976
|
169 function flift(f) {
|
kkallio@1484
|
170 return {c: "c", v:{env:cons(f,null), body:{c:"v", n:1}}};
|
adamc@976
|
171 }
|
adamc@976
|
172
|
adamc@729
|
173 function onDisconnect(f) {
|
adamc@1260
|
174 disconnectHandlers = cons(flift(f), disconnectHandlers);
|
adamc@729
|
175 }
|
adamc@729
|
176
|
adamc@729
|
177 function discon() {
|
adamc@1260
|
178 runHandlers("Disconnect", disconnectHandlers, null);
|
adamc@729
|
179 }
|
adamc@729
|
180
|
adamc@729
|
181 var connectHandlers = null;
|
adamc@729
|
182
|
adamc@729
|
183 function onConnectFail(f) {
|
adamc@1260
|
184 connectHandlers = cons(flift(f), connectHandlers);
|
adamc@729
|
185 }
|
adamc@729
|
186
|
adamc@729
|
187 function conn() {
|
adamc@1260
|
188 runHandlers("Connect", connectHandlers, null);
|
adamc@729
|
189 }
|
adamc@729
|
190
|
adamc@729
|
191 var serverHandlers = null;
|
adamc@729
|
192
|
adamc@729
|
193 function onServerError(f) {
|
adamc@1260
|
194 serverHandlers = cons(f, serverHandlers);
|
adamc@729
|
195 }
|
adamc@729
|
196
|
adamc@729
|
197 function servErr(s) {
|
adamc@1260
|
198 window.setTimeout(function () { runHandlers("Server", serverHandlers, s); }, 0);
|
adamc@729
|
199 }
|
adamc@729
|
200
|
adamc@729
|
201
|
adamc@692
|
202 // Embedding closures in XML strings
|
adamc@692
|
203
|
adamc@693
|
204 function cs(f) {
|
adamc@1260
|
205 return {closure: f};
|
adamc@693
|
206 }
|
adamc@693
|
207
|
adamc@693
|
208 function isWeird(v) {
|
adamc@1260
|
209 return v.closure != null || v.cat1 != null;
|
adamc@693
|
210 }
|
adamc@693
|
211
|
adamc@692
|
212 function cat(s1, s2) {
|
adamc@1260
|
213 if (isWeird(s1) || isWeird(s2))
|
adamc@1260
|
214 return {cat1: s1, cat2: s2};
|
adamc@1260
|
215 else
|
adamc@1260
|
216 return s1 + s2;
|
adamc@692
|
217 }
|
adamc@692
|
218
|
adamc@692
|
219 var closures = [];
|
adamc@703
|
220 var freeClosures = null;
|
adamc@692
|
221
|
adamc@692
|
222 function newClosure(f) {
|
adamc@1260
|
223 var n;
|
adamc@1260
|
224 if (freeClosures == null) {
|
adamc@1260
|
225 n = closures.length;
|
adamc@1260
|
226 } else {
|
adamc@1260
|
227 n = freeClosures.data;
|
adamc@1260
|
228 freeClosures = freeClosures.next;
|
adamc@1260
|
229 }
|
adamc@1260
|
230 closures[n] = f;
|
adamc@1260
|
231 return n;
|
adamc@692
|
232 }
|
adamc@692
|
233
|
adamc@703
|
234 function freeClosure(n) {
|
adamc@1260
|
235 closures[n] = null;
|
adamc@1260
|
236 freeClosures = cons(n, freeClosures);
|
adamc@703
|
237 }
|
adamc@703
|
238
|
adamc@692
|
239 function cr(n) {
|
adamc@1260
|
240 return closures[n];
|
adamc@692
|
241 }
|
adamc@692
|
242
|
adamc@703
|
243 function flatten(cls, tr) {
|
adamc@1260
|
244 if (tr.cat1 != null)
|
adamc@1260
|
245 return flatten(cls, tr.cat1) + flatten(cls, tr.cat2);
|
adamc@1260
|
246 else if (tr.closure != null) {
|
adamc@1260
|
247 var cl = newClosure(tr.closure);
|
adamc@1260
|
248 cls.v = cons(cl, cls.v);
|
adamc@1260
|
249 return "cr(" + cl + ")";
|
adamc@1260
|
250 } else
|
adamc@1260
|
251 return tr;
|
adamc@692
|
252 }
|
adamc@692
|
253
|
adamc@728
|
254 function flattenLocal(s) {
|
adamc@1260
|
255 var cls = {v : null};
|
adamc@1260
|
256 var r = flatten(cls, s);
|
adamc@1260
|
257 for (cl = cls.v; cl != null; cl = cl.next)
|
adamc@1260
|
258 freeClosure(cl.data);
|
adamc@1260
|
259 return r;
|
adamc@728
|
260 }
|
adamc@728
|
261
|
adamc@692
|
262
|
adamc@692
|
263
|
adamc@692
|
264 // Dynamic tree management
|
adamc@692
|
265
|
adamc@690
|
266 function populate(node) {
|
adamc@1260
|
267 var s = node.signal;
|
adamc@1260
|
268 var oldSources = node.sources;
|
adamc@1260
|
269 try {
|
adamc@1260
|
270 var sr = execF(s, null);
|
adamc@1260
|
271 var newSources = sr._sources;
|
adamc@690
|
272
|
adamc@1260
|
273 for (var sp = oldSources; sp; sp = sp.next)
|
adamc@1260
|
274 if (!member(sp.data, newSources))
|
adamc@1260
|
275 sp.data.dyns = remove(node, sp.data.dyns);
|
adamc@690
|
276
|
adamc@1260
|
277 for (var sp = newSources; sp; sp = sp.next)
|
adamc@1260
|
278 if (!member(sp.data, oldSources))
|
adamc@1260
|
279 sp.data.dyns = cons(node, sp.data.dyns);
|
adamc@690
|
280
|
adamc@1260
|
281 node.sources = newSources;
|
adamc@1260
|
282 node.recreate(sr._data);
|
adamc@1260
|
283 } catch (v) {
|
adamc@1260
|
284 doExn(v);
|
adamc@1260
|
285 }
|
adamc@579
|
286 }
|
adamc@574
|
287
|
adamc@580
|
288 function sc(v) {
|
adamc@1260
|
289 return {data : v, dyns : null};
|
adamc@580
|
290 }
|
adamc@580
|
291 function sv(s, v) {
|
adam@1491
|
292 if (s.data != v) {
|
adam@1491
|
293 s.data = v;
|
adamc@967
|
294
|
adam@1491
|
295 for (var ls = s.dyns; ls; ls = ls.next)
|
adam@1491
|
296 if (!ls.dead)
|
adam@1491
|
297 populate(ls.data);
|
adam@1491
|
298 }
|
adamc@580
|
299 }
|
adamc@601
|
300 function sg(s) {
|
adamc@1260
|
301 return s.data;
|
adamc@601
|
302 }
|
adamc@579
|
303
|
adamc@580
|
304 function ss(s) {
|
adamc@1260
|
305 return {env:cons(s, null), body:{c:"r", l:
|
adamc@1260
|
306 cons({n:"sources", v:{c:"c", v:cons(s, null)}},
|
adamc@1260
|
307 cons({n:"data", v:{c:"f", f:sg, a:cons({c:"v", n:1}, null)}}, null))}};
|
adamc@580
|
308 }
|
adamc@580
|
309 function sr(v) {
|
adamc@1260
|
310 return {env:null, body:{c:"c", v:{_sources : null, _data : v}}};
|
adamc@580
|
311 }
|
adamc@580
|
312 function sb(x,y) {
|
adamc@1260
|
313 return {env:cons(y,cons(x,null)),
|
adamc@1260
|
314 body:{c:"=",
|
adamc@1260
|
315 e1:{c:"a", f:{c:"v", n:2}, x:{c:"c", v:null}},
|
adamc@1260
|
316 e2:{c:"=",
|
adamc@1260
|
317 e1:{c:"a",
|
adamc@1260
|
318 f:{c:"a", f:{c:"v", n:2}, x:{c:".", r:{c:"v", n:0}, f:"data"}},
|
adamc@1260
|
319 x:{c:"c", v:null}},
|
adamc@1260
|
320 e2:{c:"r", l:cons(
|
adamc@1260
|
321 {n:"sources", v:{c:"f", f:union, a:cons({c:".", r:{c:"v", n:1}, f:"sources"},
|
adamc@1260
|
322 cons({c:".", r:{c:"v", n:0}, f:"sources"}, null))}},
|
adamc@1260
|
323 cons({n:"data", v:{c:".", r:{c:"v", n:0}, f:"data"}}, null))}}}};
|
adamc@580
|
324 }
|
adamc@841
|
325 function scur(s) {
|
adamc@1260
|
326 return execF(s, null)._data;
|
adamc@841
|
327 }
|
adamc@571
|
328
|
adamc@604
|
329 function lastParent() {
|
adamc@1260
|
330 var pos = document.body;
|
adamc@604
|
331
|
adamc@1260
|
332 while (pos.lastChild && pos.lastChild.nodeType == 1)
|
adamc@1260
|
333 pos = pos.lastChild;
|
adamc@600
|
334
|
adamc@1260
|
335 pos = pos.parentNode;
|
adamc@1039
|
336
|
adamc@1260
|
337 return pos;
|
adamc@600
|
338 }
|
adamc@600
|
339
|
adamc@898
|
340 function parent() {
|
adamc@1260
|
341 return thisScript ? thisScript.parentNode : lastParent();
|
adamc@898
|
342 }
|
adamc@898
|
343
|
adamc@604
|
344 function addNode(node) {
|
adamc@1260
|
345 if (thisScript)
|
adamc@1260
|
346 thisScript.parentNode.replaceChild(node, thisScript);
|
adamc@1260
|
347 else
|
adamc@1260
|
348 lastParent().appendChild(node);
|
adamc@603
|
349 }
|
adamc@603
|
350
|
adamc@690
|
351 var thisScript = null;
|
adamc@690
|
352
|
adamc@604
|
353 function runScripts(node) {
|
adamc@1260
|
354 if (node.getElementsByTagName) {
|
adamc@1260
|
355 var savedScript = thisScript;
|
adamc@603
|
356
|
adamc@1260
|
357 var scripts = node.getElementsByTagName("script"), scriptsCopy = [];
|
adamc@1260
|
358 var len = scripts.length;
|
adamc@1260
|
359 for (var i = 0; i < len; ++i)
|
adamc@1260
|
360 scriptsCopy[i] = scripts[i];
|
adamc@1260
|
361 for (var i = 0; i < len; ++i) {
|
adamc@1260
|
362 thisScript = scriptsCopy[i];
|
adamc@1039
|
363
|
adamc@1260
|
364 try {
|
adamc@1260
|
365 eval(thisScript.text);
|
adamc@1260
|
366 } catch (v) {
|
adamc@1260
|
367 doExn(v);
|
adamc@1260
|
368 }
|
adamc@1260
|
369 if (thisScript.parentNode)
|
adamc@1260
|
370 thisScript.parentNode.removeChild(thisScript);
|
adamc@1260
|
371 }
|
adamc@1260
|
372
|
adamc@1260
|
373 thisScript = savedScript;
|
adamc@729
|
374 }
|
adamc@603
|
375 }
|
adamc@603
|
376
|
adamc@603
|
377
|
adamc@692
|
378 // Dynamic tree entry points
|
adamc@692
|
379
|
adamc@967
|
380 function killScript(scr) {
|
adamc@1260
|
381 scr.dead = true;
|
adamc@1260
|
382 for (var ls = scr.sources; ls; ls = ls.next)
|
adamc@1260
|
383 ls.data.dyns = remove(scr, ls.data.dyns);
|
adamc@1260
|
384 for (var ls = scr.closures; ls; ls = ls.next)
|
adamc@1260
|
385 freeClosure(ls.data);
|
adamc@967
|
386 }
|
adamc@967
|
387
|
adam@1538
|
388 // Sometimes we wind up with tables that contain <script>s outside the single <tbody>.
|
adam@1538
|
389 // To avoid dealing with that case, we normalize by moving <script>s into <tbody>.
|
adam@1538
|
390 function normalizeTable(table) {
|
adam@1538
|
391 var orig = table;
|
adam@1538
|
392
|
adam@1538
|
393 var script, next;
|
adam@1538
|
394
|
adam@1538
|
395 while (table.tagName != "TABLE")
|
adam@1538
|
396 table = table.parentNode;
|
adam@1538
|
397
|
adam@1538
|
398 for (var tbody = table.firstChild; tbody; tbody = tbody.nextSibling) {
|
adam@1538
|
399 if (tbody.tagName == "TBODY") {
|
adam@1538
|
400 for (script = table.firstChild; script && script != tbody; script = next) {
|
adam@1538
|
401 next = script.nextSibling;
|
adam@1538
|
402
|
adam@1538
|
403 tbody.insertBefore(script, tbody.firstChild);
|
adam@1538
|
404 }
|
adam@1538
|
405
|
adam@1538
|
406 return;
|
adam@1538
|
407 }
|
adam@1538
|
408 }
|
adam@1538
|
409
|
adam@1538
|
410 var tbody = document.createElement("tbody");
|
adam@1538
|
411 for (script = table.firstChild; script; script = next) {
|
adam@1538
|
412 next = script.nextSibling;
|
adam@1538
|
413
|
adam@1538
|
414 tbody.insertBefore(script, tbody.firstChild);
|
adam@1538
|
415 }
|
adam@1538
|
416 table.appendChild(tbody);
|
adam@1538
|
417 }
|
adam@1538
|
418
|
adamc@901
|
419 function dyn(pnode, s) {
|
adamc@1260
|
420 var x = document.createElement("script");
|
adamc@1260
|
421 x.dead = false;
|
adamc@1260
|
422 x.signal = s;
|
adamc@1260
|
423 x.sources = null;
|
adamc@1260
|
424 x.closures = null;
|
adamc@899
|
425
|
adamc@1260
|
426 var firstChild = null;
|
adamc@899
|
427
|
adamc@1260
|
428 x.recreate = function(v) {
|
adamc@1260
|
429 for (var ls = x.closures; ls; ls = ls.next)
|
adamc@1260
|
430 freeClosure(ls.data);
|
adamc@945
|
431
|
adamc@946
|
432 var next;
|
adamc@1260
|
433 for (var child = firstChild; child && child != x; child = next) {
|
adamc@1260
|
434 next = child.nextSibling;
|
adamc@1260
|
435
|
adamc@1260
|
436 killScript(child);
|
adamc@1260
|
437 if (child.getElementsByTagName) {
|
adamc@1260
|
438 var arr = child.getElementsByTagName("script");
|
adamc@1260
|
439 for (var i = 0; i < arr.length; ++i)
|
adamc@1260
|
440 killScript(arr[i]);
|
adamc@1260
|
441 }
|
adamc@1260
|
442
|
adamc@1260
|
443 if (child.parentNode)
|
adamc@1260
|
444 child.parentNode.removeChild(child);
|
adamc@946
|
445 }
|
adamc@903
|
446
|
adamc@1260
|
447 var cls = {v : null};
|
adamc@1260
|
448 var html = flatten(cls, v);
|
adamc@1260
|
449 if (pnode != "table" && pnode != "tr")
|
adamc@1260
|
450 html = dynPrefix + html;
|
adamc@1260
|
451 x.closures = cls.v;
|
adamc@946
|
452
|
adamc@1260
|
453 if (pnode == "table") {
|
adam@1538
|
454 normalizeTable(x.parentNode);
|
adam@1538
|
455
|
adamc@1260
|
456 var dummy = document.createElement("body");
|
adamc@1260
|
457 dummy.innerHTML = "<table>" + html + "</table>";
|
adamc@1260
|
458 runScripts(dummy);
|
adamc@1260
|
459 var table = x.parentNode;
|
adamc@1260
|
460
|
adamc@1260
|
461 if (table) {
|
adamc@1260
|
462 firstChild = null;
|
adamc@1260
|
463 var tbody;
|
adamc@1260
|
464
|
adamc@1260
|
465 var arr = dummy.getElementsByTagName("tbody");
|
adamc@1260
|
466
|
adamc@1260
|
467 var tbody;
|
adamc@1260
|
468 if (arr.length > 0 && arr[0].parentNode == dummy.firstChild) {
|
adamc@1260
|
469 tbody = arr[0];
|
adamc@1260
|
470 var next;
|
adamc@1260
|
471 for (var node = dummy.firstChild.firstChild; node; node = next) {
|
adamc@1260
|
472 next = node.nextSibling;
|
adamc@1260
|
473
|
adamc@1260
|
474 if (node.tagName != "TBODY")
|
adamc@1260
|
475 tbody.appendChild(node);
|
adamc@1260
|
476 }
|
adamc@1260
|
477 } else
|
adamc@1260
|
478 tbody = dummy.firstChild;
|
adamc@1260
|
479
|
adamc@1260
|
480 var next;
|
adamc@1260
|
481 firstChild = document.createElement("script");
|
adamc@1260
|
482 table.insertBefore(firstChild, x);
|
adamc@1260
|
483 for (var node = tbody.firstChild; node; node = next) {
|
adamc@1260
|
484 next = node.nextSibling;
|
adamc@1260
|
485 table.insertBefore(node, x);
|
adamc@1260
|
486 }
|
adamc@1260
|
487 }
|
adamc@1260
|
488 } else if (pnode == "tr") {
|
adamc@1260
|
489 var dummy = document.createElement("body");
|
adamc@1260
|
490 dummy.innerHTML = "<table><tr>" + html + "</tr></table>";
|
adamc@1260
|
491 runScripts(dummy);
|
adamc@1260
|
492 var table = x.parentNode;
|
adamc@1260
|
493
|
adamc@1260
|
494 if (table) {
|
adamc@1260
|
495 var arr = dummy.getElementsByTagName("tr");
|
adamc@1260
|
496 firstChild = null;
|
adamc@1260
|
497 var tr;
|
adamc@1260
|
498 if (arr.length > 0 && table != null)
|
adamc@1260
|
499 tr = arr[0];
|
adamc@1260
|
500 else
|
adamc@1260
|
501 tr = dummy.firstChild;
|
adamc@1260
|
502
|
adamc@1260
|
503 var next;
|
adamc@1260
|
504 firstChild = document.createElement("script");
|
adamc@1260
|
505 table.insertBefore(firstChild, x);
|
adamc@1260
|
506 for (var node = tr.firstChild; node; node = next) {
|
adamc@1260
|
507 next = node.nextSibling;
|
adamc@1260
|
508 table.insertBefore(node, x);
|
adamc@1260
|
509 }
|
adamc@1260
|
510 }
|
adamc@1260
|
511 } else {
|
adamc@1260
|
512 firstChild = document.createElement("span");
|
adamc@1260
|
513 firstChild.innerHTML = html;
|
adamc@1260
|
514 runScripts(firstChild);
|
adamc@1260
|
515 if (x.parentNode)
|
adamc@1260
|
516 x.parentNode.insertBefore(firstChild, x);
|
adamc@903
|
517 }
|
adamc@1260
|
518 };
|
adamc@899
|
519
|
adamc@1260
|
520 addNode(x);
|
adamc@1260
|
521 populate(x);
|
adamc@571
|
522 }
|
adamc@582
|
523
|
adam@1463
|
524 function input(x, s, recreate, type, name) {
|
adam@1463
|
525 if (name) x.name = name;
|
adamc@1260
|
526 if (type) x.type = type;
|
adamc@1260
|
527 x.dead = false;
|
adamc@1260
|
528 x.signal = ss(s);
|
adamc@1260
|
529 x.sources = null;
|
adamc@1260
|
530 x.recreate = recreate(x);
|
adamc@1260
|
531 addNode(x);
|
adamc@1260
|
532 populate(x);
|
adamc@816
|
533
|
adamc@1260
|
534 return x;
|
adamc@816
|
535 }
|
adamc@816
|
536
|
adam@1463
|
537 function inp(s, name) {
|
adamc@1260
|
538 var x = input(document.createElement("input"), s,
|
adam@1463
|
539 function(x) { return function(v) { if (x.value != v) x.value = v; }; }, null, name);
|
adamc@1260
|
540 x.value = s.data;
|
adam@1491
|
541 x.onkeyup = x.oninput = x.onchange = x.onpropertychange = function() { sv(s, x.value) };
|
adamc@816
|
542
|
adamc@1260
|
543 return x;
|
adamc@816
|
544 }
|
adamc@816
|
545
|
adam@1479
|
546 function selectValue(x) {
|
adam@1479
|
547 if (x.options.length == 0)
|
adam@1479
|
548 return "";
|
adam@1479
|
549 else
|
adam@1479
|
550 return x.options[x.selectedIndex].value;
|
adam@1479
|
551 }
|
adam@1479
|
552
|
adam@1479
|
553 function setSelectValue(x, v) {
|
adam@1479
|
554 for (var i = 0; i < x.options.length; ++i) {
|
adam@1479
|
555 if(x.options[i].value == v) {
|
adam@1479
|
556 x.selectedIndex = i;
|
adam@1479
|
557 return;
|
adam@1479
|
558 }
|
adam@1479
|
559 }
|
adam@1479
|
560 }
|
adam@1479
|
561
|
adamc@816
|
562 function sel(s, content) {
|
adamc@1260
|
563 var dummy = document.createElement("span");
|
adamc@1260
|
564 dummy.innerHTML = "<select>" + content + "</select>";
|
adam@1479
|
565 var x = input(dummy.firstChild, s, function(x) { return function(v) { if (selectValue(x) != v) setSelectValue(x, v); }; });
|
adam@1479
|
566
|
adam@1479
|
567 for (var i = 0; i < x.options.length; ++i) {
|
adam@1479
|
568 if (x.options[i].value == "")
|
adam@1479
|
569 x.options[i].value = x.options[i].text;
|
adam@1479
|
570 else
|
adam@1479
|
571 x.options[i].value = x.options[i].value.substring(1);
|
adam@1479
|
572 }
|
adam@1479
|
573
|
adam@1479
|
574 setSelectValue(x, s.data);
|
adam@1479
|
575 if (selectValue(x) != s.data)
|
adam@1479
|
576 sv(s, selectValue(x));
|
adam@1479
|
577 x.onchange = function() { sv(s, selectValue(x)) };
|
adamc@797
|
578
|
adamc@1260
|
579 return x;
|
adamc@598
|
580 }
|
adamc@598
|
581
|
adamc@817
|
582 function chk(s) {
|
adamc@1260
|
583 var x = input(document.createElement("input"), s,
|
adamc@1260
|
584 function(x) { return function(v) { if (x.checked != v) x.checked = v; }; }, "checkbox");
|
adamc@1260
|
585 x.defaultChecked = x.checked = s.data;
|
adamc@1260
|
586 x.onclick = function() { sv(s, x.checked) };
|
adamc@817
|
587
|
adamc@1260
|
588 return x;
|
adamc@817
|
589 }
|
adamc@817
|
590
|
adamc@1099
|
591 function tbx(s) {
|
adamc@1260
|
592 var x = input(document.createElement("textarea"), s,
|
adamc@1260
|
593 function(x) { return function(v) { if (x.innerHTML != v) x.innerHTML = v; }; });
|
adamc@1260
|
594 x.innerHTML = s.data;
|
adam@1491
|
595 x.onkeyup = x.oninput = x.onchange = x.onpropertychange = function() { sv(s, x.value) };
|
adamc@1099
|
596
|
adamc@1260
|
597 return x;
|
adamc@1099
|
598 }
|
adamc@1099
|
599
|
adamc@800
|
600 function addOnChange(x, f) {
|
adamc@1260
|
601 var old = x.onchange;
|
adam@1290
|
602 if (old == null)
|
adam@1290
|
603 x.onchange = f;
|
adam@1290
|
604 else
|
adam@1290
|
605 x.onchange = function() { old(); f(); };
|
adam@1290
|
606 }
|
adam@1290
|
607
|
adam@1290
|
608 function addOnKeyUp(x, f) {
|
adam@1290
|
609 var old = x.onkeyup;
|
adam@1290
|
610 if (old == null)
|
adam@1290
|
611 x.onkeyup = f;
|
adam@1290
|
612 else
|
adam@1290
|
613 x.onkeyup = function(x) { old(x); f(x); };
|
adamc@800
|
614 }
|
adamc@800
|
615
|
adamc@692
|
616
|
adamc@692
|
617 // Basic string operations
|
adamc@692
|
618
|
adamc@597
|
619 function eh(x) {
|
adamc@1260
|
620 if (x == null)
|
adamc@1260
|
621 return "NULL";
|
adamc@1260
|
622 else
|
adamc@1260
|
623 return x.split("&").join("&").split("<").join("<").split(">").join(">");
|
adamc@597
|
624 }
|
adamc@597
|
625
|
adamc@582
|
626 function ts(x) { return x.toString() }
|
adamc@586
|
627 function bs(b) { return (b ? "True" : "False") }
|
adamc@586
|
628
|
adamc@1057
|
629 function id(x) { return x; }
|
adamc@1038
|
630 function sub(s, i) { return s.charAt(i); }
|
adamc@828
|
631 function suf(s, i) { return s.substring(i); }
|
adamc@828
|
632 function slen(s) { return s.length; }
|
adamc@829
|
633 function sidx(s, ch) {
|
adamc@1260
|
634 var r = s.indexOf(ch);
|
adamc@1260
|
635 if (r == -1)
|
adamc@1260
|
636 return null;
|
adamc@1260
|
637 else
|
adamc@1260
|
638 return r;
|
adamc@829
|
639 }
|
adamc@831
|
640 function sspn(s, chs) {
|
adamc@1260
|
641 for (var i = 0; i < s.length; ++i)
|
adamc@1260
|
642 if (chs.indexOf(s.charAt(i)) != -1)
|
adamc@1260
|
643 return i;
|
adamc@831
|
644
|
adamc@1260
|
645 return null;
|
adamc@831
|
646 }
|
adamc@829
|
647 function schr(s, ch) {
|
adamc@1260
|
648 var r = s.indexOf(ch);
|
adamc@1260
|
649 if (r == -1)
|
adamc@1260
|
650 return null;
|
adamc@1260
|
651 else
|
adamc@1260
|
652 return s.substring(r);
|
adamc@829
|
653 }
|
adamc@829
|
654 function ssub(s, start, len) {
|
adamc@1260
|
655 return s.substring(start, start+len);
|
adamc@829
|
656 }
|
adamc@821
|
657
|
adamc@649
|
658 function pi(s) {
|
adamc@1260
|
659 var r = parseInt(s);
|
adamc@1260
|
660 if (r.toString() == s)
|
adamc@1260
|
661 return r;
|
adamc@1260
|
662 else
|
adamc@1260
|
663 er("Can't parse int: " + s);
|
adamc@649
|
664 }
|
adamc@649
|
665
|
adamc@649
|
666 function pfl(s) {
|
adamc@1260
|
667 var r = parseFloat(s);
|
adamc@1260
|
668 if (r.toString() == s)
|
adamc@1260
|
669 return r;
|
adamc@1260
|
670 else
|
adamc@1260
|
671 er("Can't parse float: " + s);
|
adamc@649
|
672 }
|
adamc@649
|
673
|
adamc@838
|
674 function pio(s) {
|
adamc@1260
|
675 var r = parseInt(s);
|
adamc@1260
|
676 if (r.toString() == s)
|
adamc@1260
|
677 return r;
|
adamc@1260
|
678 else
|
adamc@1260
|
679 return null;
|
adamc@838
|
680 }
|
adamc@838
|
681
|
adamc@838
|
682 function pflo(s) {
|
adamc@1260
|
683 var r = parseFloat(s);
|
adamc@1260
|
684 if (r.toString() == s)
|
adamc@1260
|
685 return r;
|
adamc@1260
|
686 else
|
adamc@1260
|
687 return null;
|
adamc@838
|
688 }
|
adamc@838
|
689
|
adamc@692
|
690 function uf(s) {
|
adamc@1260
|
691 if (s.length == 0)
|
adamc@1260
|
692 return "_";
|
adamc@1260
|
693 s = s.replace(new RegExp ("\\.", "g"), ".2E");
|
adamc@1260
|
694 return (s.charAt(0) == '_' ? "_" : "") + encodeURIComponent(s).replace(new RegExp ("%", "g"), ".");
|
adamc@691
|
695 }
|
adamc@691
|
696
|
adamc@692
|
697 function uu(s) {
|
adamc@1260
|
698 if (s.length > 0 && s.charAt(0) == '_') {
|
adamc@1260
|
699 s = s.substring(1);
|
adamc@1260
|
700 } else if (s.length >= 3 && (s.charAt(0) == '%' || s.charAt(0) == '.')
|
adamc@1260
|
701 && s.charAt(1) == '5' && (s.charAt(2) == 'f' || s.charAt(2) == 'F'))
|
adamc@1260
|
702 s = s.substring(3);
|
adamc@1260
|
703 s = s.replace(new RegExp ("\\+", "g"), " ");
|
adamc@1260
|
704 s = s.replace(new RegExp ("\\.", "g"), "%");
|
adamc@1260
|
705 return decodeURIComponent(s);
|
adamc@905
|
706 }
|
adamc@905
|
707
|
adamc@1108
|
708 function atr(s) {
|
adamc@1260
|
709 return s.replace(new RegExp ("\"", "g"), """).replace(new RegExp ("&", "g"), "&")
|
adamc@1260
|
710 }
|
adamc@1108
|
711
|
adamc@912
|
712 function ub(b) {
|
adamc@1260
|
713 return b ? "1" : "0";
|
adamc@912
|
714 }
|
adamc@912
|
715
|
adamc@905
|
716 function uul(getToken, getData) {
|
adamc@1260
|
717 var tok = getToken();
|
adamc@1260
|
718 if (tok == "Nil") {
|
adamc@1260
|
719 return null;
|
adamc@1260
|
720 } else if (tok == "Cons") {
|
adamc@1260
|
721 var d = getData();
|
adamc@1260
|
722 var l = uul(getToken, getData);
|
adamc@1260
|
723 return {_1:d, _2:l};
|
adamc@1260
|
724 } else
|
adamc@1260
|
725 whine("Can't unmarshal list (" + tok + ")");
|
adamc@692
|
726 }
|
adamc@692
|
727
|
adamc@961
|
728 function strcmp(str1, str2) {
|
adamc@1260
|
729 return ((str1 == str2) ? 0 : ((str1 > str2) ? 1 : -1));
|
adamc@961
|
730 }
|
adamc@692
|
731
|
adamc@603
|
732
|
adamc@692
|
733 // Remote calls
|
adamc@609
|
734
|
adamc@703
|
735 var client_id = null;
|
adamc@668
|
736 var client_pass = 0;
|
adamc@668
|
737 var url_prefix = "/";
|
adamc@673
|
738 var timeout = 60;
|
adamc@668
|
739
|
adamc@668
|
740 function getXHR(uri)
|
adamc@609
|
741 {
|
adamc@609
|
742 try {
|
adamc@1260
|
743 return new XMLHttpRequest();
|
adamc@609
|
744 } catch (e) {
|
adamc@1260
|
745 try {
|
adamc@1260
|
746 return new ActiveXObject("Msxml2.XMLHTTP");
|
adamc@1260
|
747 } catch (e) {
|
adamc@1260
|
748 try {
|
adamc@1260
|
749 return new ActiveXObject("Microsoft.XMLHTTP");
|
adamc@1260
|
750 } catch (e) {
|
adamc@1260
|
751 whine("Your browser doesn't seem to support AJAX.");
|
adamc@1260
|
752 }
|
adamc@1260
|
753 }
|
adamc@609
|
754 }
|
adamc@609
|
755 }
|
adamc@609
|
756
|
adamc@736
|
757 var sig = null;
|
adamc@736
|
758
|
adamc@1042
|
759 var unloading = false, inFlight = null;
|
adamc@1042
|
760
|
adamc@1042
|
761 function unload() {
|
adamc@1260
|
762 unloading = true;
|
adamc@1042
|
763
|
adamc@1260
|
764 for (; inFlight; inFlight = inFlight.next) {
|
adamc@1260
|
765 inFlight.data.abort();
|
adamc@1260
|
766 }
|
adamc@1042
|
767 }
|
adamc@1042
|
768
|
adamc@736
|
769 function requestUri(xhr, uri, needsSig) {
|
adamc@1260
|
770 if (unloading)
|
adamc@1260
|
771 return;
|
adamc@1042
|
772
|
adamc@1260
|
773 xhr.open("POST", uri, true);
|
adamc@1260
|
774 xhr.setRequestHeader("Content-type", "text/plain");
|
adam@1549
|
775 try {
|
adam@1549
|
776 xhr.setRequestHeader("Content-length", "0");
|
adam@1549
|
777 xhr.setRequestHeader("Connection", "close");
|
adam@1549
|
778 } catch (e) { }
|
adamc@668
|
779
|
adamc@1260
|
780 if (client_id != null) {
|
adamc@1260
|
781 xhr.setRequestHeader("UrWeb-Client", client_id.toString());
|
adamc@1260
|
782 xhr.setRequestHeader("UrWeb-Pass", client_pass.toString());
|
adamc@1260
|
783 }
|
adamc@668
|
784
|
adamc@1260
|
785 if (needsSig) {
|
adamc@1260
|
786 if (sig == null)
|
adamc@1260
|
787 whine("Missing cookie signature!");
|
adamc@736
|
788
|
adamc@1260
|
789 xhr.setRequestHeader("UrWeb-Sig", sig);
|
adamc@1260
|
790 }
|
adamc@736
|
791
|
adamc@1260
|
792 inFlight = cons(xhr, inFlight);
|
adamc@1260
|
793 xhr.send(null);
|
adamc@668
|
794 }
|
adamc@668
|
795
|
adamc@1042
|
796 function xhrFinished(xhr) {
|
adamc@1260
|
797 xhr.abort();
|
adamc@1260
|
798 inFlight = remove(xhr, inFlight);
|
adamc@1042
|
799 }
|
adamc@1042
|
800
|
adamc@1111
|
801 function unurlify(parse, s) {
|
adamc@1260
|
802 return parse(s);
|
adamc@1111
|
803 }
|
adamc@1111
|
804
|
adam@1385
|
805 function redirect(s) {
|
adam@1385
|
806 window.location = s;
|
adam@1385
|
807 }
|
adam@1385
|
808
|
adamc@978
|
809 function rc(prefix, uri, parse, k, needsSig) {
|
adamc@1260
|
810 uri = cat(prefix, uri);
|
adamc@1260
|
811 uri = flattenLocal(uri);
|
adamc@1260
|
812 var xhr = getXHR();
|
adamc@609
|
813
|
adamc@1260
|
814 xhr.onreadystatechange = function() {
|
adamc@1260
|
815 if (xhr.readyState == 4) {
|
adamc@1260
|
816 var isok = false;
|
adamc@612
|
817
|
adamc@1260
|
818 try {
|
adamc@1260
|
819 if (xhr.status == 200)
|
adamc@1260
|
820 isok = true;
|
adamc@1260
|
821 } catch (e) { }
|
adamc@612
|
822
|
adamc@1260
|
823 if (isok) {
|
adam@1446
|
824 var lines = xhr.responseText.split("\n");
|
adam@1446
|
825 if (lines.length != 2)
|
adam@1446
|
826 whine("Bad RPC response lines");
|
adam@1446
|
827
|
adam@1446
|
828 eval(lines[0]);
|
adam@1446
|
829
|
adamc@1260
|
830 try {
|
adam@1446
|
831 k(parse(lines[1]));
|
adamc@1260
|
832 } catch (v) {
|
adamc@1260
|
833 doExn(v);
|
adamc@1260
|
834 }
|
adamc@1260
|
835 } else {
|
adamc@1260
|
836 conn();
|
adamc@1260
|
837 }
|
adamc@1260
|
838
|
adamc@1260
|
839 xhrFinished(xhr);
|
adamc@729
|
840 }
|
adamc@1260
|
841 };
|
adamc@1042
|
842
|
adamc@1260
|
843 requestUri(xhr, uri, needsSig);
|
adamc@609
|
844 }
|
adamc@667
|
845
|
adamc@667
|
846 function path_join(s1, s2) {
|
adamc@1260
|
847 if (s1.length > 0 && s1.charAt(s1.length-1) == '/')
|
adamc@1260
|
848 return s1 + s2;
|
adamc@1260
|
849 else
|
adamc@1260
|
850 return s1 + "/" + s2;
|
adamc@667
|
851 }
|
adamc@667
|
852
|
adamc@670
|
853 var channels = [];
|
adamc@670
|
854
|
adamc@670
|
855 function newQueue() {
|
adamc@1260
|
856 return { front : null, back : null };
|
adamc@670
|
857 }
|
adamc@670
|
858 function enqueue(q, v) {
|
adamc@1260
|
859 if (q.front == null) {
|
adamc@1260
|
860 q.front = cons(v, null);
|
adamc@1260
|
861 q.back = q.front;
|
adamc@1260
|
862 } else {
|
adamc@1260
|
863 var node = cons(v, null);
|
adamc@1260
|
864 q.back.next = node;
|
adamc@1260
|
865 q.back = node;
|
adamc@1260
|
866 }
|
adamc@670
|
867 }
|
adamc@670
|
868 function dequeue(q) {
|
adamc@670
|
869 if (q.front == null)
|
adamc@1260
|
870 return null;
|
adamc@1260
|
871 else {
|
adamc@1260
|
872 var r = q.front.data;
|
adamc@1260
|
873 q.front = q.front.next;
|
adamc@1260
|
874 if (q.front == null)
|
adamc@1260
|
875 q.back = null;
|
adamc@1260
|
876 return r;
|
adamc@1260
|
877 }
|
adamc@670
|
878 }
|
adamc@670
|
879
|
adamc@670
|
880 function newChannel() {
|
adamc@1260
|
881 return { msgs : newQueue(), listeners : newQueue() };
|
adamc@670
|
882 }
|
adamc@670
|
883
|
adamc@667
|
884 function listener() {
|
adamc@1260
|
885 var uri = path_join(url_prefix, ".msgs");
|
adamc@1260
|
886 var xhr = getXHR();
|
adamc@1260
|
887 var tid, orsc, onTimeout;
|
adamc@673
|
888
|
adamc@1260
|
889 var connect = function () {
|
adamc@1260
|
890 xhr.onreadystatechange = orsc;
|
adamc@1260
|
891 tid = window.setTimeout(onTimeout, timeout * 500);
|
adamc@1260
|
892 requestUri(xhr, uri, false);
|
adamc@1260
|
893 }
|
adamc@673
|
894
|
adamc@1260
|
895 orsc = function() {
|
adamc@1260
|
896 if (xhr.readyState == 4) {
|
adamc@1260
|
897 window.clearTimeout(tid);
|
adamc@673
|
898
|
adamc@1260
|
899 var isok = false;
|
adamc@667
|
900
|
adamc@1260
|
901 try {
|
adamc@1260
|
902 if (xhr.status == 200)
|
adamc@1260
|
903 isok = true;
|
adamc@1260
|
904 } catch (e) { }
|
adamc@667
|
905
|
adamc@1260
|
906 if (isok) {
|
adam@1447
|
907 var text = xhr.responseText;
|
adamc@1260
|
908 if (text == "")
|
adamc@1260
|
909 return;
|
adamc@1260
|
910 var lines = text.split("\n");
|
adamc@889
|
911
|
adamc@1260
|
912 if (lines.length < 2) {
|
adamc@1260
|
913 discon();
|
adamc@1260
|
914 return;
|
adamc@1260
|
915 }
|
adamc@1260
|
916
|
adam@1447
|
917 var messageReader = function(i) {
|
adam@1447
|
918 if (i+1 >= lines.length) {
|
adam@1447
|
919 xhrFinished(xhr);
|
adam@1447
|
920 connect();
|
adam@1447
|
921 }
|
adam@1447
|
922 else {
|
adam@1447
|
923 var chn = lines[i];
|
adam@1447
|
924 var msg = lines[i+1];
|
adamc@1260
|
925
|
adam@1447
|
926 if (chn == "E") {
|
adam@1447
|
927 eval(msg);
|
adam@1447
|
928 window.setTimeout(function() { messageReader(i+2); }, 0);
|
adam@1447
|
929 } else {
|
adam@1447
|
930 if (chn < 0)
|
adam@1447
|
931 whine("Out-of-bounds channel in message from remote server");
|
adamc@1260
|
932
|
adam@1447
|
933 var ch;
|
adamc@1260
|
934
|
adam@1447
|
935 if (chn >= channels.length || channels[chn] == null) {
|
adam@1447
|
936 ch = newChannel();
|
adam@1447
|
937 channels[chn] = ch;
|
adam@1447
|
938 } else
|
adam@1447
|
939 ch = channels[chn];
|
adamc@1260
|
940
|
adam@1447
|
941 var listener = dequeue(ch.listeners);
|
adam@1447
|
942 if (listener == null) {
|
adam@1447
|
943 enqueue(ch.msgs, msg);
|
adam@1447
|
944 } else {
|
adam@1447
|
945 try {
|
adam@1447
|
946 listener(msg);
|
adam@1447
|
947 } catch (v) {
|
adam@1447
|
948 doExn(v);
|
adam@1447
|
949 }
|
adam@1447
|
950 }
|
adam@1447
|
951
|
adam@1447
|
952 messageReader(i+2);
|
adamc@1260
|
953 }
|
adamc@1260
|
954 }
|
adamc@1260
|
955 }
|
adamc@1260
|
956
|
adam@1447
|
957 messageReader(0);
|
adamc@1260
|
958 }
|
adamc@1260
|
959 else {
|
adamc@1260
|
960 try {
|
adamc@1260
|
961 if (xhr.status != 0)
|
adamc@1260
|
962 servErr("Error querying remote server for messages: " + xhr.status);
|
adamc@1260
|
963 } catch (e) { }
|
adamc@1260
|
964 }
|
adamc@729
|
965 }
|
adamc@1260
|
966 };
|
adamc@669
|
967
|
adamc@1260
|
968 onTimeout = function() {
|
adamc@1260
|
969 xhrFinished(xhr);
|
adamc@1260
|
970 connect();
|
adamc@1260
|
971 };
|
adamc@670
|
972
|
adamc@673
|
973 connect();
|
adamc@667
|
974 }
|
adamc@670
|
975
|
adamc@670
|
976 function rv(chn, parse, k) {
|
adamc@1260
|
977 if (chn == null)
|
adamc@1260
|
978 return;
|
adamc@682
|
979
|
adamc@1260
|
980 if (chn < 0)
|
adamc@1260
|
981 whine("Out-of-bounds channel receive");
|
adamc@670
|
982
|
adamc@1260
|
983 var ch;
|
adamc@670
|
984
|
adamc@1260
|
985 if (chn >= channels.length || channels[chn] == null) {
|
adamc@1260
|
986 ch = newChannel();
|
adamc@1260
|
987 channels[chn] = ch;
|
adamc@1260
|
988 } else
|
adamc@1260
|
989 ch = channels[chn];
|
adamc@670
|
990
|
adamc@1260
|
991 var msg = dequeue(ch.msgs);
|
adamc@1260
|
992 if (msg == null) {
|
adamc@1260
|
993 enqueue(ch.listeners, function(msg) { k(parse(msg)); });
|
adamc@1260
|
994 } else {
|
adamc@1260
|
995 try {
|
adamc@1260
|
996 k(parse(msg));
|
adamc@1260
|
997 } catch (v) {
|
adamc@1260
|
998 doExn(v);
|
adamc@1260
|
999 }
|
adamc@728
|
1000 }
|
adamc@670
|
1001 }
|
adamc@693
|
1002
|
adamc@978
|
1003 function sl(ms, k) {
|
adamc@1260
|
1004 window.setTimeout(function() { k(null); }, ms);
|
adamc@1021
|
1005 }
|
adamc@1021
|
1006
|
adamc@1021
|
1007 function sp(e) {
|
adamc@1260
|
1008 execF(e, null);
|
adamc@978
|
1009 }
|
adamc@978
|
1010
|
adamc@693
|
1011
|
adamc@895
|
1012 // Key events
|
adamc@895
|
1013
|
adamc@970
|
1014 var uw_event = null;
|
adamc@970
|
1015
|
adamc@970
|
1016 function kc() {
|
adam@1290
|
1017 return window.event ? event.keyCode : (uw_event ? uw_event.keyCode : 0);
|
adamc@970
|
1018 }
|
adamc@970
|
1019
|
adamc@970
|
1020
|
adamc@970
|
1021 // The Ur interpreter
|
adamc@970
|
1022
|
adamc@970
|
1023 var urfuncs = [];
|
adamc@970
|
1024
|
adamc@970
|
1025 function lookup(env, n) {
|
adamc@1260
|
1026 while (env != null) {
|
adamc@1260
|
1027 if (n == 0)
|
adamc@1260
|
1028 return env.data;
|
adamc@1260
|
1029 else {
|
adamc@1260
|
1030 --n;
|
adamc@1260
|
1031 env = env.next;
|
adamc@1260
|
1032 }
|
adamc@970
|
1033 }
|
adamc@970
|
1034
|
adamc@1260
|
1035 whine("Out-of-bounds Ur variable reference");
|
adamc@970
|
1036 }
|
adamc@970
|
1037
|
adamc@974
|
1038 function execP(env, p, v) {
|
adamc@1260
|
1039 switch (p.c) {
|
adamc@1260
|
1040 case "w":
|
adamc@1260
|
1041 return env;
|
adamc@1260
|
1042 case "v":
|
adamc@1260
|
1043 return cons(v, env);
|
adamc@1260
|
1044 case "c":
|
adamc@1260
|
1045 if (v == p.v)
|
adamc@1260
|
1046 return env;
|
adamc@1260
|
1047 else
|
adamc@1260
|
1048 return false;
|
adamc@1260
|
1049 case "s":
|
adamc@1260
|
1050 if (v == null)
|
adamc@1260
|
1051 return false;
|
adamc@1260
|
1052 else
|
adamc@1260
|
1053 return execP(env, p.p, p.n ? v.v : v);
|
adamc@1260
|
1054 case "1":
|
adamc@1260
|
1055 if (v.n != p.n)
|
adamc@1260
|
1056 return false;
|
adamc@1260
|
1057 else
|
adamc@1260
|
1058 return execP(env, p.p, v.v);
|
adamc@1260
|
1059 case "r":
|
adamc@1260
|
1060 for (var fs = p.l; fs != null; fs = fs.next) {
|
adamc@1260
|
1061 env = execP(env, fs.data.p, v["_" + fs.data.n]);
|
adamc@1260
|
1062 if (env == false)
|
adamc@1260
|
1063 return false;
|
adamc@1260
|
1064 }
|
adamc@1260
|
1065 return env;
|
adamc@1260
|
1066 default:
|
adamc@1260
|
1067 whine("Unknown Ur pattern kind" + p.c);
|
adamc@974
|
1068 }
|
adamc@974
|
1069 }
|
adamc@974
|
1070
|
adamc@970
|
1071 function exec0(env, e) {
|
adamc@1260
|
1072 return exec1(env, null, e);
|
adamc@1020
|
1073 }
|
adamc@1020
|
1074
|
adamc@1020
|
1075 function exec1(env, stack, e) {
|
adamc@1260
|
1076 var stack, usedK = false;
|
adamc@970
|
1077
|
adamc@1260
|
1078 var saveEnv = function() {
|
adamc@1260
|
1079 if (stack.next != null && stack.next.data.c != "<")
|
adamc@1260
|
1080 stack = cons({c: "<", env: env}, stack.next);
|
adamc@1260
|
1081 else
|
adamc@1260
|
1082 stack = stack.next;
|
adamc@1260
|
1083 };
|
adamc@983
|
1084
|
adamc@1260
|
1085 while (true) {
|
adamc@1260
|
1086 switch (e.c) {
|
adamc@1260
|
1087 case "c":
|
adamc@1260
|
1088 var v = e.v;
|
adamc@1260
|
1089 if (stack == null)
|
adamc@1260
|
1090 return v;
|
adamc@1260
|
1091 var fr = stack.data;
|
adamc@970
|
1092
|
adamc@1260
|
1093 switch (fr.c) {
|
adamc@1260
|
1094 case "s":
|
adamc@1260
|
1095 e = {c: "c", v: {v: v}};
|
adamc@1260
|
1096 stack = stack.next;
|
adamc@1260
|
1097 break;
|
adamc@1260
|
1098 case "1":
|
adamc@1260
|
1099 e = {c: "c", v: {n: fr.n, v: v}};
|
adamc@1260
|
1100 stack = stack.next;
|
adamc@1260
|
1101 break;
|
adamc@1260
|
1102 case "f":
|
adamc@1260
|
1103 fr.args[fr.pos++] = v;
|
adamc@1260
|
1104 if (fr.a == null) {
|
adamc@1260
|
1105 var res;
|
adamc@1260
|
1106 stack = stack.next;
|
adamc@1035
|
1107
|
adamc@1260
|
1108 if (fr.f.apply)
|
adamc@1260
|
1109 res = fr.f.apply(null, fr.args);
|
adamc@1260
|
1110 else if (fr.args.length == 0)
|
adamc@1260
|
1111 res = fr.f();
|
adamc@1260
|
1112 else if (fr.args.length == 1)
|
adamc@1260
|
1113 res = fr.f(fr.args[0]);
|
adamc@1260
|
1114 else if (fr.args.length == 2)
|
adamc@1260
|
1115 res = fr.f(fr.args[0], fr.args[1]);
|
adamc@1260
|
1116 else if (fr.args.length == 3)
|
adamc@1260
|
1117 res = fr.f(fr.args[0], fr.args[1], fr.args[2]);
|
adamc@1260
|
1118 else if (fr.args.length == 4)
|
adamc@1260
|
1119 res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3]);
|
adamc@1260
|
1120 else if (fr.args.length == 5)
|
adamc@1260
|
1121 res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3], fr.args[4]);
|
adamc@1260
|
1122 else
|
adamc@1260
|
1123 whine("Native function has " + fr.args.length + " args, but there is no special case for that count.");
|
adamc@1035
|
1124
|
adamc@1260
|
1125 e = {c: "c", v: res};
|
adamc@1260
|
1126 if (usedK) return null;
|
adamc@1260
|
1127 } else {
|
adamc@1260
|
1128 e = fr.a.data;
|
adamc@1260
|
1129 fr.a = fr.a.next;
|
adamc@1260
|
1130 }
|
adamc@1260
|
1131 break;
|
adamc@1260
|
1132 case "a1":
|
adamc@1260
|
1133 e = fr.x;
|
adamc@1260
|
1134 stack = cons({c: "a2", f: v}, stack.next);
|
adamc@1260
|
1135 break;
|
adamc@1260
|
1136 case "a2":
|
adamc@1260
|
1137 if (fr.f == null)
|
adamc@1260
|
1138 whine("Ur: applying null function");
|
adamc@1260
|
1139 else if (fr.f.body) {
|
adamc@1260
|
1140 saveEnv();
|
adamc@1260
|
1141 env = cons(v, fr.f.env);
|
adamc@1260
|
1142 e = fr.f.body;
|
adamc@1260
|
1143 } else {
|
adamc@1260
|
1144 e = {c: "c", v: fr.f(v)};
|
adamc@1260
|
1145 stack = stack.next;
|
adamc@1260
|
1146 }
|
adamc@1260
|
1147 break;
|
adamc@1260
|
1148 case "<":
|
adamc@1260
|
1149 env = fr.env;
|
adamc@1260
|
1150 stack = stack.next;
|
adamc@1260
|
1151 break;
|
adamc@1260
|
1152 case "r":
|
adamc@1260
|
1153 fr.fs["_" + fr.n] = v;
|
adamc@1260
|
1154 if (fr.l == null) {
|
adamc@1260
|
1155 e = {c: "c", v: fr.fs};
|
adamc@1260
|
1156 stack = stack.next;
|
adamc@1260
|
1157 } else {
|
adamc@1260
|
1158 fr.n = fr.l.data.n;
|
adamc@1260
|
1159 e = fr.l.data.v;
|
adamc@1260
|
1160 fr.l = fr.l.next;
|
adamc@1260
|
1161 }
|
adamc@1260
|
1162 break;
|
adamc@1260
|
1163 case ".":
|
adamc@1260
|
1164 e = {c: "c", v: v["_" + fr.f]};
|
adamc@1260
|
1165 stack = stack.next;
|
adamc@1260
|
1166 break;
|
adamc@1260
|
1167 case ";":
|
adamc@1260
|
1168 e = fr.e2;
|
adamc@1260
|
1169 stack = stack.next;
|
adamc@1260
|
1170 break;
|
adamc@1260
|
1171 case "=":
|
adamc@1260
|
1172 saveEnv();
|
adamc@1260
|
1173 env = cons(v, env);
|
adamc@1260
|
1174 e = fr.e2;
|
adamc@1260
|
1175 break;
|
adamc@1260
|
1176 case "m":
|
adamc@1260
|
1177 var ps;
|
adamc@1260
|
1178 for (ps = fr.p; ps != null; ps = ps.next) {
|
adamc@1260
|
1179 var r = execP(env, ps.data.p, v);
|
adamc@1260
|
1180 if (r != false) {
|
adamc@1260
|
1181 saveEnv();
|
adamc@1260
|
1182 env = r;
|
adamc@1260
|
1183 e = ps.data.b;
|
adamc@1260
|
1184 break;
|
adamc@1260
|
1185 }
|
adamc@1260
|
1186 }
|
adamc@1260
|
1187 if (ps == null)
|
adamc@1260
|
1188 whine("Match failure in Ur interpretation");
|
adamc@1260
|
1189 break;
|
adamc@1260
|
1190 default:
|
adamc@1260
|
1191 whine("Unknown Ur continuation kind " + fr.c);
|
adamc@1260
|
1192 }
|
adamc@1260
|
1193
|
adamc@1260
|
1194 break;
|
adamc@1260
|
1195 case "v":
|
adamc@1260
|
1196 e = {c: "c", v: lookup(env, e.n)};
|
adamc@1260
|
1197 break;
|
adamc@1260
|
1198 case "n":
|
adamc@1260
|
1199 var idx = e.n;
|
adamc@1260
|
1200 e = urfuncs[idx];
|
adamc@1260
|
1201 if (e.c == "t")
|
adamc@1262
|
1202 e = urfuncs[idx] = eval("(" + e.f + ")");
|
adamc@1260
|
1203 break;
|
adamc@1260
|
1204 case "s":
|
adamc@1260
|
1205 stack = cons({c: "s"}, stack);
|
adamc@1260
|
1206 e = e.v;
|
adamc@1260
|
1207 break;
|
adamc@1260
|
1208 case "1":
|
adamc@1260
|
1209 stack = cons({c: "1", n: e.n}, stack);
|
adamc@1260
|
1210 e = e.v;
|
adamc@1260
|
1211 break;
|
adamc@1260
|
1212 case "f":
|
adamc@1260
|
1213 if (e.a == null)
|
adamc@1260
|
1214 e = {c: "c", v: (eval(e.f))()};
|
adamc@1260
|
1215 else {
|
adamc@1260
|
1216 var args = [];
|
adamc@1260
|
1217 stack = cons({c: "f", f: eval(e.f), args: args, pos: 0, a: e.a.next}, stack);
|
adamc@1260
|
1218 if (!e.a.data.c) alert("[2] fr.f = " + e.f + "; 0 = " + e.a.data);
|
adamc@1260
|
1219 e = e.a.data;
|
adamc@1260
|
1220 }
|
adamc@1260
|
1221 break;
|
adamc@1260
|
1222 case "l":
|
adamc@1260
|
1223 e = {c: "c", v: {env: env, body: e.b}};
|
adamc@1260
|
1224 break;
|
adamc@1260
|
1225 case "a":
|
adamc@1260
|
1226 stack = cons({c: "a1", x: e.x}, stack);
|
adamc@1260
|
1227 e = e.f;
|
adamc@1260
|
1228 break;
|
adamc@1260
|
1229 case "r":
|
adamc@1260
|
1230 if (e.l == null)
|
adamc@1260
|
1231 whine("Empty Ur record in interpretation");
|
adamc@1260
|
1232 var fs = {};
|
adamc@1260
|
1233 stack = cons({c: "r", n: e.l.data.n, fs: fs, l: e.l.next}, stack);
|
adamc@1260
|
1234 e = e.l.data.v;
|
adamc@1260
|
1235 break;
|
adamc@1260
|
1236 case ".":
|
adamc@1260
|
1237 stack = cons({c: ".", f: e.f}, stack);
|
adamc@1260
|
1238 e = e.r;
|
adamc@1260
|
1239 break;
|
adamc@1260
|
1240 case ";":
|
adamc@1260
|
1241 stack = cons({c: ";", e2: e.e2}, stack);
|
adamc@1260
|
1242 e = e.e1;
|
adamc@1260
|
1243 break;
|
adamc@1260
|
1244 case "=":
|
adamc@1260
|
1245 stack = cons({c: "=", e2: e.e2}, stack);
|
adamc@1260
|
1246 e = e.e1;
|
adamc@1260
|
1247 break;
|
adamc@1260
|
1248 case "m":
|
adamc@1260
|
1249 stack = cons({c: "m", p: e.p}, stack);
|
adamc@1260
|
1250 e = e.e;
|
adamc@1260
|
1251 break;
|
adamc@1260
|
1252 case "e":
|
adamc@1260
|
1253 e = {c: "c", v: cs({c: "wc", env: env, body: e.e})};
|
adamc@1260
|
1254 break;
|
adamc@1260
|
1255 case "wc":
|
adamc@1260
|
1256 env = e.env;
|
adamc@1260
|
1257 e = e.body;
|
adamc@1260
|
1258 break;
|
adamc@1260
|
1259 case "K":
|
adamc@1260
|
1260 { var savedStack = stack.next, savedEnv = env;
|
adamc@1260
|
1261 e = {c: "c", v: function(v) { return exec1(savedEnv, savedStack, {c: "c", v: v}); } };}
|
adamc@1260
|
1262 usedK = true;
|
adamc@1260
|
1263 break;
|
adamc@1260
|
1264 default:
|
adamc@1260
|
1265 whine("Unknown Ur expression kind " + e.c);
|
adamc@970
|
1266 }
|
adamc@970
|
1267 }
|
adamc@970
|
1268 }
|
adamc@970
|
1269
|
adamc@976
|
1270 function execD(e) {
|
adamc@1260
|
1271 return exec0(null, e);
|
adamc@976
|
1272 }
|
adamc@976
|
1273
|
adamc@970
|
1274 function exec(e) {
|
adamc@1260
|
1275 var r = exec0(null, e);
|
adamc@970
|
1276
|
adamc@1260
|
1277 if (r != null && r.body != null)
|
adamc@1260
|
1278 return function(v) { return exec0(cons(v, r.env), r.body); };
|
adamc@1260
|
1279 else
|
adamc@1260
|
1280 return r;
|
adamc@895
|
1281 }
|
adamc@895
|
1282
|
adamc@977
|
1283 function execF(f, x) {
|
adamc@1260
|
1284 return exec0(cons(x, f.env), f.body);
|
adamc@977
|
1285 }
|
adamc@977
|
1286
|
adamc@895
|
1287
|
adam@1290
|
1288 // Wrappers
|
adam@1290
|
1289
|
adam@1290
|
1290 function confrm(s) {
|
adam@1290
|
1291 return confirm(s) ? true : false;
|
adam@1290
|
1292 }
|
adam@1290
|
1293
|
adam@1290
|
1294
|
adamc@1323
|
1295 // URL blessing
|
adamc@1323
|
1296
|
adamc@1323
|
1297 var urlRules = null;
|
adamc@1323
|
1298
|
adamc@1323
|
1299 function checkUrl(s) {
|
adamc@1323
|
1300 for (var r = urlRules; r; r = r.next) {
|
adamc@1323
|
1301 var ru = r.data;
|
adamc@1323
|
1302 if (ru.prefix ? s.indexOf(ru.pattern) == 0 : s == ru.pattern)
|
adamc@1323
|
1303 return ru.allow ? s : null;
|
adamc@1323
|
1304 }
|
adamc@1323
|
1305
|
adamc@1323
|
1306 return null;
|
adamc@1323
|
1307 }
|
adamc@1323
|
1308
|
adamc@1323
|
1309 function bless(s) {
|
adamc@1323
|
1310 u = checkUrl(s);
|
adamc@1323
|
1311 if (u == null)
|
adamc@1323
|
1312 er("Disallowed URL: " + s);
|
adamc@1323
|
1313 return u;
|
adamc@1323
|
1314 }
|
adamc@1323
|
1315
|
adamc@693
|
1316 // App-specific code
|
adamc@693
|
1317
|