// Copyright 2000-2005 the Contributors, as shown in the revision logs. // Licensed under the Apache Public Source License 2.0 ("the License"). // You may not use this file except in compliance with the License. package org.ibex.js; import java.io.*; import org.ibex.util.*; public class Test extends JS.Obj { private static final JS.Method METHOD = new JS.Method(); static String action; public static void main(String[] args) throws Exception { if(args.length == 0) { System.err.println("Usage Test filename"); System.exit(1); } JS f = JSU.fromReader(args[0],1,new FileReader(args[0])); System.out.println(((JSFunction)f).dump()); JS s = new JS.Obj(); s.put(JSU.S("sys"),new Test()); f = JSU.cloneWithNewGlobalScope(f,s); //JS ret = f.call(null,null,null,null,0); Interpreter i = new Interpreter((JSFunction)f, true, new JS[0]); JS ret = (JS)i.run(null); try { while(true) { if("throw".equals(action)) ret = (JS)i.run(new JSExn("this was thrown to a paused context")); else if("bgget".equals(action)) ret = (JS)i.run(JSU.S("I'm returning this from a get request")); else { System.out.println("got a background put " + action); ret = (JS)i.run(null); } } } catch (Pausable.AlreadyRunningException e) {} System.out.println("Script returned: " + JSU.toString(ret)); } public JS get(JS key) throws JSExn { if(!JSU.isString(key)) return null; if("print".equals(JSU.toString(key))) return METHOD; if("clone".equals(JSU.toString(key))) return METHOD; if("firethis".equals(JSU.toString(key))) return METHOD; if("bgget".equals(JSU.toString(key))) { action = "bgget"; try { JSU.pause(); } catch(Pausable.NotPausableException e) { throw new Error("should never happen"); } return null; } return super.get(key); } public void put(JS key, JS val) throws JSExn { if("bgput".equals(JSU.toString(key))) { action = JSU.toString(val); try { JSU.pause(); } catch(Pausable.NotPausableException e) { throw new Error("should never happen"); } return; } if("exit".equals(JSU.toString(key))) { System.exit(JSU.toInt(val)); return; } super.put(key,val); } public JS call(JS method, JS[] args) throws JSExn { if(!JSU.isString(method)) return null; if("print".equals(JSU.toString(method))) { System.out.println(JSU.str(args[0])); return null; } if("clone".equals(JSU.toString(method))) return args.length < 1 || args[0] == null ? null : new JS.Clone(args[0]); if("firethis".equals(JSU.toString(method))) { String action = JSU.toString(args[0]); JS target = args[1]; JS key = args[2]; if(action.equals("get")) return args[1].getAndTriggerTraps(key); else if(action.equals("put")) args[1].putAndTriggerTraps(key,JSU.S("some value")); else if(action.equals("trigger")) return target.justTriggerTraps(key,JSU.S("some trigger value")); return null; } return null; } }