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