licensing update to APSL 2.0
[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
9 public class Test extends JS {
10     static JS.UnpauseCallback up = null;
11     static String action;
12     
13     public static void main(String[] args) throws Exception {
14         if(args.length == 0) { System.err.println("Usage Test filename"); System.exit(1); }
15         JS f = JS.fromReader(args[0],1,new FileReader(args[0]));
16         System.out.println(((JSFunction)f).dump());
17         JS s = new JS.O();
18         s.put(JS.S("sys"),new Test());
19         f = JS.cloneWithNewGlobalScope(f,s);
20         //JS ret = f.call(null,null,null,null,0);
21         Interpreter i = new Interpreter((JSFunction)f, true, new Interpreter.JSArgs(f));
22         JS ret = i.resume();
23         while(up != null) {
24             JS.UnpauseCallback up = Test.up; Test.up = null;
25             if("throw".equals(action)) ret = up.unpause(new JSExn("this was thrown to a paused context"));
26             else if("bgget".equals(action)) ret = up.unpause(JS.S("I'm returning this from a get request"));
27             else {
28                 System.out.println("got a background put " + action);
29                 ret = up.unpause();
30             }
31         }
32         System.out.println("Script returned: " + JS.toString(ret));
33     }
34     
35     public JS get(JS key) throws JSExn {
36         if(!JS.isString(key)) return null;
37         if("print".equals(JS.toString(key))) return METHOD;
38         if("clone".equals(JS.toString(key))) return METHOD;
39         if("firethis".equals(JS.toString(key))) return METHOD;
40         if("bgget".equals(JS.toString(key))) {
41             action = "bgget";
42             try {
43                 up = JS.pause();
44             } catch(NotPauseableException 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(JS.toString(key))) {
54             action = JS.toString(val);
55             try {
56                 up = JS.pause();
57             } catch(NotPauseableException e) {
58                 throw new Error("should never happen");
59             }
60             return;
61         }   
62         if("exit".equals(JS.toString(key))) {
63             System.exit(JS.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(!JS.isString(method)) return null;
71         if("print".equals(JS.toString(method))) {
72             System.out.println(JS.debugToString(a0));
73             return null;
74         }
75         if("clone".equals(JS.toString(method))) return a0 == null ? null : a0.jsclone();
76         if("firethis".equals(JS.toString(method))) {
77             String action = JS.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,JS.S("some value"));
82             else if(action.equals("trigger")) return target.justTriggerTraps(key,JS.S("some trigger value"));
83             return null;
84         }
85         return null;
86     }
87 }