updated Makefile.common
[org.ibex.core.git] / src / org / ibex / js / Test.java
index 92c6f95..1bf62e1 100644 (file)
@@ -3,24 +3,79 @@ package org.ibex.js;
 import java.io.*;
 
 public class Test extends JS {
+    static JS.UnpauseCallback up = null;
+    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 = JS.fromReader(args[0],0,new FileReader(args[0]));
-        JSScope s = new JSScope(null);
-        s.put("sys",new Test());
-        f = JS.cloneWithNewParentScope(f,s);
-        Object ret = f.call(null,null,null,null,0);
+        JS f = JS.fromReader(args[0],1,new FileReader(args[0]));
+        System.out.println(((JSFunction)f).dump());
+        JS s = new JS.O();
+        s.put(JS.S("sys"),new Test());
+        f = JS.cloneWithNewGlobalScope(f,s);
+        //JS ret = f.call(null,null,null,null,0);
+        Interpreter i = new Interpreter((JSFunction)f, true, new Interpreter.JSArgs(f));
+        JS ret = i.resume();
+        while(up != null) {
+            JS.UnpauseCallback up = Test.up; Test.up = null;
+            if("throw".equals(action)) ret = up.unpause(new JSExn("this was thrown to a paused context"));
+            else if("bgget".equals(action)) ret = up.unpause(JS.S("I'm returning this from a get request"));
+            else {
+                System.out.println("got a background put " + action);
+                ret = up.unpause();
+            }
+        }
         System.out.println("Script returned: " + JS.toString(ret));
     }
     
-    public Object get(Object key) throws JSExn {
-        if("print".equals(key)) return METHOD;
+    public JS get(JS key) throws JSExn {
+        if(!JS.isString(key)) return null;
+        if("print".equals(JS.toString(key))) return METHOD;
+        if("clone".equals(JS.toString(key))) return METHOD;
+        if("firethis".equals(JS.toString(key))) return METHOD;
+        if("bgget".equals(JS.toString(key))) {
+            action = "bgget";
+            try {
+                up = JS.pause();
+            } catch(NotPauseableException 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(JS.toString(key))) {
+            action = JS.toString(val);
+            try {
+                up = JS.pause();
+            } catch(NotPauseableException e) {
+                throw new Error("should never happen");
+            }
+            return;
+        }   
+        if("exit".equals(JS.toString(key))) {
+            System.exit(JS.toInt(val));
+            return;
+        }
+        super.put(key,val);
+    }
     
-    public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
-        if("print".equals(method)) {
-            System.out.println(JS.toString(a0));
+    public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
+        if(!JS.isString(method)) return null;
+        if("print".equals(JS.toString(method))) {
+            System.out.println(JS.debugToString(a0));
+            return null;
+        }
+        if("clone".equals(JS.toString(method))) return a0 == null ? null : a0.jsclone();
+        if("firethis".equals(JS.toString(method))) {
+            String action = JS.toString(a0);
+            JS target = a1;
+            JS key = a2;
+            if(action.equals("get")) return a1.getAndTriggerTraps(key);
+            else if(action.equals("put")) a1.putAndTriggerTraps(key,JS.S("some value"));
+            else if(action.equals("trigger")) return target.justTriggerTraps(key,JS.S("some trigger value"));
             return null;
         }
         return null;