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@1619
|
22 function divInt(x, y) { if (y == 0) er("Division by zero"); var n = x / y; return n < 0 ? Math.ceil(n) : Math.floor(n); }
|
adamc@970
|
23 function mod(x, y) { return x % y; }
|
adam@1619
|
24 function modInt(x, y) { if (y == 0) er("Division by zero"); 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)
|
adam@1643
|
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@1571
|
96 // Floats
|
adam@1571
|
97
|
adam@1571
|
98 function float(n) {
|
adam@1571
|
99 return n;
|
adam@1571
|
100 }
|
adam@1571
|
101
|
adam@1571
|
102 function trunc(n) {
|
adam@1571
|
103 return ~~n;
|
adam@1571
|
104 }
|
adam@1571
|
105
|
adam@1571
|
106 function ceil(n) {
|
adam@1571
|
107 return Math.ceil(n);
|
adam@1571
|
108 }
|
adam@1571
|
109
|
adam@1571
|
110 function round(n) {
|
adam@1571
|
111 return Math.round(n);
|
adam@1571
|
112 }
|
adam@1571
|
113
|
adam@1571
|
114
|
adam@1599
|
115 // Time, represented as counts of microseconds since the epoch
|
adam@1487
|
116
|
adam@1629
|
117 var time_format = "%c";
|
adam@1629
|
118
|
adam@1487
|
119 function showTime(tm) {
|
adam@1629
|
120 return strftime(time_format, tm);
|
adam@1629
|
121 }
|
adam@1629
|
122
|
adam@1629
|
123 function showTimeHtml(tm) {
|
adam@1629
|
124 return eh(showTime(tm));
|
adam@1487
|
125 }
|
adam@1487
|
126
|
adam@1487
|
127 function now() {
|
adam@1487
|
128 return (new Date()).getTime() * 1000;
|
adam@1487
|
129 }
|
adam@1487
|
130
|
adam@1514
|
131 function diffInSeconds(tm1, tm2) {
|
adam@1514
|
132 return Math.round((tm2 - tm1) / 1000000);
|
adam@1514
|
133 }
|
adam@1514
|
134
|
adam@1514
|
135 function toSeconds(tm) {
|
adam@1514
|
136 return Math.round(tm / 1000000);
|
adam@1514
|
137 }
|
adam@1514
|
138
|
adam@1518
|
139 function addSeconds(tm, n) {
|
adam@1518
|
140 return tm + n * 1000000;
|
adam@1518
|
141 }
|
adam@1518
|
142
|
adam@1599
|
143 function stringToTime_error(string) {
|
adam@1599
|
144 var t = Date.parse(string);
|
adam@1599
|
145 if (isNaN(t))
|
adam@1632
|
146 er("Invalid date string: " + string);
|
adam@1599
|
147 else
|
adam@1599
|
148 return t * 1000;
|
adam@1599
|
149 }
|
adam@1599
|
150
|
adam@1599
|
151 function stringToTime(string) {
|
adam@1599
|
152 try {
|
adam@1599
|
153 var t = Date.parse(string);
|
adam@1599
|
154 if (isNaN(t))
|
adam@1599
|
155 return null;
|
adam@1599
|
156 else
|
adam@1599
|
157 return t * 1000;
|
adam@1599
|
158 } catch (e) {
|
adam@1599
|
159 return null;
|
adam@1599
|
160 }
|
rmbruijn@1597
|
161 }
|
rmbruijn@1597
|
162
|
adam@1609
|
163 /*
|
adam@1615
|
164 strftime() implementation from:
|
adam@1615
|
165 YUI 3.4.1 (build 4118)
|
adam@1615
|
166 Copyright 2011 Yahoo! Inc. All rights reserved.
|
adam@1615
|
167 Licensed under the BSD License.
|
adam@1615
|
168 http://yuilibrary.com/license/
|
adam@1615
|
169 */
|
adam@1609
|
170
|
adam@1615
|
171 var xPad=function (x, pad, r)
|
adam@1615
|
172 {
|
adam@1615
|
173 if(typeof r === "undefined")
|
adam@1615
|
174 {
|
adam@1615
|
175 r=10;
|
adam@1615
|
176 }
|
adam@1615
|
177 pad = pad.toString();
|
adam@1615
|
178 for( ; parseInt(x, 10)<r && r>1; r/=10) {
|
adam@1615
|
179 x = pad + x;
|
adam@1615
|
180 }
|
adam@1615
|
181 return x.toString();
|
adam@1615
|
182 };
|
adam@1609
|
183
|
adam@1615
|
184 var YDateEn = {
|
adam@1615
|
185 a: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
|
adam@1615
|
186 A: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
|
adam@1615
|
187 b: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
|
adam@1615
|
188 B: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
|
adam@1615
|
189 c: "%a %d %b %Y %T %Z",
|
adam@1615
|
190 p: ["AM", "PM"],
|
adam@1615
|
191 P: ["am", "pm"],
|
adam@1615
|
192 r: "%I:%M:%S %p",
|
adam@1615
|
193 x: "%d/%m/%y",
|
adam@1615
|
194 X: "%T"
|
adam@1615
|
195 };
|
adam@1609
|
196
|
adam@1615
|
197 var Dt = {
|
adam@1615
|
198 formats: {
|
adam@1615
|
199 a: function (d, l) { return l.a[d.getDay()]; },
|
adam@1615
|
200 A: function (d, l) { return l.A[d.getDay()]; },
|
adam@1615
|
201 b: function (d, l) { return l.b[d.getMonth()]; },
|
adam@1615
|
202 B: function (d, l) { return l.B[d.getMonth()]; },
|
adam@1615
|
203 C: function (d) { return xPad(parseInt(d.getFullYear()/100, 10), 0); },
|
adam@1615
|
204 d: ["getDate", "0"],
|
adam@1615
|
205 e: ["getDate", " "],
|
adam@1615
|
206 g: function (d) { return xPad(parseInt(Dt.formats.G(d)%100, 10), 0); },
|
adam@1615
|
207 G: function (d) {
|
adam@1615
|
208 var y = d.getFullYear();
|
adam@1615
|
209 var V = parseInt(Dt.formats.V(d), 10);
|
adam@1615
|
210 var W = parseInt(Dt.formats.W(d), 10);
|
adam@1615
|
211
|
adam@1615
|
212 if(W > V) {
|
adam@1615
|
213 y++;
|
adam@1615
|
214 } else if(W===0 && V>=52) {
|
adam@1615
|
215 y--;
|
adam@1615
|
216 }
|
adam@1615
|
217
|
adam@1615
|
218 return y;
|
adam@1615
|
219 },
|
adam@1615
|
220 H: ["getHours", "0"],
|
adam@1615
|
221 I: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, 0); },
|
adam@1615
|
222 j: function (d) {
|
adam@1615
|
223 var gmd_1 = new Date("" + d.getFullYear() + "/1/1 GMT");
|
adam@1615
|
224 var gmdate = new Date("" + d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate() + " GMT");
|
adam@1615
|
225 var ms = gmdate - gmd_1;
|
adam@1615
|
226 var doy = parseInt(ms/60000/60/24, 10)+1;
|
adam@1615
|
227 return xPad(doy, 0, 100);
|
adam@1615
|
228 },
|
adam@1615
|
229 k: ["getHours", " "],
|
adam@1615
|
230 l: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, " "); },
|
adam@1615
|
231 m: function (d) { return xPad(d.getMonth()+1, 0); },
|
adam@1615
|
232 M: ["getMinutes", "0"],
|
adam@1615
|
233 p: function (d, l) { return l.p[d.getHours() >= 12 ? 1 : 0 ]; },
|
adam@1615
|
234 P: function (d, l) { return l.P[d.getHours() >= 12 ? 1 : 0 ]; },
|
adam@1615
|
235 s: function (d, l) { return parseInt(d.getTime()/1000, 10); },
|
adam@1615
|
236 S: ["getSeconds", "0"],
|
adam@1615
|
237 u: function (d) { var dow = d.getDay(); return dow===0?7:dow; },
|
adam@1615
|
238 U: function (d) {
|
adam@1615
|
239 var doy = parseInt(Dt.formats.j(d), 10);
|
adam@1615
|
240 var rdow = 6-d.getDay();
|
adam@1615
|
241 var woy = parseInt((doy+rdow)/7, 10);
|
adam@1615
|
242 return xPad(woy, 0);
|
adam@1615
|
243 },
|
adam@1615
|
244 V: function (d) {
|
adam@1615
|
245 var woy = parseInt(Dt.formats.W(d), 10);
|
adam@1615
|
246 var dow1_1 = (new Date("" + d.getFullYear() + "/1/1")).getDay();
|
adam@1615
|
247 var idow = woy + (dow1_1 > 4 || dow1_1 <= 1 ? 0 : 1);
|
adam@1615
|
248 if(idow === 53 && (new Date("" + d.getFullYear() + "/12/31")).getDay() < 4)
|
adam@1615
|
249 {
|
adam@1615
|
250 idow = 1;
|
adam@1615
|
251 }
|
adam@1615
|
252 else if(idow === 0)
|
adam@1615
|
253 {
|
adam@1615
|
254 idow = Dt.formats.V(new Date("" + (d.getFullYear()-1) + "/12/31"));
|
adam@1615
|
255 }
|
adam@1615
|
256
|
adam@1615
|
257 return xPad(idow, 0);
|
adam@1615
|
258 },
|
adam@1615
|
259 w: "getDay",
|
adam@1615
|
260 W: function (d) {
|
adam@1615
|
261 var doy = parseInt(Dt.formats.j(d), 10);
|
adam@1615
|
262 var rdow = 7-Dt.formats.u(d);
|
adam@1615
|
263 var woy = parseInt((doy+rdow)/7, 10);
|
adam@1615
|
264 return xPad(woy, 0, 10);
|
adam@1615
|
265 },
|
adam@1615
|
266 y: function (d) { return xPad(d.getFullYear()%100, 0); },
|
adam@1615
|
267 Y: "getFullYear",
|
adam@1615
|
268 z: function (d) {
|
adam@1615
|
269 var o = d.getTimezoneOffset();
|
adam@1615
|
270 var H = xPad(parseInt(Math.abs(o/60), 10), 0);
|
adam@1615
|
271 var M = xPad(Math.abs(o%60), 0);
|
adam@1615
|
272 return (o>0?"-":"+") + H + M;
|
adam@1615
|
273 },
|
adam@1615
|
274 Z: function (d) {
|
adam@1615
|
275 var tz = d.toString().replace(/^.*:\d\d( GMT[+-]\d+)? \(?([A-Za-z ]+)\)?\d*$/, "$2").replace(/[a-z ]/g, "");
|
adam@1615
|
276 if(tz.length > 4) {
|
adam@1615
|
277 tz = Dt.formats.z(d);
|
adam@1615
|
278 }
|
adam@1615
|
279 return tz;
|
adam@1615
|
280 },
|
adam@1615
|
281 "%": function (d) { return "%"; }
|
adam@1615
|
282 },
|
adam@1615
|
283
|
adam@1615
|
284 aggregates: {
|
adam@1615
|
285 c: "locale",
|
adam@1615
|
286 D: "%m/%d/%y",
|
adam@1615
|
287 F: "%Y-%m-%d",
|
adam@1615
|
288 h: "%b",
|
adam@1615
|
289 n: "\n",
|
adam@1615
|
290 r: "%I:%M:%S %p",
|
adam@1615
|
291 R: "%H:%M",
|
adam@1615
|
292 t: "\t",
|
adam@1615
|
293 T: "%H:%M:%S",
|
adam@1615
|
294 x: "locale",
|
adam@1615
|
295 X: "locale"
|
adam@1615
|
296 },
|
adam@1615
|
297
|
adam@1615
|
298 format : function (oDate, format) {
|
adam@1615
|
299 var replace_aggs = function (m0, m1) {
|
adam@1615
|
300 var f = Dt.aggregates[m1];
|
adam@1615
|
301 return (f === "locale" ? YDateEn[m1] : f);
|
adam@1615
|
302 };
|
adam@1615
|
303
|
adam@1615
|
304 var replace_formats = function (m0, m1) {
|
adam@1615
|
305 var f = Dt.formats[m1];
|
adam@1615
|
306 switch(typeof f) {
|
adam@1615
|
307 case "string":
|
adam@1615
|
308 return oDate[f]();
|
adam@1615
|
309 case "function":
|
adam@1615
|
310 return f.call(oDate, oDate, YDateEn);
|
adam@1615
|
311 case "array":
|
adam@1615
|
312 case "object":
|
adam@1615
|
313 if(typeof(f[0]) === "string")
|
adam@1615
|
314 return xPad(oDate[f[0]](), f[1]);
|
adam@1615
|
315 default:
|
adam@1615
|
316 return m1;
|
adam@1615
|
317 }
|
adam@1615
|
318 };
|
adam@1615
|
319
|
adam@1615
|
320 while(format.match(/%[cDFhnrRtTxX]/)) {
|
adam@1615
|
321 format = format.replace(/%([cDFhnrRtTxX])/g, replace_aggs);
|
adam@1609
|
322 }
|
adam@1615
|
323
|
adam@1615
|
324 var str = format.replace(/%([aAbBCdegGHIjklmMpPsSuUVwWyYzZ%])/g, replace_formats);
|
adam@1615
|
325
|
adam@1615
|
326 replace_aggs = replace_formats = undefined;
|
adam@1615
|
327
|
adam@1615
|
328 return str;
|
adam@1615
|
329 }
|
adam@1609
|
330 };
|
adam@1609
|
331
|
adam@1615
|
332 // End of YUI code
|
adam@1609
|
333
|
adam@1609
|
334 function strftime(fmt, thisTime)
|
adam@1609
|
335 {
|
adam@1609
|
336 var thisDate = new Date();
|
adam@1622
|
337 thisDate.setTime(Math.floor(thisTime / 1000));
|
adam@1615
|
338 return Dt.format(thisDate, fmt);
|
adam@1622
|
339 };
|
adam@1609
|
340
|
adam@1487
|
341
|
adamc@729
|
342 // Error handling
|
adamc@729
|
343
|
adam@1625
|
344 function uw_debug(msg) {
|
adam@1625
|
345 try {
|
adam@1625
|
346 console.debug(msg);
|
adam@1625
|
347 } catch (e) {
|
adam@1625
|
348 alert("DEBUG: " + msg);
|
adam@1625
|
349 }
|
adam@1625
|
350
|
adam@1625
|
351 return 0;
|
adam@1625
|
352 }
|
adam@1625
|
353
|
adamc@729
|
354 function whine(msg) {
|
adamc@1260
|
355 alert(msg);
|
adamc@1260
|
356 throw msg;
|
adamc@729
|
357 }
|
adamc@729
|
358
|
adamc@810
|
359 function pf(loc) {
|
adamc@1260
|
360 throw ("Pattern match failure (" + loc + ")");
|
adamc@729
|
361 }
|
adamc@729
|
362
|
adam@1553
|
363 var lameDuck = false;
|
adam@1553
|
364
|
adamc@798
|
365 function runHandlers(kind, ls, arg) {
|
adam@1553
|
366 if (!lameDuck) {
|
adam@1553
|
367 if (ls == null)
|
adam@1553
|
368 alert(kind + ": " + arg);
|
adam@1553
|
369 for (; ls; ls = ls.next)
|
adam@1553
|
370 try {
|
adam@1553
|
371 exec({c:"a", f:{c:"a", f:ls.data, x:{c:"c", v:arg}}, x:{c:"c", v:null}});
|
adam@1553
|
372 } catch (v) { }
|
adam@1553
|
373 }
|
adamc@729
|
374 }
|
adamc@729
|
375
|
adamc@729
|
376 var errorHandlers = null;
|
adamc@729
|
377
|
adamc@729
|
378 function onError(f) {
|
adamc@1260
|
379 errorHandlers = cons(f, errorHandlers);
|
adamc@729
|
380 }
|
adamc@729
|
381
|
adamc@729
|
382 function er(s) {
|
adamc@1260
|
383 runHandlers("Error", errorHandlers, s);
|
adamc@1260
|
384 throw {uw_error: s};
|
adamc@729
|
385 }
|
adamc@729
|
386
|
adamc@729
|
387 var failHandlers = null;
|
adamc@729
|
388
|
adamc@729
|
389 function onFail(f) {
|
adamc@1260
|
390 failHandlers = cons(f, failHandlers);
|
adamc@729
|
391 }
|
adamc@729
|
392
|
adamc@729
|
393 function doExn(v) {
|
adamc@1260
|
394 if (v == null || v.uw_error == null) {
|
adamc@1260
|
395 var s = (v == null ? "null" : v.message ? v.message : v.toString());
|
adamc@1260
|
396 if (v != null && v.fileName && v.lineNumber)
|
adamc@1260
|
397 s += " (" + v.fileName + ":" + v.lineNumber + ")";
|
adamc@1260
|
398 runHandlers("Fail", failHandlers, s);
|
adamc@1260
|
399 }
|
adamc@729
|
400 }
|
adamc@729
|
401
|
adamc@729
|
402 var disconnectHandlers = null;
|
adamc@729
|
403
|
adamc@976
|
404 function flift(f) {
|
kkallio@1484
|
405 return {c: "c", v:{env:cons(f,null), body:{c:"v", n:1}}};
|
adamc@976
|
406 }
|
adamc@976
|
407
|
adamc@729
|
408 function onDisconnect(f) {
|
adamc@1260
|
409 disconnectHandlers = cons(flift(f), disconnectHandlers);
|
adamc@729
|
410 }
|
adamc@729
|
411
|
adamc@729
|
412 function discon() {
|
adamc@1260
|
413 runHandlers("Disconnect", disconnectHandlers, null);
|
adamc@729
|
414 }
|
adamc@729
|
415
|
adamc@729
|
416 var connectHandlers = null;
|
adamc@729
|
417
|
adamc@729
|
418 function onConnectFail(f) {
|
adamc@1260
|
419 connectHandlers = cons(flift(f), connectHandlers);
|
adamc@729
|
420 }
|
adamc@729
|
421
|
adamc@729
|
422 function conn() {
|
adamc@1260
|
423 runHandlers("Connect", connectHandlers, null);
|
adamc@729
|
424 }
|
adamc@729
|
425
|
adamc@729
|
426 var serverHandlers = null;
|
adamc@729
|
427
|
adamc@729
|
428 function onServerError(f) {
|
adamc@1260
|
429 serverHandlers = cons(f, serverHandlers);
|
adamc@729
|
430 }
|
adamc@729
|
431
|
adamc@729
|
432 function servErr(s) {
|
adamc@1260
|
433 window.setTimeout(function () { runHandlers("Server", serverHandlers, s); }, 0);
|
adamc@729
|
434 }
|
adamc@729
|
435
|
adam@1555
|
436 // Key events
|
adam@1555
|
437
|
adam@1555
|
438 var uw_event = null;
|
adam@1555
|
439
|
adam@1555
|
440 function kc() {
|
adam@1555
|
441 return window.event ? event.keyCode : (uw_event ? uw_event.which : 0);
|
adam@1555
|
442 }
|
adam@1555
|
443
|
adam@1555
|
444 // Document events
|
adam@1555
|
445
|
adam@1555
|
446 function uw_handler(name, f) {
|
adam@1555
|
447 var old = document[name];
|
adam@1555
|
448 if (old == undefined)
|
adam@1559
|
449 document[name] = function(event) { uw_event = event; execF(f); };
|
adam@1555
|
450 else
|
adam@1559
|
451 document[name] = function(event) { uw_event = event; old(); execF(f); };
|
adam@1555
|
452 }
|
adam@1555
|
453
|
adam@1555
|
454 function uw_onClick(f) {
|
adam@1555
|
455 uw_handler("onclick", f);
|
adam@1555
|
456 }
|
adam@1555
|
457
|
adam@1555
|
458 function uw_onDblclick(f) {
|
adam@1555
|
459 uw_handler("ondblclick", f);
|
adam@1555
|
460 }
|
adam@1555
|
461
|
adam@1555
|
462 function uw_onMousedown(f) {
|
adam@1555
|
463 uw_handler("onmousedown", f);
|
adam@1555
|
464 }
|
adam@1555
|
465
|
adam@1555
|
466 function uw_onMouseup(f) {
|
adam@1555
|
467 uw_handler("onmouseup", f);
|
adam@1555
|
468 }
|
adam@1555
|
469
|
adam@1555
|
470 function uw_keyHandler(name, f) {
|
adam@1555
|
471 var old = document[name];
|
adam@1555
|
472 if (old == undefined)
|
adam@1559
|
473 document[name] = function(event) { uw_event = event; execF(execF(f, kc())); };
|
adam@1555
|
474 else
|
adam@1559
|
475 document[name] = function(event) { uw_event = event; old(); execF(execF(f, kc())); };
|
adam@1555
|
476 }
|
adam@1555
|
477
|
adam@1555
|
478 function uw_onKeydown(f) {
|
adam@1555
|
479 uw_keyHandler("onkeydown", f);
|
adam@1555
|
480 }
|
adam@1555
|
481
|
adam@1555
|
482 function uw_onKeypress(f) {
|
adam@1555
|
483 uw_keyHandler("onkeypress", f);
|
adam@1555
|
484 }
|
adam@1555
|
485
|
adam@1555
|
486 function uw_onKeyup(f) {
|
adam@1555
|
487 uw_keyHandler("onkeyup", f);
|
adam@1555
|
488 }
|
adam@1555
|
489
|
adam@1559
|
490 // Cancelling of further event processing
|
adam@1559
|
491
|
adam@1559
|
492 function uw_preventDefault() {
|
adam@1559
|
493 var e = window.event ? window.event : uw_event;
|
adam@1559
|
494 e.returnValue = false;
|
adam@1559
|
495 if (e.preventDefault) e.preventDefault();
|
adam@1559
|
496 }
|
adam@1559
|
497
|
adam@1559
|
498 function uw_stopPropagation() {
|
adam@1559
|
499 var e = window.event ? window.event : uw_event;
|
adam@1559
|
500 e.cancelBubble = true;
|
adam@1559
|
501 if (e.stopPropagation) e.stopPropagation();
|
adam@1559
|
502 }
|
adamc@729
|
503
|
adamc@692
|
504 // Embedding closures in XML strings
|
adamc@692
|
505
|
adamc@693
|
506 function cs(f) {
|
adamc@1260
|
507 return {closure: f};
|
adamc@693
|
508 }
|
adamc@693
|
509
|
adamc@693
|
510 function isWeird(v) {
|
adamc@1260
|
511 return v.closure != null || v.cat1 != null;
|
adamc@693
|
512 }
|
adamc@693
|
513
|
adamc@692
|
514 function cat(s1, s2) {
|
adamc@1260
|
515 if (isWeird(s1) || isWeird(s2))
|
adamc@1260
|
516 return {cat1: s1, cat2: s2};
|
adamc@1260
|
517 else
|
adamc@1260
|
518 return s1 + s2;
|
adamc@692
|
519 }
|
adamc@692
|
520
|
adamc@692
|
521 var closures = [];
|
adamc@703
|
522 var freeClosures = null;
|
adamc@692
|
523
|
adamc@692
|
524 function newClosure(f) {
|
adamc@1260
|
525 var n;
|
adamc@1260
|
526 if (freeClosures == null) {
|
adamc@1260
|
527 n = closures.length;
|
adamc@1260
|
528 } else {
|
adamc@1260
|
529 n = freeClosures.data;
|
adamc@1260
|
530 freeClosures = freeClosures.next;
|
adamc@1260
|
531 }
|
adamc@1260
|
532 closures[n] = f;
|
adamc@1260
|
533 return n;
|
adamc@692
|
534 }
|
adamc@692
|
535
|
adamc@703
|
536 function freeClosure(n) {
|
adamc@1260
|
537 closures[n] = null;
|
adamc@1260
|
538 freeClosures = cons(n, freeClosures);
|
adamc@703
|
539 }
|
adamc@703
|
540
|
adamc@692
|
541 function cr(n) {
|
adamc@1260
|
542 return closures[n];
|
adamc@692
|
543 }
|
adamc@692
|
544
|
adamc@703
|
545 function flatten(cls, tr) {
|
adamc@1260
|
546 if (tr.cat1 != null)
|
adamc@1260
|
547 return flatten(cls, tr.cat1) + flatten(cls, tr.cat2);
|
adamc@1260
|
548 else if (tr.closure != null) {
|
adamc@1260
|
549 var cl = newClosure(tr.closure);
|
adamc@1260
|
550 cls.v = cons(cl, cls.v);
|
adamc@1260
|
551 return "cr(" + cl + ")";
|
adamc@1260
|
552 } else
|
adamc@1260
|
553 return tr;
|
adamc@692
|
554 }
|
adamc@692
|
555
|
adamc@728
|
556 function flattenLocal(s) {
|
adamc@1260
|
557 var cls = {v : null};
|
adamc@1260
|
558 var r = flatten(cls, s);
|
adamc@1260
|
559 for (cl = cls.v; cl != null; cl = cl.next)
|
adamc@1260
|
560 freeClosure(cl.data);
|
adamc@1260
|
561 return r;
|
adamc@728
|
562 }
|
adamc@728
|
563
|
adamc@692
|
564
|
adamc@692
|
565 // Dynamic tree management
|
adamc@692
|
566
|
adamc@690
|
567 function populate(node) {
|
adamc@1260
|
568 var s = node.signal;
|
adamc@1260
|
569 var oldSources = node.sources;
|
adamc@1260
|
570 try {
|
adamc@1260
|
571 var sr = execF(s, null);
|
adamc@1260
|
572 var newSources = sr._sources;
|
adamc@690
|
573
|
adamc@1260
|
574 for (var sp = oldSources; sp; sp = sp.next)
|
adamc@1260
|
575 if (!member(sp.data, newSources))
|
adamc@1260
|
576 sp.data.dyns = remove(node, sp.data.dyns);
|
adamc@690
|
577
|
adamc@1260
|
578 for (var sp = newSources; sp; sp = sp.next)
|
adamc@1260
|
579 if (!member(sp.data, oldSources))
|
adamc@1260
|
580 sp.data.dyns = cons(node, sp.data.dyns);
|
adamc@690
|
581
|
adamc@1260
|
582 node.sources = newSources;
|
adamc@1260
|
583 node.recreate(sr._data);
|
adamc@1260
|
584 } catch (v) {
|
adamc@1260
|
585 doExn(v);
|
adamc@1260
|
586 }
|
adamc@579
|
587 }
|
adamc@574
|
588
|
adamc@580
|
589 function sc(v) {
|
adamc@1260
|
590 return {data : v, dyns : null};
|
adamc@580
|
591 }
|
adamc@580
|
592 function sv(s, v) {
|
adam@1491
|
593 if (s.data != v) {
|
adam@1491
|
594 s.data = v;
|
adamc@967
|
595
|
adam@1491
|
596 for (var ls = s.dyns; ls; ls = ls.next)
|
adam@1491
|
597 if (!ls.dead)
|
adam@1491
|
598 populate(ls.data);
|
adam@1491
|
599 }
|
adamc@580
|
600 }
|
adamc@601
|
601 function sg(s) {
|
adamc@1260
|
602 return s.data;
|
adamc@601
|
603 }
|
adamc@579
|
604
|
adamc@580
|
605 function ss(s) {
|
adamc@1260
|
606 return {env:cons(s, null), body:{c:"r", l:
|
adamc@1260
|
607 cons({n:"sources", v:{c:"c", v:cons(s, null)}},
|
adamc@1260
|
608 cons({n:"data", v:{c:"f", f:sg, a:cons({c:"v", n:1}, null)}}, null))}};
|
adamc@580
|
609 }
|
adamc@580
|
610 function sr(v) {
|
adamc@1260
|
611 return {env:null, body:{c:"c", v:{_sources : null, _data : v}}};
|
adamc@580
|
612 }
|
adamc@580
|
613 function sb(x,y) {
|
adamc@1260
|
614 return {env:cons(y,cons(x,null)),
|
adamc@1260
|
615 body:{c:"=",
|
adamc@1260
|
616 e1:{c:"a", f:{c:"v", n:2}, x:{c:"c", v:null}},
|
adamc@1260
|
617 e2:{c:"=",
|
adamc@1260
|
618 e1:{c:"a",
|
adamc@1260
|
619 f:{c:"a", f:{c:"v", n:2}, x:{c:".", r:{c:"v", n:0}, f:"data"}},
|
adamc@1260
|
620 x:{c:"c", v:null}},
|
adamc@1260
|
621 e2:{c:"r", l:cons(
|
adamc@1260
|
622 {n:"sources", v:{c:"f", f:union, a:cons({c:".", r:{c:"v", n:1}, f:"sources"},
|
adamc@1260
|
623 cons({c:".", r:{c:"v", n:0}, f:"sources"}, null))}},
|
adamc@1260
|
624 cons({n:"data", v:{c:".", r:{c:"v", n:0}, f:"data"}}, null))}}}};
|
adamc@580
|
625 }
|
adamc@841
|
626 function scur(s) {
|
adamc@1260
|
627 return execF(s, null)._data;
|
adamc@841
|
628 }
|
adamc@571
|
629
|
adamc@604
|
630 function lastParent() {
|
adamc@1260
|
631 var pos = document.body;
|
adamc@604
|
632
|
adamc@1260
|
633 while (pos.lastChild && pos.lastChild.nodeType == 1)
|
adamc@1260
|
634 pos = pos.lastChild;
|
adamc@600
|
635
|
adamc@1260
|
636 pos = pos.parentNode;
|
adamc@1039
|
637
|
adamc@1260
|
638 return pos;
|
adamc@600
|
639 }
|
adamc@600
|
640
|
adamc@898
|
641 function parent() {
|
adamc@1260
|
642 return thisScript ? thisScript.parentNode : lastParent();
|
adamc@898
|
643 }
|
adamc@898
|
644
|
adamc@604
|
645 function addNode(node) {
|
adamc@1260
|
646 if (thisScript)
|
adamc@1260
|
647 thisScript.parentNode.replaceChild(node, thisScript);
|
adamc@1260
|
648 else
|
adamc@1260
|
649 lastParent().appendChild(node);
|
adamc@603
|
650 }
|
adamc@603
|
651
|
adamc@690
|
652 var thisScript = null;
|
adamc@690
|
653
|
adamc@604
|
654 function runScripts(node) {
|
adamc@1260
|
655 if (node.getElementsByTagName) {
|
adamc@1260
|
656 var savedScript = thisScript;
|
adamc@603
|
657
|
adamc@1260
|
658 var scripts = node.getElementsByTagName("script"), scriptsCopy = [];
|
adamc@1260
|
659 var len = scripts.length;
|
adamc@1260
|
660 for (var i = 0; i < len; ++i)
|
adamc@1260
|
661 scriptsCopy[i] = scripts[i];
|
adamc@1260
|
662 for (var i = 0; i < len; ++i) {
|
adamc@1260
|
663 thisScript = scriptsCopy[i];
|
adamc@1039
|
664
|
adamc@1260
|
665 try {
|
adamc@1260
|
666 eval(thisScript.text);
|
adamc@1260
|
667 } catch (v) {
|
adamc@1260
|
668 doExn(v);
|
adamc@1260
|
669 }
|
adamc@1260
|
670 if (thisScript.parentNode)
|
adamc@1260
|
671 thisScript.parentNode.removeChild(thisScript);
|
adamc@1260
|
672 }
|
adamc@1260
|
673
|
adamc@1260
|
674 thisScript = savedScript;
|
adamc@729
|
675 }
|
adamc@603
|
676 }
|
adamc@603
|
677
|
adamc@603
|
678
|
adamc@692
|
679 // Dynamic tree entry points
|
adamc@692
|
680
|
adamc@967
|
681 function killScript(scr) {
|
adamc@1260
|
682 scr.dead = true;
|
adamc@1260
|
683 for (var ls = scr.sources; ls; ls = ls.next)
|
adamc@1260
|
684 ls.data.dyns = remove(scr, ls.data.dyns);
|
adamc@1260
|
685 for (var ls = scr.closures; ls; ls = ls.next)
|
adamc@1260
|
686 freeClosure(ls.data);
|
adamc@967
|
687 }
|
adamc@967
|
688
|
adam@1538
|
689 // Sometimes we wind up with tables that contain <script>s outside the single <tbody>.
|
adam@1538
|
690 // To avoid dealing with that case, we normalize by moving <script>s into <tbody>.
|
adam@1538
|
691 function normalizeTable(table) {
|
adam@1538
|
692 var orig = table;
|
adam@1538
|
693
|
adam@1538
|
694 var script, next;
|
adam@1538
|
695
|
adam@1538
|
696 while (table.tagName != "TABLE")
|
adam@1538
|
697 table = table.parentNode;
|
adam@1538
|
698
|
adam@1538
|
699 for (var tbody = table.firstChild; tbody; tbody = tbody.nextSibling) {
|
adam@1538
|
700 if (tbody.tagName == "TBODY") {
|
adam@1538
|
701 for (script = table.firstChild; script && script != tbody; script = next) {
|
adam@1538
|
702 next = script.nextSibling;
|
adam@1538
|
703
|
adam@1538
|
704 tbody.insertBefore(script, tbody.firstChild);
|
adam@1538
|
705 }
|
adam@1538
|
706
|
adam@1538
|
707 return;
|
adam@1538
|
708 }
|
adam@1538
|
709 }
|
adam@1538
|
710
|
adam@1538
|
711 var tbody = document.createElement("tbody");
|
adam@1538
|
712 for (script = table.firstChild; script; script = next) {
|
adam@1538
|
713 next = script.nextSibling;
|
rmbruijn@1597
|
714
|
adam@1538
|
715 tbody.insertBefore(script, tbody.firstChild);
|
adam@1538
|
716 }
|
adam@1538
|
717 table.appendChild(tbody);
|
adam@1538
|
718 }
|
adam@1538
|
719
|
adamc@901
|
720 function dyn(pnode, s) {
|
adamc@1260
|
721 var x = document.createElement("script");
|
adamc@1260
|
722 x.dead = false;
|
adamc@1260
|
723 x.signal = s;
|
adamc@1260
|
724 x.sources = null;
|
adamc@1260
|
725 x.closures = null;
|
adamc@899
|
726
|
adamc@1260
|
727 var firstChild = null;
|
adamc@899
|
728
|
adamc@1260
|
729 x.recreate = function(v) {
|
adamc@1260
|
730 for (var ls = x.closures; ls; ls = ls.next)
|
adamc@1260
|
731 freeClosure(ls.data);
|
adamc@945
|
732
|
adamc@946
|
733 var next;
|
adamc@1260
|
734 for (var child = firstChild; child && child != x; child = next) {
|
adamc@1260
|
735 next = child.nextSibling;
|
adamc@1260
|
736
|
adamc@1260
|
737 killScript(child);
|
adamc@1260
|
738 if (child.getElementsByTagName) {
|
adamc@1260
|
739 var arr = child.getElementsByTagName("script");
|
adamc@1260
|
740 for (var i = 0; i < arr.length; ++i)
|
adamc@1260
|
741 killScript(arr[i]);
|
adamc@1260
|
742 }
|
adamc@1260
|
743
|
adamc@1260
|
744 if (child.parentNode)
|
adamc@1260
|
745 child.parentNode.removeChild(child);
|
adamc@946
|
746 }
|
adamc@903
|
747
|
adamc@1260
|
748 var cls = {v : null};
|
adamc@1260
|
749 var html = flatten(cls, v);
|
adamc@1260
|
750 if (pnode != "table" && pnode != "tr")
|
adamc@1260
|
751 html = dynPrefix + html;
|
adamc@1260
|
752 x.closures = cls.v;
|
adamc@946
|
753
|
adamc@1260
|
754 if (pnode == "table") {
|
adam@1538
|
755 normalizeTable(x.parentNode);
|
adam@1538
|
756
|
adamc@1260
|
757 var dummy = document.createElement("body");
|
adamc@1260
|
758 dummy.innerHTML = "<table>" + html + "</table>";
|
adamc@1260
|
759 runScripts(dummy);
|
adamc@1260
|
760 var table = x.parentNode;
|
adamc@1260
|
761
|
adamc@1260
|
762 if (table) {
|
adamc@1260
|
763 firstChild = null;
|
adamc@1260
|
764 var tbody;
|
adamc@1260
|
765
|
adamc@1260
|
766 var arr = dummy.getElementsByTagName("tbody");
|
adamc@1260
|
767
|
adamc@1260
|
768 var tbody;
|
adamc@1260
|
769 if (arr.length > 0 && arr[0].parentNode == dummy.firstChild) {
|
adamc@1260
|
770 tbody = arr[0];
|
adamc@1260
|
771 var next;
|
adamc@1260
|
772 for (var node = dummy.firstChild.firstChild; node; node = next) {
|
adamc@1260
|
773 next = node.nextSibling;
|
adamc@1260
|
774
|
adamc@1260
|
775 if (node.tagName != "TBODY")
|
adamc@1260
|
776 tbody.appendChild(node);
|
adamc@1260
|
777 }
|
adamc@1260
|
778 } else
|
adamc@1260
|
779 tbody = dummy.firstChild;
|
adamc@1260
|
780
|
adamc@1260
|
781 var next;
|
adamc@1260
|
782 firstChild = document.createElement("script");
|
adamc@1260
|
783 table.insertBefore(firstChild, x);
|
adamc@1260
|
784 for (var node = tbody.firstChild; node; node = next) {
|
adamc@1260
|
785 next = node.nextSibling;
|
adamc@1260
|
786 table.insertBefore(node, x);
|
adamc@1260
|
787 }
|
adamc@1260
|
788 }
|
adamc@1260
|
789 } else if (pnode == "tr") {
|
adamc@1260
|
790 var dummy = document.createElement("body");
|
adamc@1260
|
791 dummy.innerHTML = "<table><tr>" + html + "</tr></table>";
|
adamc@1260
|
792 runScripts(dummy);
|
adamc@1260
|
793 var table = x.parentNode;
|
adamc@1260
|
794
|
adamc@1260
|
795 if (table) {
|
adamc@1260
|
796 var arr = dummy.getElementsByTagName("tr");
|
adamc@1260
|
797 firstChild = null;
|
adamc@1260
|
798 var tr;
|
adamc@1260
|
799 if (arr.length > 0 && table != null)
|
adamc@1260
|
800 tr = arr[0];
|
adamc@1260
|
801 else
|
adamc@1260
|
802 tr = dummy.firstChild;
|
adamc@1260
|
803
|
adamc@1260
|
804 var next;
|
adamc@1260
|
805 firstChild = document.createElement("script");
|
adamc@1260
|
806 table.insertBefore(firstChild, x);
|
adamc@1260
|
807 for (var node = tr.firstChild; node; node = next) {
|
adamc@1260
|
808 next = node.nextSibling;
|
adamc@1260
|
809 table.insertBefore(node, x);
|
adamc@1260
|
810 }
|
adamc@1260
|
811 }
|
adamc@1260
|
812 } else {
|
adamc@1260
|
813 firstChild = document.createElement("span");
|
adamc@1260
|
814 firstChild.innerHTML = html;
|
adamc@1260
|
815 if (x.parentNode)
|
adamc@1260
|
816 x.parentNode.insertBefore(firstChild, x);
|
adam@1650
|
817 runScripts(firstChild);
|
adamc@903
|
818 }
|
adamc@1260
|
819 };
|
adamc@899
|
820
|
adamc@1260
|
821 addNode(x);
|
adamc@1260
|
822 populate(x);
|
adamc@571
|
823 }
|
adamc@582
|
824
|
adam@1463
|
825 function input(x, s, recreate, type, name) {
|
adam@1463
|
826 if (name) x.name = name;
|
adamc@1260
|
827 if (type) x.type = type;
|
adamc@1260
|
828 x.dead = false;
|
adamc@1260
|
829 x.signal = ss(s);
|
adamc@1260
|
830 x.sources = null;
|
adamc@1260
|
831 x.recreate = recreate(x);
|
adamc@1260
|
832 addNode(x);
|
adamc@1260
|
833 populate(x);
|
adamc@816
|
834
|
adamc@1260
|
835 return x;
|
adamc@816
|
836 }
|
adamc@816
|
837
|
adam@1463
|
838 function inp(s, name) {
|
adamc@1260
|
839 var x = input(document.createElement("input"), s,
|
adam@1463
|
840 function(x) { return function(v) { if (x.value != v) x.value = v; }; }, null, name);
|
adamc@1260
|
841 x.value = s.data;
|
adam@1491
|
842 x.onkeyup = x.oninput = x.onchange = x.onpropertychange = function() { sv(s, x.value) };
|
adamc@816
|
843
|
adamc@1260
|
844 return x;
|
adamc@816
|
845 }
|
adamc@816
|
846
|
adam@1479
|
847 function selectValue(x) {
|
adam@1479
|
848 if (x.options.length == 0)
|
adam@1479
|
849 return "";
|
adam@1479
|
850 else
|
adam@1479
|
851 return x.options[x.selectedIndex].value;
|
adam@1479
|
852 }
|
adam@1479
|
853
|
adam@1479
|
854 function setSelectValue(x, v) {
|
adam@1479
|
855 for (var i = 0; i < x.options.length; ++i) {
|
adam@1479
|
856 if(x.options[i].value == v) {
|
adam@1479
|
857 x.selectedIndex = i;
|
adam@1479
|
858 return;
|
adam@1479
|
859 }
|
adam@1479
|
860 }
|
adam@1479
|
861 }
|
adam@1479
|
862
|
adamc@816
|
863 function sel(s, content) {
|
adamc@1260
|
864 var dummy = document.createElement("span");
|
adamc@1260
|
865 dummy.innerHTML = "<select>" + content + "</select>";
|
adam@1479
|
866 var x = input(dummy.firstChild, s, function(x) { return function(v) { if (selectValue(x) != v) setSelectValue(x, v); }; });
|
adam@1479
|
867
|
adam@1479
|
868 for (var i = 0; i < x.options.length; ++i) {
|
adam@1479
|
869 if (x.options[i].value == "")
|
adam@1479
|
870 x.options[i].value = x.options[i].text;
|
adam@1479
|
871 else
|
adam@1479
|
872 x.options[i].value = x.options[i].value.substring(1);
|
adam@1479
|
873 }
|
adam@1479
|
874
|
adam@1479
|
875 setSelectValue(x, s.data);
|
adam@1479
|
876 if (selectValue(x) != s.data)
|
adam@1479
|
877 sv(s, selectValue(x));
|
adam@1479
|
878 x.onchange = function() { sv(s, selectValue(x)) };
|
adamc@797
|
879
|
adamc@1260
|
880 return x;
|
adamc@598
|
881 }
|
adamc@598
|
882
|
adamc@817
|
883 function chk(s) {
|
adamc@1260
|
884 var x = input(document.createElement("input"), s,
|
adamc@1260
|
885 function(x) { return function(v) { if (x.checked != v) x.checked = v; }; }, "checkbox");
|
adamc@1260
|
886 x.defaultChecked = x.checked = s.data;
|
adam@1610
|
887 x.onclick = x.onkeyup = x.oninput = x.onchange = x.onpropertychange = function() { sv(s, x.checked) };
|
adamc@817
|
888
|
adamc@1260
|
889 return x;
|
adamc@817
|
890 }
|
adamc@817
|
891
|
adamc@1099
|
892 function tbx(s) {
|
adamc@1260
|
893 var x = input(document.createElement("textarea"), s,
|
adamc@1260
|
894 function(x) { return function(v) { if (x.innerHTML != v) x.innerHTML = v; }; });
|
adamc@1260
|
895 x.innerHTML = s.data;
|
adam@1491
|
896 x.onkeyup = x.oninput = x.onchange = x.onpropertychange = function() { sv(s, x.value) };
|
adamc@1099
|
897
|
adamc@1260
|
898 return x;
|
adamc@1099
|
899 }
|
adamc@1099
|
900
|
adam@1643
|
901 function dynClass(html, s) {
|
adam@1643
|
902 var htmlCls = {v : null};
|
adam@1643
|
903 html = flatten(htmlCls, html);
|
adam@1643
|
904 htmlCls = htmlCls.v;
|
adam@1643
|
905
|
adam@1643
|
906 var dummy = document.createElement("body");
|
adam@1643
|
907 dummy.innerHTML = html;
|
adam@1643
|
908 runScripts(dummy);
|
adam@1643
|
909 var html = dummy.firstChild;
|
adam@1643
|
910 dummy.removeChild(html);
|
adam@1643
|
911 addNode(html);
|
adam@1643
|
912
|
adam@1643
|
913 var x = document.createElement("script");
|
adam@1643
|
914 x.dead = false;
|
adam@1643
|
915 x.signal = s;
|
adam@1643
|
916 x.sources = null;
|
adam@1643
|
917 x.closures = htmlCls;
|
adam@1643
|
918
|
adam@1643
|
919 x.recreate = function(v) {
|
adam@1643
|
920 for (var ls = x.closures; ls != htmlCls; ls = ls.next)
|
adam@1643
|
921 freeClosure(ls.data);
|
adam@1643
|
922
|
adam@1643
|
923 var cls = {v : null};
|
adam@1643
|
924 html.className = flatten(cls, v);
|
adam@1643
|
925 x.closures = concat(cls.v, htmlCls);
|
adam@1643
|
926 };
|
adam@1643
|
927
|
adam@1643
|
928 addNode(x);
|
adam@1643
|
929 populate(x);
|
adam@1643
|
930 }
|
adam@1643
|
931
|
adamc@800
|
932 function addOnChange(x, f) {
|
adamc@1260
|
933 var old = x.onchange;
|
adam@1290
|
934 if (old == null)
|
adam@1290
|
935 x.onchange = f;
|
adam@1290
|
936 else
|
adam@1290
|
937 x.onchange = function() { old(); f(); };
|
adam@1290
|
938 }
|
adam@1290
|
939
|
adam@1290
|
940 function addOnKeyUp(x, f) {
|
adam@1290
|
941 var old = x.onkeyup;
|
adam@1290
|
942 if (old == null)
|
adam@1290
|
943 x.onkeyup = f;
|
adam@1290
|
944 else
|
adam@1290
|
945 x.onkeyup = function(x) { old(x); f(x); };
|
adamc@800
|
946 }
|
adamc@800
|
947
|
adamc@692
|
948
|
adamc@692
|
949 // Basic string operations
|
adamc@692
|
950
|
adamc@597
|
951 function eh(x) {
|
adamc@1260
|
952 if (x == null)
|
adamc@1260
|
953 return "NULL";
|
adamc@1260
|
954 else
|
adamc@1260
|
955 return x.split("&").join("&").split("<").join("<").split(">").join(">");
|
adamc@597
|
956 }
|
adamc@597
|
957
|
adamc@582
|
958 function ts(x) { return x.toString() }
|
adamc@586
|
959 function bs(b) { return (b ? "True" : "False") }
|
adamc@586
|
960
|
adamc@1057
|
961 function id(x) { return x; }
|
adamc@1038
|
962 function sub(s, i) { return s.charAt(i); }
|
adamc@828
|
963 function suf(s, i) { return s.substring(i); }
|
adamc@828
|
964 function slen(s) { return s.length; }
|
adamc@829
|
965 function sidx(s, ch) {
|
adamc@1260
|
966 var r = s.indexOf(ch);
|
adamc@1260
|
967 if (r == -1)
|
adamc@1260
|
968 return null;
|
adamc@1260
|
969 else
|
adamc@1260
|
970 return r;
|
adamc@829
|
971 }
|
adamc@831
|
972 function sspn(s, chs) {
|
adamc@1260
|
973 for (var i = 0; i < s.length; ++i)
|
adamc@1260
|
974 if (chs.indexOf(s.charAt(i)) != -1)
|
adamc@1260
|
975 return i;
|
adamc@831
|
976
|
adamc@1260
|
977 return null;
|
adamc@831
|
978 }
|
adamc@829
|
979 function schr(s, ch) {
|
adamc@1260
|
980 var r = s.indexOf(ch);
|
adamc@1260
|
981 if (r == -1)
|
adamc@1260
|
982 return null;
|
adamc@1260
|
983 else
|
adamc@1260
|
984 return s.substring(r);
|
adamc@829
|
985 }
|
adamc@829
|
986 function ssub(s, start, len) {
|
adamc@1260
|
987 return s.substring(start, start+len);
|
adamc@829
|
988 }
|
adam@1624
|
989 function strlenGe(s, len) {
|
adam@1624
|
990 return s.length >= len;
|
adam@1624
|
991 }
|
adamc@821
|
992
|
adam@1621
|
993 function trimZeroes(s) {
|
adam@1621
|
994 for (var i = 0; i < s.length; ++i)
|
adam@1632
|
995 if (s.charAt(i) != '0') {
|
adam@1621
|
996 if (i > 0)
|
adam@1621
|
997 return s.substring(i);
|
adam@1621
|
998 else
|
adam@1621
|
999 return s;
|
adam@1621
|
1000 }
|
adam@1621
|
1001
|
adam@1631
|
1002 if (s.length == 0)
|
adam@1621
|
1003 return s;
|
adam@1621
|
1004 else
|
adam@1631
|
1005 return "0";
|
adam@1621
|
1006 }
|
adam@1621
|
1007
|
adamc@649
|
1008 function pi(s) {
|
adam@1631
|
1009 var st = trimZeroes(s);
|
adam@1631
|
1010 var r = parseInt(st);
|
adam@1631
|
1011 if (r.toString() == st)
|
adamc@1260
|
1012 return r;
|
adamc@1260
|
1013 else
|
adamc@1260
|
1014 er("Can't parse int: " + s);
|
adamc@649
|
1015 }
|
adamc@649
|
1016
|
adamc@649
|
1017 function pfl(s) {
|
adamc@1260
|
1018 var r = parseFloat(s);
|
adamc@1260
|
1019 if (r.toString() == s)
|
adamc@1260
|
1020 return r;
|
adamc@1260
|
1021 else
|
adamc@1260
|
1022 er("Can't parse float: " + s);
|
adamc@649
|
1023 }
|
adamc@649
|
1024
|
adamc@838
|
1025 function pio(s) {
|
adam@1631
|
1026 var st = trimZeroes(s);
|
adam@1631
|
1027 var r = parseInt(st);
|
adam@1631
|
1028 if (r.toString() == st)
|
adamc@1260
|
1029 return r;
|
adamc@1260
|
1030 else
|
adamc@1260
|
1031 return null;
|
adamc@838
|
1032 }
|
adamc@838
|
1033
|
adamc@838
|
1034 function pflo(s) {
|
adamc@1260
|
1035 var r = parseFloat(s);
|
adamc@1260
|
1036 if (r.toString() == s)
|
adamc@1260
|
1037 return r;
|
adamc@1260
|
1038 else
|
adamc@1260
|
1039 return null;
|
adamc@838
|
1040 }
|
adamc@838
|
1041
|
adam@1620
|
1042 function parseSource(s1, s2) {
|
adam@1620
|
1043 return eval("s" + s1 + "_" + s2);
|
adam@1620
|
1044 }
|
adam@1620
|
1045
|
adamc@692
|
1046 function uf(s) {
|
adamc@1260
|
1047 if (s.length == 0)
|
adamc@1260
|
1048 return "_";
|
adamc@1260
|
1049 s = s.replace(new RegExp ("\\.", "g"), ".2E");
|
adamc@1260
|
1050 return (s.charAt(0) == '_' ? "_" : "") + encodeURIComponent(s).replace(new RegExp ("%", "g"), ".");
|
adamc@691
|
1051 }
|
adamc@691
|
1052
|
adamc@692
|
1053 function uu(s) {
|
adamc@1260
|
1054 if (s.length > 0 && s.charAt(0) == '_') {
|
adamc@1260
|
1055 s = s.substring(1);
|
adamc@1260
|
1056 } else if (s.length >= 3 && (s.charAt(0) == '%' || s.charAt(0) == '.')
|
adamc@1260
|
1057 && s.charAt(1) == '5' && (s.charAt(2) == 'f' || s.charAt(2) == 'F'))
|
adamc@1260
|
1058 s = s.substring(3);
|
adamc@1260
|
1059 s = s.replace(new RegExp ("\\+", "g"), " ");
|
adamc@1260
|
1060 s = s.replace(new RegExp ("\\.", "g"), "%");
|
adamc@1260
|
1061 return decodeURIComponent(s);
|
adamc@905
|
1062 }
|
adamc@905
|
1063
|
adamc@1108
|
1064 function atr(s) {
|
adamc@1260
|
1065 return s.replace(new RegExp ("\"", "g"), """).replace(new RegExp ("&", "g"), "&")
|
adamc@1260
|
1066 }
|
adamc@1108
|
1067
|
adamc@912
|
1068 function ub(b) {
|
adamc@1260
|
1069 return b ? "1" : "0";
|
adamc@912
|
1070 }
|
adamc@912
|
1071
|
adamc@905
|
1072 function uul(getToken, getData) {
|
adamc@1260
|
1073 var tok = getToken();
|
adamc@1260
|
1074 if (tok == "Nil") {
|
adamc@1260
|
1075 return null;
|
adamc@1260
|
1076 } else if (tok == "Cons") {
|
adamc@1260
|
1077 var d = getData();
|
adamc@1260
|
1078 var l = uul(getToken, getData);
|
adamc@1260
|
1079 return {_1:d, _2:l};
|
adamc@1260
|
1080 } else
|
adamc@1260
|
1081 whine("Can't unmarshal list (" + tok + ")");
|
adamc@692
|
1082 }
|
adamc@692
|
1083
|
adamc@961
|
1084 function strcmp(str1, str2) {
|
adamc@1260
|
1085 return ((str1 == str2) ? 0 : ((str1 > str2) ? 1 : -1));
|
adamc@961
|
1086 }
|
adamc@692
|
1087
|
adamc@603
|
1088
|
adamc@692
|
1089 // Remote calls
|
adamc@609
|
1090
|
adamc@703
|
1091 var client_id = null;
|
adamc@668
|
1092 var client_pass = 0;
|
adamc@668
|
1093 var url_prefix = "/";
|
adamc@673
|
1094 var timeout = 60;
|
adam@1552
|
1095 var isPost = false;
|
adamc@668
|
1096
|
adamc@668
|
1097 function getXHR(uri)
|
adamc@609
|
1098 {
|
adamc@609
|
1099 try {
|
adamc@1260
|
1100 return new XMLHttpRequest();
|
adamc@609
|
1101 } catch (e) {
|
adamc@1260
|
1102 try {
|
adamc@1260
|
1103 return new ActiveXObject("Msxml2.XMLHTTP");
|
adamc@1260
|
1104 } catch (e) {
|
adamc@1260
|
1105 try {
|
adamc@1260
|
1106 return new ActiveXObject("Microsoft.XMLHTTP");
|
adamc@1260
|
1107 } catch (e) {
|
adamc@1260
|
1108 whine("Your browser doesn't seem to support AJAX.");
|
adamc@1260
|
1109 }
|
adamc@1260
|
1110 }
|
adamc@609
|
1111 }
|
adamc@609
|
1112 }
|
adamc@609
|
1113
|
adamc@736
|
1114 var sig = null;
|
adamc@736
|
1115
|
adamc@1042
|
1116 var unloading = false, inFlight = null;
|
adamc@1042
|
1117
|
adamc@1042
|
1118 function unload() {
|
adamc@1260
|
1119 unloading = true;
|
adamc@1042
|
1120
|
adamc@1260
|
1121 for (; inFlight; inFlight = inFlight.next) {
|
adamc@1260
|
1122 inFlight.data.abort();
|
adamc@1260
|
1123 }
|
adamc@1042
|
1124 }
|
adamc@1042
|
1125
|
adamc@736
|
1126 function requestUri(xhr, uri, needsSig) {
|
adamc@1260
|
1127 if (unloading)
|
adamc@1260
|
1128 return;
|
adamc@1042
|
1129
|
adamc@1260
|
1130 xhr.open("POST", uri, true);
|
adamc@1260
|
1131 xhr.setRequestHeader("Content-type", "text/plain");
|
adam@1549
|
1132 try {
|
adam@1549
|
1133 xhr.setRequestHeader("Content-length", "0");
|
adam@1549
|
1134 xhr.setRequestHeader("Connection", "close");
|
adam@1549
|
1135 } catch (e) { }
|
adamc@668
|
1136
|
adamc@1260
|
1137 if (client_id != null) {
|
adamc@1260
|
1138 xhr.setRequestHeader("UrWeb-Client", client_id.toString());
|
adamc@1260
|
1139 xhr.setRequestHeader("UrWeb-Pass", client_pass.toString());
|
adamc@1260
|
1140 }
|
adamc@668
|
1141
|
adamc@1260
|
1142 if (needsSig) {
|
adamc@1260
|
1143 if (sig == null)
|
adamc@1260
|
1144 whine("Missing cookie signature!");
|
adamc@736
|
1145
|
adamc@1260
|
1146 xhr.setRequestHeader("UrWeb-Sig", sig);
|
adamc@1260
|
1147 }
|
adamc@736
|
1148
|
adamc@1260
|
1149 inFlight = cons(xhr, inFlight);
|
adamc@1260
|
1150 xhr.send(null);
|
adamc@668
|
1151 }
|
adamc@668
|
1152
|
adamc@1042
|
1153 function xhrFinished(xhr) {
|
adamc@1260
|
1154 xhr.abort();
|
adamc@1260
|
1155 inFlight = remove(xhr, inFlight);
|
adamc@1042
|
1156 }
|
adamc@1042
|
1157
|
adamc@1111
|
1158 function unurlify(parse, s) {
|
adamc@1260
|
1159 return parse(s);
|
adamc@1111
|
1160 }
|
adamc@1111
|
1161
|
adam@1385
|
1162 function redirect(s) {
|
adam@1385
|
1163 window.location = s;
|
adam@1385
|
1164 }
|
adam@1385
|
1165
|
adamc@978
|
1166 function rc(prefix, uri, parse, k, needsSig) {
|
adamc@1260
|
1167 uri = cat(prefix, uri);
|
adamc@1260
|
1168 uri = flattenLocal(uri);
|
adamc@1260
|
1169 var xhr = getXHR();
|
adamc@609
|
1170
|
adamc@1260
|
1171 xhr.onreadystatechange = function() {
|
adamc@1260
|
1172 if (xhr.readyState == 4) {
|
adamc@1260
|
1173 var isok = false;
|
adamc@612
|
1174
|
adamc@1260
|
1175 try {
|
adamc@1260
|
1176 if (xhr.status == 200)
|
adamc@1260
|
1177 isok = true;
|
adamc@1260
|
1178 } catch (e) { }
|
adamc@612
|
1179
|
adamc@1260
|
1180 if (isok) {
|
adam@1446
|
1181 var lines = xhr.responseText.split("\n");
|
adam@1446
|
1182 if (lines.length != 2)
|
adam@1446
|
1183 whine("Bad RPC response lines");
|
adam@1446
|
1184
|
adam@1446
|
1185 eval(lines[0]);
|
adam@1446
|
1186
|
adamc@1260
|
1187 try {
|
adam@1446
|
1188 k(parse(lines[1]));
|
adamc@1260
|
1189 } catch (v) {
|
adamc@1260
|
1190 doExn(v);
|
adamc@1260
|
1191 }
|
adamc@1260
|
1192 } else {
|
adamc@1260
|
1193 conn();
|
adamc@1260
|
1194 }
|
adamc@1260
|
1195
|
adamc@1260
|
1196 xhrFinished(xhr);
|
adamc@729
|
1197 }
|
adamc@1260
|
1198 };
|
adamc@1042
|
1199
|
adamc@1260
|
1200 requestUri(xhr, uri, needsSig);
|
adamc@609
|
1201 }
|
adamc@667
|
1202
|
adamc@667
|
1203 function path_join(s1, s2) {
|
adamc@1260
|
1204 if (s1.length > 0 && s1.charAt(s1.length-1) == '/')
|
adamc@1260
|
1205 return s1 + s2;
|
adamc@1260
|
1206 else
|
adamc@1260
|
1207 return s1 + "/" + s2;
|
adamc@667
|
1208 }
|
adamc@667
|
1209
|
adamc@670
|
1210 var channels = [];
|
adamc@670
|
1211
|
adamc@670
|
1212 function newQueue() {
|
adamc@1260
|
1213 return { front : null, back : null };
|
adamc@670
|
1214 }
|
adamc@670
|
1215 function enqueue(q, v) {
|
adamc@1260
|
1216 if (q.front == null) {
|
adamc@1260
|
1217 q.front = cons(v, null);
|
adamc@1260
|
1218 q.back = q.front;
|
adamc@1260
|
1219 } else {
|
adamc@1260
|
1220 var node = cons(v, null);
|
adamc@1260
|
1221 q.back.next = node;
|
adamc@1260
|
1222 q.back = node;
|
adamc@1260
|
1223 }
|
adamc@670
|
1224 }
|
adamc@670
|
1225 function dequeue(q) {
|
adamc@670
|
1226 if (q.front == null)
|
adamc@1260
|
1227 return null;
|
adamc@1260
|
1228 else {
|
adamc@1260
|
1229 var r = q.front.data;
|
adamc@1260
|
1230 q.front = q.front.next;
|
adamc@1260
|
1231 if (q.front == null)
|
adamc@1260
|
1232 q.back = null;
|
adamc@1260
|
1233 return r;
|
adamc@1260
|
1234 }
|
adamc@670
|
1235 }
|
adamc@670
|
1236
|
adamc@670
|
1237 function newChannel() {
|
adamc@1260
|
1238 return { msgs : newQueue(), listeners : newQueue() };
|
adamc@670
|
1239 }
|
adamc@670
|
1240
|
adamc@667
|
1241 function listener() {
|
adamc@1260
|
1242 var uri = path_join(url_prefix, ".msgs");
|
adamc@1260
|
1243 var xhr = getXHR();
|
adamc@1260
|
1244 var tid, orsc, onTimeout;
|
adamc@673
|
1245
|
adamc@1260
|
1246 var connect = function () {
|
adamc@1260
|
1247 xhr.onreadystatechange = orsc;
|
adamc@1260
|
1248 tid = window.setTimeout(onTimeout, timeout * 500);
|
adamc@1260
|
1249 requestUri(xhr, uri, false);
|
adamc@1260
|
1250 }
|
adamc@673
|
1251
|
adamc@1260
|
1252 orsc = function() {
|
adamc@1260
|
1253 if (xhr.readyState == 4) {
|
adamc@1260
|
1254 window.clearTimeout(tid);
|
adamc@673
|
1255
|
adamc@1260
|
1256 var isok = false;
|
adamc@667
|
1257
|
adamc@1260
|
1258 try {
|
adamc@1260
|
1259 if (xhr.status == 200)
|
adamc@1260
|
1260 isok = true;
|
adamc@1260
|
1261 } catch (e) { }
|
adamc@667
|
1262
|
adamc@1260
|
1263 if (isok) {
|
adam@1447
|
1264 var text = xhr.responseText;
|
adamc@1260
|
1265 if (text == "")
|
adamc@1260
|
1266 return;
|
adamc@1260
|
1267 var lines = text.split("\n");
|
adamc@889
|
1268
|
adam@1552
|
1269 if (lines.length == 1 && lines[0] == "R") {
|
adam@1553
|
1270 lameDuck = true;
|
adam@1553
|
1271
|
adam@1552
|
1272 if (isPost)
|
adam@1552
|
1273 history.back();
|
adam@1552
|
1274 else
|
adam@1552
|
1275 location.reload();
|
adam@1552
|
1276
|
adam@1552
|
1277 return;
|
adam@1552
|
1278 }
|
adam@1552
|
1279
|
adamc@1260
|
1280 if (lines.length < 2) {
|
adamc@1260
|
1281 discon();
|
adamc@1260
|
1282 return;
|
adamc@1260
|
1283 }
|
adamc@1260
|
1284
|
adam@1447
|
1285 var messageReader = function(i) {
|
adam@1447
|
1286 if (i+1 >= lines.length) {
|
adam@1447
|
1287 xhrFinished(xhr);
|
adam@1447
|
1288 connect();
|
adam@1447
|
1289 }
|
adam@1447
|
1290 else {
|
adam@1447
|
1291 var chn = lines[i];
|
adam@1447
|
1292 var msg = lines[i+1];
|
adamc@1260
|
1293
|
adam@1447
|
1294 if (chn == "E") {
|
adam@1447
|
1295 eval(msg);
|
adam@1447
|
1296 window.setTimeout(function() { messageReader(i+2); }, 0);
|
adam@1447
|
1297 } else {
|
adam@1447
|
1298 if (chn < 0)
|
adam@1447
|
1299 whine("Out-of-bounds channel in message from remote server");
|
adamc@1260
|
1300
|
adam@1447
|
1301 var ch;
|
adamc@1260
|
1302
|
adam@1447
|
1303 if (chn >= channels.length || channels[chn] == null) {
|
adam@1447
|
1304 ch = newChannel();
|
adam@1447
|
1305 channels[chn] = ch;
|
adam@1447
|
1306 } else
|
adam@1447
|
1307 ch = channels[chn];
|
adamc@1260
|
1308
|
adam@1447
|
1309 var listener = dequeue(ch.listeners);
|
adam@1447
|
1310 if (listener == null) {
|
adam@1447
|
1311 enqueue(ch.msgs, msg);
|
adam@1447
|
1312 } else {
|
adam@1447
|
1313 try {
|
adam@1447
|
1314 listener(msg);
|
adam@1447
|
1315 } catch (v) {
|
adam@1447
|
1316 doExn(v);
|
adam@1447
|
1317 }
|
adam@1447
|
1318 }
|
adam@1447
|
1319
|
adam@1447
|
1320 messageReader(i+2);
|
adamc@1260
|
1321 }
|
adamc@1260
|
1322 }
|
adamc@1260
|
1323 }
|
adamc@1260
|
1324
|
adam@1447
|
1325 messageReader(0);
|
adamc@1260
|
1326 }
|
adamc@1260
|
1327 else {
|
adamc@1260
|
1328 try {
|
adamc@1260
|
1329 if (xhr.status != 0)
|
adamc@1260
|
1330 servErr("Error querying remote server for messages: " + xhr.status);
|
adamc@1260
|
1331 } catch (e) { }
|
adamc@1260
|
1332 }
|
adamc@729
|
1333 }
|
adamc@1260
|
1334 };
|
adamc@669
|
1335
|
adamc@1260
|
1336 onTimeout = function() {
|
adamc@1260
|
1337 xhrFinished(xhr);
|
adamc@1260
|
1338 connect();
|
adamc@1260
|
1339 };
|
adamc@670
|
1340
|
adamc@673
|
1341 connect();
|
adamc@667
|
1342 }
|
adamc@670
|
1343
|
adamc@670
|
1344 function rv(chn, parse, k) {
|
adamc@1260
|
1345 if (chn == null)
|
adamc@1260
|
1346 return;
|
adamc@682
|
1347
|
adamc@1260
|
1348 if (chn < 0)
|
adamc@1260
|
1349 whine("Out-of-bounds channel receive");
|
adamc@670
|
1350
|
adamc@1260
|
1351 var ch;
|
adamc@670
|
1352
|
adamc@1260
|
1353 if (chn >= channels.length || channels[chn] == null) {
|
adamc@1260
|
1354 ch = newChannel();
|
adamc@1260
|
1355 channels[chn] = ch;
|
adamc@1260
|
1356 } else
|
adamc@1260
|
1357 ch = channels[chn];
|
adamc@670
|
1358
|
adamc@1260
|
1359 var msg = dequeue(ch.msgs);
|
adamc@1260
|
1360 if (msg == null) {
|
adamc@1260
|
1361 enqueue(ch.listeners, function(msg) { k(parse(msg)); });
|
adamc@1260
|
1362 } else {
|
adamc@1260
|
1363 try {
|
adamc@1260
|
1364 k(parse(msg));
|
adamc@1260
|
1365 } catch (v) {
|
adamc@1260
|
1366 doExn(v);
|
adamc@1260
|
1367 }
|
adamc@728
|
1368 }
|
adamc@670
|
1369 }
|
adamc@693
|
1370
|
adamc@978
|
1371 function sl(ms, k) {
|
adamc@1260
|
1372 window.setTimeout(function() { k(null); }, ms);
|
adamc@1021
|
1373 }
|
adamc@1021
|
1374
|
adamc@1021
|
1375 function sp(e) {
|
adamc@1260
|
1376 execF(e, null);
|
adamc@978
|
1377 }
|
adamc@978
|
1378
|
adamc@693
|
1379
|
adamc@970
|
1380 // The Ur interpreter
|
adamc@970
|
1381
|
adamc@970
|
1382 var urfuncs = [];
|
adamc@970
|
1383
|
adamc@970
|
1384 function lookup(env, n) {
|
adamc@1260
|
1385 while (env != null) {
|
adamc@1260
|
1386 if (n == 0)
|
adamc@1260
|
1387 return env.data;
|
adamc@1260
|
1388 else {
|
adamc@1260
|
1389 --n;
|
adamc@1260
|
1390 env = env.next;
|
adamc@1260
|
1391 }
|
adamc@970
|
1392 }
|
adamc@970
|
1393
|
adamc@1260
|
1394 whine("Out-of-bounds Ur variable reference");
|
adamc@970
|
1395 }
|
adamc@970
|
1396
|
adamc@974
|
1397 function execP(env, p, v) {
|
adamc@1260
|
1398 switch (p.c) {
|
adamc@1260
|
1399 case "w":
|
adamc@1260
|
1400 return env;
|
adamc@1260
|
1401 case "v":
|
adamc@1260
|
1402 return cons(v, env);
|
adamc@1260
|
1403 case "c":
|
adamc@1260
|
1404 if (v == p.v)
|
adamc@1260
|
1405 return env;
|
adamc@1260
|
1406 else
|
adamc@1260
|
1407 return false;
|
adamc@1260
|
1408 case "s":
|
adamc@1260
|
1409 if (v == null)
|
adamc@1260
|
1410 return false;
|
adamc@1260
|
1411 else
|
adamc@1260
|
1412 return execP(env, p.p, p.n ? v.v : v);
|
adamc@1260
|
1413 case "1":
|
adamc@1260
|
1414 if (v.n != p.n)
|
adamc@1260
|
1415 return false;
|
adamc@1260
|
1416 else
|
adamc@1260
|
1417 return execP(env, p.p, v.v);
|
adamc@1260
|
1418 case "r":
|
adamc@1260
|
1419 for (var fs = p.l; fs != null; fs = fs.next) {
|
adamc@1260
|
1420 env = execP(env, fs.data.p, v["_" + fs.data.n]);
|
adamc@1260
|
1421 if (env == false)
|
adamc@1260
|
1422 return false;
|
adamc@1260
|
1423 }
|
adamc@1260
|
1424 return env;
|
adamc@1260
|
1425 default:
|
adamc@1260
|
1426 whine("Unknown Ur pattern kind" + p.c);
|
adamc@974
|
1427 }
|
adamc@974
|
1428 }
|
adamc@974
|
1429
|
adamc@970
|
1430 function exec0(env, e) {
|
adamc@1260
|
1431 return exec1(env, null, e);
|
adamc@1020
|
1432 }
|
adamc@1020
|
1433
|
adamc@1020
|
1434 function exec1(env, stack, e) {
|
adamc@1260
|
1435 var stack, usedK = false;
|
adamc@970
|
1436
|
adamc@1260
|
1437 var saveEnv = function() {
|
adamc@1260
|
1438 if (stack.next != null && stack.next.data.c != "<")
|
adamc@1260
|
1439 stack = cons({c: "<", env: env}, stack.next);
|
adamc@1260
|
1440 else
|
adamc@1260
|
1441 stack = stack.next;
|
adamc@1260
|
1442 };
|
adamc@983
|
1443
|
adamc@1260
|
1444 while (true) {
|
adamc@1260
|
1445 switch (e.c) {
|
adamc@1260
|
1446 case "c":
|
adamc@1260
|
1447 var v = e.v;
|
adamc@1260
|
1448 if (stack == null)
|
adamc@1260
|
1449 return v;
|
adamc@1260
|
1450 var fr = stack.data;
|
adamc@970
|
1451
|
adamc@1260
|
1452 switch (fr.c) {
|
adamc@1260
|
1453 case "s":
|
adamc@1260
|
1454 e = {c: "c", v: {v: v}};
|
adamc@1260
|
1455 stack = stack.next;
|
adamc@1260
|
1456 break;
|
adamc@1260
|
1457 case "1":
|
adamc@1260
|
1458 e = {c: "c", v: {n: fr.n, v: v}};
|
adamc@1260
|
1459 stack = stack.next;
|
adamc@1260
|
1460 break;
|
adamc@1260
|
1461 case "f":
|
adamc@1260
|
1462 fr.args[fr.pos++] = v;
|
adamc@1260
|
1463 if (fr.a == null) {
|
adamc@1260
|
1464 var res;
|
adamc@1260
|
1465 stack = stack.next;
|
adamc@1035
|
1466
|
adamc@1260
|
1467 if (fr.f.apply)
|
adamc@1260
|
1468 res = fr.f.apply(null, fr.args);
|
adamc@1260
|
1469 else if (fr.args.length == 0)
|
adamc@1260
|
1470 res = fr.f();
|
adamc@1260
|
1471 else if (fr.args.length == 1)
|
adamc@1260
|
1472 res = fr.f(fr.args[0]);
|
adamc@1260
|
1473 else if (fr.args.length == 2)
|
adamc@1260
|
1474 res = fr.f(fr.args[0], fr.args[1]);
|
adamc@1260
|
1475 else if (fr.args.length == 3)
|
adamc@1260
|
1476 res = fr.f(fr.args[0], fr.args[1], fr.args[2]);
|
adamc@1260
|
1477 else if (fr.args.length == 4)
|
adamc@1260
|
1478 res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3]);
|
adamc@1260
|
1479 else if (fr.args.length == 5)
|
adamc@1260
|
1480 res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3], fr.args[4]);
|
adamc@1260
|
1481 else
|
adamc@1260
|
1482 whine("Native function has " + fr.args.length + " args, but there is no special case for that count.");
|
adamc@1035
|
1483
|
adamc@1260
|
1484 e = {c: "c", v: res};
|
adamc@1260
|
1485 if (usedK) return null;
|
adamc@1260
|
1486 } else {
|
adamc@1260
|
1487 e = fr.a.data;
|
adamc@1260
|
1488 fr.a = fr.a.next;
|
adamc@1260
|
1489 }
|
adamc@1260
|
1490 break;
|
adamc@1260
|
1491 case "a1":
|
adamc@1260
|
1492 e = fr.x;
|
adamc@1260
|
1493 stack = cons({c: "a2", f: v}, stack.next);
|
adamc@1260
|
1494 break;
|
adamc@1260
|
1495 case "a2":
|
adamc@1260
|
1496 if (fr.f == null)
|
adamc@1260
|
1497 whine("Ur: applying null function");
|
adamc@1260
|
1498 else if (fr.f.body) {
|
adamc@1260
|
1499 saveEnv();
|
adamc@1260
|
1500 env = cons(v, fr.f.env);
|
adamc@1260
|
1501 e = fr.f.body;
|
adamc@1260
|
1502 } else {
|
adamc@1260
|
1503 e = {c: "c", v: fr.f(v)};
|
adamc@1260
|
1504 stack = stack.next;
|
adamc@1260
|
1505 }
|
adamc@1260
|
1506 break;
|
adamc@1260
|
1507 case "<":
|
adamc@1260
|
1508 env = fr.env;
|
adamc@1260
|
1509 stack = stack.next;
|
adamc@1260
|
1510 break;
|
adamc@1260
|
1511 case "r":
|
adamc@1260
|
1512 fr.fs["_" + fr.n] = v;
|
adamc@1260
|
1513 if (fr.l == null) {
|
adamc@1260
|
1514 e = {c: "c", v: fr.fs};
|
adamc@1260
|
1515 stack = stack.next;
|
adamc@1260
|
1516 } else {
|
adamc@1260
|
1517 fr.n = fr.l.data.n;
|
adamc@1260
|
1518 e = fr.l.data.v;
|
adamc@1260
|
1519 fr.l = fr.l.next;
|
adamc@1260
|
1520 }
|
adamc@1260
|
1521 break;
|
adamc@1260
|
1522 case ".":
|
adamc@1260
|
1523 e = {c: "c", v: v["_" + fr.f]};
|
adamc@1260
|
1524 stack = stack.next;
|
adamc@1260
|
1525 break;
|
adamc@1260
|
1526 case ";":
|
adamc@1260
|
1527 e = fr.e2;
|
adamc@1260
|
1528 stack = stack.next;
|
adamc@1260
|
1529 break;
|
adamc@1260
|
1530 case "=":
|
adamc@1260
|
1531 saveEnv();
|
adamc@1260
|
1532 env = cons(v, env);
|
adamc@1260
|
1533 e = fr.e2;
|
adamc@1260
|
1534 break;
|
adamc@1260
|
1535 case "m":
|
adamc@1260
|
1536 var ps;
|
adamc@1260
|
1537 for (ps = fr.p; ps != null; ps = ps.next) {
|
adamc@1260
|
1538 var r = execP(env, ps.data.p, v);
|
adamc@1260
|
1539 if (r != false) {
|
adamc@1260
|
1540 saveEnv();
|
adamc@1260
|
1541 env = r;
|
adamc@1260
|
1542 e = ps.data.b;
|
adamc@1260
|
1543 break;
|
adamc@1260
|
1544 }
|
adamc@1260
|
1545 }
|
adamc@1260
|
1546 if (ps == null)
|
adamc@1260
|
1547 whine("Match failure in Ur interpretation");
|
adamc@1260
|
1548 break;
|
adamc@1260
|
1549 default:
|
adamc@1260
|
1550 whine("Unknown Ur continuation kind " + fr.c);
|
adamc@1260
|
1551 }
|
adamc@1260
|
1552
|
adamc@1260
|
1553 break;
|
adamc@1260
|
1554 case "v":
|
adamc@1260
|
1555 e = {c: "c", v: lookup(env, e.n)};
|
adamc@1260
|
1556 break;
|
adamc@1260
|
1557 case "n":
|
adamc@1260
|
1558 var idx = e.n;
|
adamc@1260
|
1559 e = urfuncs[idx];
|
adamc@1260
|
1560 if (e.c == "t")
|
adamc@1262
|
1561 e = urfuncs[idx] = eval("(" + e.f + ")");
|
adamc@1260
|
1562 break;
|
adamc@1260
|
1563 case "s":
|
adamc@1260
|
1564 stack = cons({c: "s"}, stack);
|
adamc@1260
|
1565 e = e.v;
|
adamc@1260
|
1566 break;
|
adamc@1260
|
1567 case "1":
|
adamc@1260
|
1568 stack = cons({c: "1", n: e.n}, stack);
|
adamc@1260
|
1569 e = e.v;
|
adamc@1260
|
1570 break;
|
adamc@1260
|
1571 case "f":
|
adamc@1260
|
1572 if (e.a == null)
|
adamc@1260
|
1573 e = {c: "c", v: (eval(e.f))()};
|
adamc@1260
|
1574 else {
|
adamc@1260
|
1575 var args = [];
|
adamc@1260
|
1576 stack = cons({c: "f", f: eval(e.f), args: args, pos: 0, a: e.a.next}, stack);
|
adamc@1260
|
1577 if (!e.a.data.c) alert("[2] fr.f = " + e.f + "; 0 = " + e.a.data);
|
adamc@1260
|
1578 e = e.a.data;
|
adamc@1260
|
1579 }
|
adamc@1260
|
1580 break;
|
adamc@1260
|
1581 case "l":
|
adamc@1260
|
1582 e = {c: "c", v: {env: env, body: e.b}};
|
adamc@1260
|
1583 break;
|
adamc@1260
|
1584 case "a":
|
adamc@1260
|
1585 stack = cons({c: "a1", x: e.x}, stack);
|
adamc@1260
|
1586 e = e.f;
|
adamc@1260
|
1587 break;
|
adamc@1260
|
1588 case "r":
|
adamc@1260
|
1589 if (e.l == null)
|
adamc@1260
|
1590 whine("Empty Ur record in interpretation");
|
adamc@1260
|
1591 var fs = {};
|
adamc@1260
|
1592 stack = cons({c: "r", n: e.l.data.n, fs: fs, l: e.l.next}, stack);
|
adamc@1260
|
1593 e = e.l.data.v;
|
adamc@1260
|
1594 break;
|
adamc@1260
|
1595 case ".":
|
adamc@1260
|
1596 stack = cons({c: ".", f: e.f}, stack);
|
adamc@1260
|
1597 e = e.r;
|
adamc@1260
|
1598 break;
|
adamc@1260
|
1599 case ";":
|
adamc@1260
|
1600 stack = cons({c: ";", e2: e.e2}, stack);
|
adamc@1260
|
1601 e = e.e1;
|
adamc@1260
|
1602 break;
|
adamc@1260
|
1603 case "=":
|
adamc@1260
|
1604 stack = cons({c: "=", e2: e.e2}, stack);
|
adamc@1260
|
1605 e = e.e1;
|
adamc@1260
|
1606 break;
|
adamc@1260
|
1607 case "m":
|
adamc@1260
|
1608 stack = cons({c: "m", p: e.p}, stack);
|
adamc@1260
|
1609 e = e.e;
|
adamc@1260
|
1610 break;
|
adamc@1260
|
1611 case "e":
|
adamc@1260
|
1612 e = {c: "c", v: cs({c: "wc", env: env, body: e.e})};
|
adamc@1260
|
1613 break;
|
adamc@1260
|
1614 case "wc":
|
adamc@1260
|
1615 env = e.env;
|
adamc@1260
|
1616 e = e.body;
|
adamc@1260
|
1617 break;
|
adamc@1260
|
1618 case "K":
|
adamc@1260
|
1619 { var savedStack = stack.next, savedEnv = env;
|
adamc@1260
|
1620 e = {c: "c", v: function(v) { return exec1(savedEnv, savedStack, {c: "c", v: v}); } };}
|
adamc@1260
|
1621 usedK = true;
|
rmbruijn@1597
|
1622 break;
|
adamc@1260
|
1623 default:
|
adamc@1260
|
1624 whine("Unknown Ur expression kind " + e.c);
|
adamc@970
|
1625 }
|
adamc@970
|
1626 }
|
adamc@970
|
1627 }
|
adamc@970
|
1628
|
adamc@976
|
1629 function execD(e) {
|
adamc@1260
|
1630 return exec0(null, e);
|
adamc@976
|
1631 }
|
adamc@976
|
1632
|
adamc@970
|
1633 function exec(e) {
|
adamc@1260
|
1634 var r = exec0(null, e);
|
adamc@970
|
1635
|
adamc@1260
|
1636 if (r != null && r.body != null)
|
adamc@1260
|
1637 return function(v) { return exec0(cons(v, r.env), r.body); };
|
adamc@1260
|
1638 else
|
adamc@1260
|
1639 return r;
|
adamc@895
|
1640 }
|
adamc@895
|
1641
|
adamc@977
|
1642 function execF(f, x) {
|
adamc@1260
|
1643 return exec0(cons(x, f.env), f.body);
|
adamc@977
|
1644 }
|
adamc@977
|
1645
|
adamc@895
|
1646
|
adam@1290
|
1647 // Wrappers
|
adam@1290
|
1648
|
adam@1290
|
1649 function confrm(s) {
|
adam@1290
|
1650 return confirm(s) ? true : false;
|
adam@1290
|
1651 }
|
adam@1290
|
1652
|
adam@1290
|
1653
|
adamc@1323
|
1654 // URL blessing
|
adamc@1323
|
1655
|
adamc@1323
|
1656 var urlRules = null;
|
adamc@1323
|
1657
|
adamc@1323
|
1658 function checkUrl(s) {
|
adamc@1323
|
1659 for (var r = urlRules; r; r = r.next) {
|
adamc@1323
|
1660 var ru = r.data;
|
adamc@1323
|
1661 if (ru.prefix ? s.indexOf(ru.pattern) == 0 : s == ru.pattern)
|
adamc@1323
|
1662 return ru.allow ? s : null;
|
adamc@1323
|
1663 }
|
adamc@1323
|
1664
|
adamc@1323
|
1665 return null;
|
adamc@1323
|
1666 }
|
adamc@1323
|
1667
|
adamc@1323
|
1668 function bless(s) {
|
adamc@1323
|
1669 u = checkUrl(s);
|
adamc@1323
|
1670 if (u == null)
|
adamc@1323
|
1671 er("Disallowed URL: " + s);
|
adamc@1323
|
1672 return u;
|
adamc@1323
|
1673 }
|
adamc@1323
|
1674
|
adam@1556
|
1675
|
adam@1556
|
1676 // ID generation
|
adam@1556
|
1677
|
adam@1556
|
1678 var nextId = 0;
|
adam@1556
|
1679
|
adam@1556
|
1680 function fresh() {
|
adam@1556
|
1681 return (--nextId).toString();
|
adam@1556
|
1682 }
|
adam@1556
|
1683
|
rmbruijn@1597
|
1684
|
adamc@693
|
1685 // App-specific code
|