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.
9 public class Test extends JS {
10 static JS.UnpauseCallback up = null;
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());
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));
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"));
28 System.out.println("got a background put " + action);
32 System.out.println("Script returned: " + JS.toString(ret));
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))) {
44 } catch(NotPauseableException e) {
45 throw new Error("should never happen");
49 return super.get(key);
52 public void put(JS key, JS val) throws JSExn {
53 if("bgput".equals(JS.toString(key))) {
54 action = JS.toString(val);
57 } catch(NotPauseableException e) {
58 throw new Error("should never happen");
62 if("exit".equals(JS.toString(key))) {
63 System.exit(JS.toInt(val));
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));
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);
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"));