yhara-tickets 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,762 @@
1
+
2
+ if( typeof(BiwaScheme)!='object' ) BiwaScheme={}; with(BiwaScheme) {
3
+ define_libfunc("read-line", 0, 1, function(ar){
4
+ var port = ar[0] || Port.current_input;
5
+ assert_port(port);
6
+ return port.get_string();
7
+ });
8
+
9
+ //
10
+ // element
11
+ //
12
+ define_libfunc("element-clear!", 1, 1, function(ar){
13
+ return $(ar[0]).update();
14
+ });
15
+ define_libfunc("element-empty!", 1, 1, function(ar){
16
+ return $(ar[0]).update();
17
+ });
18
+
19
+ define_libfunc("element-visible?", 1, 1, function(ar){
20
+ return $(ar[0]).visible();
21
+ });
22
+ define_libfunc("element-toggle!", 1, 1, function(ar){
23
+ return $(ar[0]).toggle();
24
+ })
25
+ define_libfunc("element-hide!", 1, 1, function(ar){
26
+ return $(ar[0]).hide();
27
+ })
28
+ define_libfunc("element-show!", 1, 1, function(ar){
29
+ return $(ar[0]).show();
30
+ })
31
+ define_libfunc("element-remove!", 1, 1, function(ar){
32
+ return $(ar[0]).remove("");
33
+ })
34
+ define_libfunc("element-update!", 2, 2, function(ar){
35
+ return $(ar[0]).update(ar[1]);
36
+ })
37
+ define_libfunc("element-replace!", 2, 2, function(ar){
38
+ return $(ar[0]).replace(ar[1]);
39
+ });
40
+ define_libfunc("element-insert!", 2, 2, function(ar){
41
+ return $(ar[0]).insert(ar[1]);
42
+ });
43
+ define_libfunc("element-wrap!", 3, 3, function(ar){
44
+ throw new Bug("not yet implemented");
45
+ });
46
+ define_libfunc("element-ancestors", 1, 1, function(ar){
47
+ throw new Bug("not yet implemented");
48
+ });
49
+ define_libfunc("element-descendants", 1, 1, function(ar){
50
+ throw new Bug("not yet implemented");
51
+ });
52
+ define_libfunc("element-first-descendant", 1, 1, function(ar){
53
+ throw new Bug("not yet implemented");
54
+ });
55
+ define_libfunc("element-immediate-descendants", 1, 1, function(ar){
56
+ throw new Bug("not yet implemented");
57
+ });
58
+ define_libfunc("element-previous-sibling", 1, 1, function(ar){
59
+ throw new Bug("not yet implemented");
60
+ });
61
+ define_libfunc("element-next-sibling", 1, 1, function(ar){
62
+ throw new Bug("not yet implemented");
63
+ });
64
+ define_libfunc("element-siblings", 1, 1, function(ar){
65
+ throw new Bug("not yet implemented");
66
+ });
67
+ define_libfunc("element-match?", 2, 2, function(ar){
68
+ throw new Bug("not yet implemented");
69
+ });
70
+ define_libfunc("element-up", 3, 3, function(ar){
71
+ throw new Bug("not yet implemented");
72
+ });
73
+ define_libfunc("element-down", 3, 3, function(ar){
74
+ throw new Bug("not yet implemented");
75
+ });
76
+ define_libfunc("element-previous", 3, 3, function(ar){
77
+ throw new Bug("not yet implemented");
78
+ });
79
+ define_libfunc("element-next", 3, 3, function(ar){
80
+ throw new Bug("not yet implemented");
81
+ });
82
+ define_libfunc("element-select", 0, 0, function(ar){
83
+ throw new Bug("not yet implemented");
84
+ });
85
+ define_libfunc("element-adjacent", 0, 0, function(ar){
86
+ throw new Bug("not yet implemented");
87
+ });
88
+ define_libfunc("element-identify", 1, 1, function(ar){
89
+ throw new Bug("not yet implemented");
90
+ });
91
+ define_libfunc("element-read-attribute", 2, 2, function(ar){
92
+ assert_string(ar[1]);
93
+ return $(ar[0]).readAttribute(ar[1]);
94
+ });
95
+ define_libfunc("element-write-attribute", 3, 3, function(ar){
96
+ assert_string(ar[1]);
97
+ return $(ar[0]).readAttribute(ar[1], ar[2]);
98
+ });
99
+ define_libfunc("element-height", 1, 1, function(ar){
100
+ return $(ar[0]).getHeight();
101
+ });
102
+ define_libfunc("element-width", 1, 1, function(ar){
103
+ return $(ar[0]).getWidth();
104
+ });
105
+ define_libfunc("element-class-names", 1, 1, function(ar){
106
+ throw new Bug("not yet implemented");
107
+ });
108
+ define_libfunc("element-has-class-name?", 2, 2, function(ar){
109
+ throw new Bug("not yet implemented");
110
+ });
111
+ define_libfunc("element-add-class-name", 2, 2, function(ar){
112
+ throw new Bug("not yet implemented");
113
+ });
114
+ define_libfunc("element-remove-class-name", 2, 2, function(ar){
115
+ throw new Bug("not yet implemented");
116
+ });
117
+ define_libfunc("element-toggle-class-name", 2, 2, function(ar){
118
+ throw new Bug("not yet implemented");
119
+ });
120
+ define_libfunc("element-clean-whitespace!", 1, 1, function(ar){
121
+ throw new Bug("not yet implemented");
122
+ });
123
+ define_libfunc("element-empty?", 1, 1, function(ar){
124
+ throw new Bug("not yet implemented");
125
+ })
126
+ define_libfunc("element-descendant-of!", 2, 2, function(ar){
127
+ throw new Bug("not yet implemented");
128
+ })
129
+ define_libfunc("scroll-to-element!", 1, 1, function(ar){
130
+ throw new Bug("not yet implemented");
131
+ })
132
+ define_libfunc("element-style", 2, 2, function(ar){
133
+ throw new Bug("not yet implemented");
134
+ })
135
+ define_libfunc("element-opacity", 2, 2, function(ar){
136
+ throw new Bug("not yet implemented");
137
+ })
138
+ define_libfunc("element-style-set!", 2, 2, function(ar){
139
+ throw new Bug("not yet implemented");
140
+ })
141
+ define_libfunc("element-opacity-set!", 2, 2, function(ar){
142
+ throw new Bug("not yet implemented");
143
+ })
144
+ define_libfunc("element-dimensions", 1, 1, function(ar){
145
+ var dimensions = $(ar[0]).getDimensions();
146
+ return new Values(dimensions.width, dimensions.height);
147
+ })
148
+ define_libfunc("element-make-positioned!", 1, 1, function(ar){
149
+ throw new Bug("not yet implemented");
150
+ })
151
+ define_libfunc("element-undo-positioned!", 1, 1, function(ar){
152
+ throw new Bug("not yet implemented");
153
+ })
154
+ define_libfunc("element-make-clipping!", 1, 1, function(ar){
155
+ throw new Bug("not yet implemented");
156
+ })
157
+ define_libfunc("element-undo-clipping!", 1, 1, function(ar){
158
+ throw new Bug("not yet implemented");
159
+ })
160
+ define_libfunc("element-cumulative-offset", 1, 1, function(ar){
161
+ throw new Bug("not yet implemented");
162
+ })
163
+ define_libfunc("element-positioned-offset", 1, 1, function(ar){
164
+ throw new Bug("not yet implemented");
165
+ })
166
+ define_libfunc("element-absolutize!", 1, 1, function(ar){
167
+ throw new Bug("not yet implemented");
168
+ })
169
+ define_libfunc("element-relativize!", 1, 1, function(ar){
170
+ throw new Bug("not yet implemented");
171
+ })
172
+ define_libfunc("element-cumulative-scroll-offset", 1, 1, function(ar){
173
+ throw new Bug("not yet implemented");
174
+ })
175
+ define_libfunc("element-offset-parent", 1, 1, function(ar){
176
+ throw new Bug("not yet implemented");
177
+ })
178
+ define_libfunc("element-viewport-offset", 1, 1, function(ar){
179
+ throw new Bug("not yet implemented");
180
+ })
181
+ define_libfunc("element-clone-position!", 1, 1, function(ar){
182
+ throw new Bug("not yet implemented");
183
+ })
184
+ define_libfunc("element-absolutize!", 1, 1, function(ar){
185
+ throw new Bug("not yet implemented");
186
+ })
187
+
188
+ // usage:
189
+ // (element-new '(div "foo")) => <div>foo</div>
190
+ // (element-new '("div#main" "foo")) => <div id='main'>foo</div>
191
+ // (element-new '("div.red" "foo")) => <div class='red'>foo</div>
192
+ // (element-new '(div align right)) => <div align='right'>foo</div>
193
+ // (element-new '(div (span "foo")) => <div><span>foo</span></div>
194
+ //
195
+
196
+ // This function is not used; slower than by_string
197
+ BiwaScheme.create_elements_by_dom = function(spec){
198
+ var create_element = function(name, attrs, children){
199
+ var tag = new Element(name, attrs); //attrs is an Object
200
+ children.each(function(child){
201
+ tag.insert({bottom: child});
202
+ });
203
+ return tag;
204
+ };
205
+ var spec = spec.to_array();
206
+ var name = spec[0].name || spec[0]; // Symbol or String
207
+ var attrs = {};
208
+ var children = [];
209
+ for(var i=1; i<spec.length; i++){
210
+ if(spec[i] instanceof Symbol){ //attribute
211
+ attrs[ spec[i].name ] = spec[i+1];
212
+ i++;
213
+ }
214
+ else{
215
+ if(spec[i] instanceof Pair)
216
+ children.push(create_elements_by_dom(spec[i]));
217
+ else
218
+ children.push(spec[i].toString());
219
+ }
220
+ }
221
+ return create_element(name, attrs, children);
222
+ };
223
+
224
+ BiwaScheme.create_elements_by_string = function(spec){
225
+ var spec = spec.to_array();
226
+ var name = spec.shift();
227
+ if(name instanceof Symbol) name = name.name;
228
+ if(name.match(/(.*)\.(.*)/)){
229
+ name = RegExp.$1;
230
+ spec.unshift(Sym("class"), RegExp.$2);
231
+ }
232
+ if(name.match(/(.*)\#(.*)/)){
233
+ name = RegExp.$1;
234
+ spec.unshift(Sym("id"), RegExp.$2);
235
+ }
236
+ var children = [];
237
+ var s = ["<" + name];
238
+ for(var i=0; i<spec.length; i++){
239
+ if(spec[i] instanceof Symbol){
240
+ s.push(' ' + spec[i].name + '="' + spec[i+1] + '"');
241
+ i++;
242
+ }
243
+ else{
244
+ if(spec[i] instanceof Pair)
245
+ children.push(create_elements_by_string(spec[i]));
246
+ else
247
+ children.push(spec[i]); // String
248
+ }
249
+ }
250
+ s.push(">");
251
+ s.push( children.join("") );
252
+ s.push("</" + name + ">");
253
+ return s.join("");
254
+ }
255
+ BiwaScheme.tree_all = function(tree, pred){
256
+ if(tree === nil)
257
+ return true;
258
+ else if(pred(tree.car) === false)
259
+ return false;
260
+ else
261
+ return BiwaScheme.tree_all(tree.cdr, pred);
262
+ }
263
+ define_libfunc("element-new", 1, 1, function(ar){
264
+ var string_or_symbol = function(item){
265
+ return Object.isString(item) ||
266
+ (item instanceof Symbol) ||
267
+ (item instanceof Pair);
268
+ };
269
+ if(BiwaScheme.tree_all(ar[0], string_or_symbol)){
270
+ var div = new Element("div");
271
+ div.update( create_elements_by_string(ar[0]) );
272
+ return div.firstChild;
273
+ }
274
+ else
275
+ return nil //create_elements_by_dom(ar[0]);
276
+ });
277
+ define_libfunc("element-content", 1, 1, function(ar){
278
+ return ar[0].value || (ar[0].innerHTML).unescapeHTML();
279
+ });
280
+
281
+ //
282
+ // load from network
283
+ //
284
+ define_libfunc("load", 1, 1, function(ar, intp){
285
+ var path = ar[0];
286
+ assert_string(path);
287
+ return new BiwaScheme.Pause(function(pause){
288
+ new Ajax.Request(path, {
289
+ method: 'get',
290
+ evalJSON: false,
291
+ evalJS: false,
292
+ onSuccess: function(transport) {
293
+ var parser_saved = intp.parser;
294
+ var after_evaluate_saved = intp.after_evaluate;
295
+ intp.evaluate(transport.responseText, function(){
296
+ intp.parser = parser_saved;
297
+ intp.after_evaluate = after_evaluate_saved;
298
+ return pause.resume(BiwaScheme.undef);
299
+ })
300
+ },
301
+ onFailure: function(transport){
302
+ throw new Error("load: network error: failed to load"+path);
303
+ }
304
+ });
305
+ });
306
+ })
307
+
308
+ // (js-load "lib/foo.js" "Foo")
309
+ define_libfunc("js-load", 2, 2, function(ar){
310
+ var path = ar[0];
311
+ var check = ar[1];
312
+ assert_string(path);
313
+ assert_string(check);
314
+
315
+ return new BiwaScheme.Pause(function(pause){
316
+ BiwaScheme.require(path, "window." + check, function(){
317
+ pause.resume(BiwaScheme.undef);
318
+ });
319
+ });
320
+ });
321
+
322
+ //
323
+ // html modification
324
+ //
325
+
326
+ BiwaScheme.getelem = function(ar){
327
+ var x = $(ar[0]);
328
+ if (x===undefined || x===null)
329
+ return false;
330
+ else
331
+ return x;
332
+ };
333
+ define_libfunc("$", 1, 1, BiwaScheme.getelem);
334
+ define_libfunc("getelem", 1, 1, BiwaScheme.getelem);
335
+
336
+ define_libfunc("set-style!", 3, 3, function(ar){
337
+ assert_string(ar[1]);
338
+ ar[0].style[ar[1]] = ar[2];
339
+ });
340
+ define_libfunc("get-style", 2, 2, function(ar){
341
+ assert_string(ar[1]);
342
+ return ar[0].style[ar[1]];
343
+ });
344
+ define_libfunc("set-content!", 2, 2, function(ar){
345
+ assert_string(ar[1]);
346
+ var str = ar[1].replace(/\n/g,"<br>").replace(/\t/g,"&nbsp;&nbsp;&nbsp;");
347
+ ar[0].innerHTML = str;
348
+ });
349
+ define_libfunc("get-content", 1, 1, function(ar){
350
+ return ar[0].value || (ar[0].innerHTML).unescapeHTML();
351
+ });
352
+
353
+ //
354
+ // timer, sleep, handlers
355
+ //
356
+ define_libfunc("timer", 2, 2, function(ar, intp){
357
+ var proc = ar[0], msec = ar[1];
358
+ setTimeout(function(){ (new Interpreter(intp.on_error)).invoke_closure(proc) }, msec);
359
+ })
360
+ define_libfunc("set-timer!", 2, 2, function(ar, intp){
361
+ var proc = ar[0], msec = ar[1];
362
+ return setInterval(function(){ (new Interpreter(intp.on_error)).invoke_closure(proc) }, msec);
363
+ })
364
+ define_libfunc("clear-timer!", 1, 1, function(ar){
365
+ var timer_id = ar[0];
366
+ clearInterval(timer_id);
367
+ })
368
+
369
+ define_libfunc("sleep", 1, 1, function(ar){
370
+ var msec = ar[0];
371
+ return new BiwaScheme.Pause(function(pause){
372
+ setTimeout(function(){ pause.resume(nil) }, msec);
373
+ });
374
+ });
375
+
376
+ define_libfunc("set-handler!", 3, 3, function(ar, intp){
377
+ throw new Error("set-handler! is obsolete, please use add-handler! instead");
378
+ })
379
+ define_libfunc("add-handler!", 3, 3, function(ar, intp){
380
+ var elem = ar[0], evtype = ar[1], proc = ar[2];
381
+ var on_error = intp.on_error;
382
+ Event.observe(elem, evtype, function(event){
383
+ var intp = new Interpreter(on_error);
384
+ intp.invoke_closure(proc, [event||Window.event]);
385
+ });
386
+ })
387
+ define_libfunc("wait-for", 2, 2, function(ar){
388
+ var elem = ar[0], evtype = ar[1];
389
+ elem.biwascheme_wait_for = elem.biwascheme_wait_for || {};
390
+
391
+ var prev_handler = elem.biwascheme_wait_for[evtype];
392
+ if (prev_handler) {
393
+ // Maybe a wait-for is called more than once,
394
+ // and previous handler is not consumed.
395
+ Event.stopObserving(elem, evtype, prev_handler);
396
+ }
397
+
398
+ return new BiwaScheme.Pause(function(pause){
399
+ var handler = function(event){
400
+ elem.biwascheme_wait_for[evtype] = undefined;
401
+ Event.stopObserving(elem, evtype, handler);
402
+ return pause.resume();
403
+ };
404
+
405
+ elem.biwascheme_wait_for[evtype] = handler;
406
+ Event.observe(elem, evtype, handler);
407
+ });
408
+ });
409
+
410
+ //
411
+ // dom
412
+ //
413
+ define_libfunc("domelem", 1, null, function(ar){
414
+ });
415
+ define_libfunc("dom-remove-children!", 1, 1, function(ar){
416
+ puts("warning: dom-remove-children! is obsolete. use element-empty! instead");
417
+ $(ar[0]).update("");
418
+ });
419
+ define_libfunc("dom-create-element", 1, 1, function(ar){
420
+ });
421
+ define_libfunc("element-append-child!", 2, 2, function(ar){
422
+ return $(ar[0]).appendChild(ar[1]);
423
+ });
424
+ define_libfunc("dom-remove-child!", 2, 2, function(ar){
425
+ });
426
+ // define_libfunc("dom-get-attribute", 2, 2, function(ar){
427
+ // });
428
+ // define_libfunc("dom-remove-child!", 2, 2, function(ar){
429
+ // });
430
+
431
+ //
432
+ // interface to javascript
433
+ //
434
+ define_libfunc("js-eval", 1, 1, function(ar){
435
+ return eval(ar[0]);
436
+ });
437
+ define_libfunc("js-ref", 2, 2, function(ar){
438
+ assert_string(ar[1]);
439
+ return ar[0][ar[1]]; //todo: first-class js function
440
+ });
441
+ define_libfunc("js-set!", 3, 3, function(ar){
442
+ assert_string(ar[1]);
443
+ return ar[0][ar[1]] = ar[2];
444
+ });
445
+
446
+ // (js-call (js-eval "Math.pow") 2 4)
447
+ define_libfunc("js-call", 1, null, function(ar){
448
+ var js_func = ar.shift();
449
+ assert_function(js_func);
450
+
451
+ return js_func.apply(js_func, ar);
452
+ });
453
+ // (js-invoke (js-new "Date") "getTime")
454
+ define_libfunc("js-invoke", 2, null, function(ar){
455
+ var js_obj = ar.shift();
456
+ var func_name = ar.shift();
457
+ assert_string(func_name);
458
+ if(js_obj[func_name])
459
+ return js_obj[func_name].apply(js_obj, ar);
460
+ else
461
+ throw new Error("js-invoke: function "+func_name+" is not defined");
462
+ });
463
+
464
+ // (js-new "Date" 2005 1 1)
465
+ define_libfunc("js-new", 1, null, function(ar){
466
+ var ctor = ar.shift();
467
+ assert_string(ctor);
468
+ if(ar.length == 0){
469
+ return eval("new " + ctor + "()");
470
+ }
471
+ else{
472
+ var args_str = ar.map(function(value, i){
473
+ return "ar['" + i + "']";
474
+ }).join(",");
475
+ return eval("new " + ctor + "(" + args_str + ")");
476
+ }
477
+ });
478
+
479
+ // (js-obj "foo" 1 "bar" 2)
480
+ // -> {"foo": 1, "bar": 2}
481
+ define_libfunc("js-obj", 0, null, function(ar){
482
+ if(ar.length % 2 != 0){
483
+ throw new Error("js-obj: number of arguments must be even");
484
+ }
485
+
486
+ var obj = {};
487
+ for(i=0; i<ar.length/2; i++){
488
+ assert_string(ar[i*2]);
489
+ obj[ar[i*2]] = ar[i*2+1];
490
+ }
491
+ return obj;
492
+ });
493
+
494
+ // (js-closure (lambda (event) ..))
495
+ define_libfunc("js-closure", 1, 1, function(ar, intp){
496
+ var proc = ar[0];
497
+ var on_error = intp.on_error;
498
+ return function(/*args*/){
499
+ var intp = new Interpreter(on_error);
500
+ intp.invoke_closure(proc, $A(arguments));
501
+ };
502
+ });
503
+
504
+ define_libfunc("js-null?", 1, 1, function(ar){
505
+ return ar[0] === null;
506
+ });
507
+
508
+ define_libfunc("js-undefined?", 1, 1, function(ar){
509
+ return ar[0] === BiwaScheme.undef;
510
+ });
511
+
512
+ //
513
+ // communication to server
514
+ //
515
+ define_libfunc("http-request", 1, 1, function(ar){
516
+ var path = ar[0];
517
+ assert_string(path);
518
+
519
+ return new BiwaScheme.Pause(function(pause){
520
+ new Ajax.Request(path, {
521
+ method: 'get',
522
+ onSuccess: function(transport) {
523
+ pause.resume(transport.responseText)
524
+ }
525
+ });
526
+ });
527
+ });
528
+ // (http-post "/foo" '(("x" . 1) ("y" . 2)))
529
+ define_libfunc("http-post", 2, 2, function(ar){
530
+ var path = ar[0];
531
+ assert_string(path);
532
+ var alist = ar[1];
533
+ assert_pair(alist);
534
+ var h = {};
535
+
536
+ alist.foreach(function(item){
537
+ assert_string(item.car);
538
+ h[item.car] = item.cdr;
539
+ })
540
+
541
+ return new BiwaScheme.Pause(function(pause){
542
+ new Ajax.Request(path, {
543
+ method: 'post',
544
+ postBody: $H(h).toQueryString(),
545
+ onSuccess: function(transport) {
546
+ pause.resume(transport.responseText)
547
+ }
548
+ });
549
+ });
550
+ });
551
+
552
+ BiwaScheme.jsonp_receiver = [];
553
+ define_libfunc("receive-jsonp", 1, 1, function(ar){
554
+ var url = ar[0];
555
+ assert_string(url);
556
+
557
+ var receives = BiwaScheme.jsonp_receiver;
558
+ for(var i=0; i<receives.length; i++)
559
+ if(receives[i] === null) break;
560
+ var receiver_id = i;
561
+ url += "?callback=BiwaScheme.jsonp_receiver[" + receiver_id + "]"
562
+
563
+ return new BiwaScheme.Pause(function(pause){
564
+ receives[receiver_id] = function(data){
565
+ pause.resume(data);
566
+ receives[receiver_id] = null;
567
+ }
568
+ var script = document.createElement('script')
569
+ script.src = url;
570
+ document.body.appendChild(script);
571
+ })
572
+ });
573
+
574
+ //
575
+ // dialog, debug
576
+ //
577
+ define_libfunc("alert", 1, 1, function(ar){
578
+ return alert(ar[0]);
579
+ });
580
+ define_libfunc("confirm", 1, 1, function(ar){
581
+ return confirm(ar[0]);
582
+ });
583
+ // define_libfunc("dumpobj", 1, 1, function(ar){
584
+ // return eval(ar[0]);
585
+ // });
586
+
587
+ //
588
+ // tuple space
589
+ //
590
+ BiwaScheme.TupleSpaceClient = Class.create({
591
+ initialize: function(server_path){
592
+ this.server_path = server_path; //must end with '/'
593
+ },
594
+
595
+ nonblocking_request: function(name, data){
596
+ var path = this.server_path + name + "?"
597
+ + encodeURIComponent(to_write(data));
598
+ return this.connect(path);
599
+ },
600
+ blocking_request: function(name, data){
601
+ this.assert_init();
602
+ var path = this.server_path + name + "?"
603
+ + encodeURIComponent(to_write(data))
604
+ + "&cid=" + this.client_id;
605
+ return new BiwaScheme.Pause(function(pause){
606
+ this.ajax(path, function(ticket){
607
+ this.observe(ticket, function(tuple){
608
+ pause.resume(tuple);
609
+ });
610
+ }.bind(this));
611
+ }.bind(this));
612
+ },
613
+
614
+ write: function(value){
615
+ return this.nonblocking_request("write", value);
616
+ },
617
+ readp: function(query){
618
+ return this.nonblocking_request("readp", query);
619
+ },
620
+ takep: function(query){
621
+ return this.nonblocking_request("takep", query);
622
+ },
623
+ dump: function(){
624
+ return this.nonblocking_request("dump", "");
625
+ },
626
+
627
+ read: function(query){
628
+ return this.blocking_request("read", query);
629
+ },
630
+ take: function(query){
631
+ return this.blocking_request("take", query);
632
+ },
633
+
634
+ ajax: function(path, callback){
635
+ new Ajax.Request(path, {
636
+ onSuccess: function(transport){
637
+ callback(transport.responseText);
638
+ },
639
+ onFailure: function(){
640
+ puts("error: failed to access " + path);
641
+ }
642
+ });
643
+ },
644
+
645
+ init_connection: function(){
646
+ if(this.client_id){
647
+ return this.client_id;
648
+ }
649
+ else{
650
+ return new Pause(function(pause){
651
+ var path = this.server_path + "init_connection";
652
+ this.ajax(path, function(str){
653
+ this.client_id = str;
654
+ this.start_connection();
655
+ pause.resume(this.client_id);
656
+ }.bind(this));
657
+ }.bind(this));
658
+ }
659
+ },
660
+ assert_init: function(){
661
+ if(!this.client_id){
662
+ puts("ts-init not called:"+Object.inspect(this.client_id));
663
+ throw new Error("ts-init not called");
664
+ }
665
+ },
666
+
667
+
668
+ // returns nothing
669
+ start_connection: function(){
670
+ var path = this.server_path + "connection?"
671
+ + "cid=" + this.client_id;
672
+ var loop = function(){
673
+ this.ajax(path, function(str){
674
+ var result = Interpreter.read(str);
675
+ var ticket = result.car, tuple = result.cdr;
676
+
677
+ this.notify(ticket, tuple);
678
+ loop();
679
+ }.bind(this));
680
+ }.bind(this);
681
+ loop();
682
+ },
683
+
684
+ waiters: [],
685
+ too_early_tuples: [],
686
+ observe: function(ticket, callback){
687
+ if(this.too_early_tuples[ticket]){
688
+ //alert("1 too early resolved");
689
+ callback(this.too_early_tuples[ticket]);
690
+ this.too_early_tuples[ticket] = undefined;
691
+ }
692
+ else if(this.waiters[ticket]){
693
+ puts("Bug: ticket conflicted");
694
+ }
695
+ else{
696
+ this.waiters[ticket] = callback;
697
+ }
698
+ },
699
+ notify: function(ticket, tuple){
700
+ var callback = this.waiters[ticket];
701
+ if(callback){
702
+ this.waiters[ticket] = undefined; //or use 'delete'?
703
+ return callback(tuple);
704
+ }
705
+ else{
706
+ this.too_early_tuples[ticket] = tuple;
707
+ }
708
+ },
709
+
710
+ connect: function(path, on_result){
711
+ path += "&time=" + (new Date()).getTime();
712
+ return new BiwaScheme.Pause(function(pause){
713
+ new Ajax.Request(path, {
714
+ method: 'get',
715
+ onSuccess: function(transport){
716
+ var result = transport.responseText;
717
+ if(on_result)
718
+ result = on_result(result);
719
+ else
720
+ result = Interpreter.read(result);
721
+
722
+ if(result == undefined)
723
+ pause.resume(false)
724
+ else
725
+ pause.resume(result)
726
+ },
727
+ onFailure: function(transport){
728
+ throw new Error("ts_client.connect: failed to access"+path);
729
+ pause.resume(false)
730
+ }
731
+ });
732
+ });
733
+ }
734
+ });
735
+ BiwaScheme.ts_client = new TupleSpaceClient("/ts/");
736
+
737
+ define_libfunc("ts-init", 0, 0, function(ar){
738
+ return ts_client.init_connection();
739
+ });
740
+ define_libfunc("ts-write", 1, 1, function(ar){
741
+ return ts_client.write(ar[0]);
742
+ });
743
+
744
+ define_libfunc("ts-read", 1, 1, function(ar){
745
+ var query = ar[0];
746
+ return ts_client.read(query); //returns a Pause
747
+ });
748
+ define_libfunc("ts-readp", 1, 1, function(ar){
749
+ return ts_client.readp(ar[0]);
750
+ });
751
+ define_libfunc("ts-take", 1, 1, function(ar){
752
+ return ts_client.take(ar[0]);
753
+ });
754
+ define_libfunc("ts-takep", 1, 1, function(ar){
755
+ return ts_client.takep(ar[0]);
756
+ });
757
+ define_libfunc("ts-dump", 0, 0, function(ar){
758
+ return ts_client.dump();
759
+ });
760
+
761
+ }
762
+