bug fixes
[org.ibex.js.git] / src / org / ibex / js / Test.java
index 1bf62e1..9598db4 100644 (file)
@@ -1,43 +1,47 @@
+// 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 {
-    static JS.UnpauseCallback up = null;
+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 = JS.fromReader(args[0],1,new FileReader(args[0]));
+        JS f = Script.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 s = new JS.Obj();
+        s.put(Script.S("sys"),new Test());
+        f = Script.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"));
+        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(Script.S("I'm returning this from a get request"));
             else {
                 System.out.println("got a background put " + action);
-                ret = up.unpause();
+                ret = (JS)i.run(null);
             }
-        }
-        System.out.println("Script returned: " + JS.toString(ret));
+        } } catch (Pausable.AlreadyRunningException e) {}
+        System.out.println("Script returned: " + Script.toString(ret));
     }
     
     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))) {
+        if(!Script.isString(key)) return null;
+        if("print".equals(Script.toString(key))) return METHOD;
+        if("clone".equals(Script.toString(key))) return METHOD;
+        if("firethis".equals(Script.toString(key))) return METHOD;
+        if("bgget".equals(Script.toString(key))) {
             action = "bgget";
             try {
-                up = JS.pause();
-            } catch(NotPauseableException e) {
+                Script.pause();
+            } catch(Pausable.NotPausableException e) {
                 throw new Error("should never happen");
             }
             return null;
@@ -46,36 +50,37 @@ public class Test extends JS {
     }
         
     public void put(JS key, JS val) throws JSExn {
-        if("bgput".equals(JS.toString(key))) {
-            action = JS.toString(val);
+        if("bgput".equals(Script.toString(key))) {
+            action = Script.toString(val);
             try {
-                up = JS.pause();
-            } catch(NotPauseableException e) {
+                Script.pause();
+            } catch(Pausable.NotPausableException e) {
                 throw new Error("should never happen");
             }
             return;
         }   
-        if("exit".equals(JS.toString(key))) {
-            System.exit(JS.toInt(val));
+        if("exit".equals(Script.toString(key))) {
+            System.exit(Script.toInt(val));
             return;
         }
         super.put(key,val);
     }
     
-    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));
+    public JS call(JS method, JS[] args) throws JSExn {
+        if(!Script.isString(method)) return null;
+        if("print".equals(Script.toString(method))) {
+            System.out.println(Script.str(args[0]));
             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"));
+        if("clone".equals(Script.toString(method)))
+            return args.length < 1 || args[0] == null ? null : new JS.Clone(args[0]);
+        if("firethis".equals(Script.toString(method))) {
+            String action = Script.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,Script.S("some value"));
+            else if(action.equals("trigger")) return target.justTriggerTraps(key,Script.S("some trigger value"));
             return null;
         }
         return null;