update Directory, Fountain, JSReflection, SOAP, XMLRPC to use baskets and new JS...
[org.ibex.js.git] / src / org / ibex / js / Test.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.js;
6
7 import java.io.*;
8 import org.ibex.util.*;
9
10 public class Test extends JS.Obj {
11     private static final JS.Method METHOD = new JS.Method();
12     static String action;
13     
14     public static void main(String[] args) throws Exception {
15         if(args.length == 0) { System.err.println("Usage Test filename"); System.exit(1); }
16         JS f = Script.fromReader(args[0],1,new FileReader(args[0]));
17         System.out.println(((JSFunction)f).dump());
18         JS s = new JS.Obj();
19         s.put(Script.S("sys"),new Test());
20         f = Script.cloneWithNewGlobalScope(f,s);
21         //JS ret = f.call(null,null,null,null,0);
22         Interpreter i = new Interpreter((JSFunction)f, true, new JS[0]);
23         JS ret = (JS)i.run(null);
24         try { while(true) {
25             if("throw".equals(action)) ret = (JS)i.run(new JSExn("this was thrown to a paused context"));
26             else if("bgget".equals(action)) ret = (JS)i.run(Script.S("I'm returning this from a get request"));
27             else {
28                 System.out.println("got a background put " + action);
29                 ret = (JS)i.run(null);
30             }
31         } } catch (Pausable.AlreadyRunningException e) {}
32         System.out.println("Script returned: " + Script.toString(ret));
33     }
34     
35     public JS get(JS key) throws JSExn {
36         if(!Script.isString(key)) return null;
37         if("print".equals(Script.toString(key))) return METHOD;
38         if("clone".equals(Script.toString(key))) return METHOD;
39         if("firethis".equals(Script.toString(key))) return METHOD;
40         if("bgget".equals(Script.toString(key))) {
41             action = "bgget";
42             try {
43                 Script.pause();
44             } catch(Pausable.NotPausableException e) {
45                 throw new Error("should never happen");
46             }
47             return null;
48         }
49         return super.get(key);
50     }
51         
52     public void put(JS key, JS val) throws JSExn {
53         if("bgput".equals(Script.toString(key))) {
54             action = Script.toString(val);
55             try {
56                 Script.pause();
57             } catch(Pausable.NotPausableException e) {
58                 throw new Error("should never happen");
59             }
60             return;
61         }   
62         if("exit".equals(Script.toString(key))) {
63             System.exit(Script.toInt(val));
64             return;
65         }
66         super.put(key,val);
67     }
68     
69     public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
70         if(!Script.isString(method)) return null;
71         if("print".equals(Script.toString(method))) {
72             System.out.println(Script.str(a0));
73             return null;
74         }
75         if("clone".equals(Script.toString(method))) return a0 == null ? null : new JS.Clone(a0);
76         if("firethis".equals(Script.toString(method))) {
77             String action = Script.toString(a0);
78             JS target = a1;
79             JS key = a2;
80             if(action.equals("get")) return a1.getAndTriggerTraps(key);
81             else if(action.equals("put")) a1.putAndTriggerTraps(key,Script.S("some value"));
82             else if(action.equals("trigger")) return target.justTriggerTraps(key,Script.S("some trigger value"));
83             return null;
84         }
85         return null;
86     }
87 }