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.
8 import org.ibex.util.*;
10 public class Test extends JS.Obj {
11 private static final JS.Method METHOD = new JS.Method();
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());
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);
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"));
28 System.out.println("got a background put " + action);
29 ret = (JS)i.run(null);
31 } } catch (Pausable.AlreadyRunningException e) {}
32 System.out.println("Script returned: " + Script.toString(ret));
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))) {
44 } catch(Pausable.NotPausableException 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(Script.toString(key))) {
54 action = Script.toString(val);
57 } catch(Pausable.NotPausableException e) {
58 throw new Error("should never happen");
62 if("exit".equals(Script.toString(key))) {
63 System.exit(Script.toInt(val));
69 public JS call(JS method, JS[] args) throws JSExn {
70 if(!Script.isString(method)) return null;
71 if("print".equals(Script.toString(method))) {
72 System.out.println(Script.str(args[0]));
75 if("clone".equals(Script.toString(method)))
76 return args.length < 1 || args[0] == null ? null : new JS.Clone(args[0]);
77 if("firethis".equals(Script.toString(method))) {
78 String action = Script.toString(args[0]);
81 if(action.equals("get")) return args[1].getAndTriggerTraps(key);
82 else if(action.equals("put")) args[1].putAndTriggerTraps(key,Script.S("some value"));
83 else if(action.equals("trigger")) return target.justTriggerTraps(key,Script.S("some trigger value"));