added some files that had been left out
[org.ibex.js.git] / src / org / ibex / js / Test.java
1 package org.ibex.js;
2
3 import java.io.*;
4
5 public class Test extends JS {
6     static JS.UnpauseCallback up = null;
7     static String action;
8     
9     public static void main(String[] args) throws Exception {
10         if(args.length == 0) { System.err.println("Usage Test filename"); System.exit(1); }
11         JS f = JS.fromReader(args[0],1,new FileReader(args[0]));
12         System.out.println(((JSFunction)f).dump());
13         JS s = new JS.O();
14         s.put(JS.S("sys"),new Test());
15         f = JS.cloneWithNewGlobalScope(f,s);
16         //JS ret = f.call(null,null,null,null,0);
17         Interpreter i = new Interpreter((JSFunction)f, true, new Interpreter.JSArgs(f));
18         JS ret = i.resume();
19         while(up != null) {
20             JS.UnpauseCallback up = Test.up; Test.up = null;
21             if("throw".equals(action)) ret = up.unpause(new JSExn("this was thrown to a paused context"));
22             else if("bgget".equals(action)) ret = up.unpause(JS.S("I'm returning this from a get request"));
23             else {
24                 System.out.println("got a background put " + action);
25                 ret = up.unpause();
26             }
27         }
28         System.out.println("Script returned: " + JS.toString(ret));
29     }
30     
31     public JS get(JS key) throws JSExn {
32         if(!JS.isString(key)) return null;
33         if("print".equals(JS.toString(key))) return METHOD;
34         if("clone".equals(JS.toString(key))) return METHOD;
35         if("firethis".equals(JS.toString(key))) return METHOD;
36         if("bgget".equals(JS.toString(key))) {
37             action = "bgget";
38             try {
39                 up = JS.pause();
40             } catch(NotPauseableException e) {
41                 throw new Error("should never happen");
42             }
43             return null;
44         }
45         return super.get(key);
46     }
47         
48     public void put(JS key, JS val) throws JSExn {
49         if("bgput".equals(JS.toString(key))) {
50             action = JS.toString(val);
51             try {
52                 up = JS.pause();
53             } catch(NotPauseableException e) {
54                 throw new Error("should never happen");
55             }
56             return;
57         }   
58         if("exit".equals(JS.toString(key))) {
59             System.exit(JS.toInt(val));
60             return;
61         }
62         super.put(key,val);
63     }
64     
65     public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
66         if(!JS.isString(method)) return null;
67         if("print".equals(JS.toString(method))) {
68             System.out.println(JS.debugToString(a0));
69             return null;
70         }
71         if("clone".equals(JS.toString(method))) return a0 == null ? null : a0.jsclone();
72         if("firethis".equals(JS.toString(method))) {
73             String action = JS.toString(a0);
74             JS target = a1;
75             JS key = a2;
76             if(action.equals("get")) return a1.getAndTriggerTraps(key);
77             else if(action.equals("put")) a1.putAndTriggerTraps(key,JS.S("some value"));
78             else if(action.equals("trigger")) return target.justTriggerTraps(key,JS.S("some trigger value"));
79             return null;
80         }
81         return null;
82     }
83 }