remove JS.call(JS[]), JS.getInputStream()
authoradam <adam@megacz.com>
Sat, 15 Jan 2005 13:43:05 +0000 (13:43 +0000)
committeradam <adam@megacz.com>
Sat, 15 Jan 2005 13:43:05 +0000 (13:43 +0000)
darcs-hash:20050115134305-5007d-e7b5c2a70dea15398ef50fa666a6934631ddca27.gz

src/org/ibex/js/Interpreter.java
src/org/ibex/js/JS.java
src/org/ibex/js/JSArray.java
src/org/ibex/js/JSU.java

index 611d2d8..e20e630 100644 (file)
@@ -379,7 +379,7 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
                     break;
                 } else {
                     JS c = (JS)object;
-                    ret = method == null ? c.call(jsargs) : c.call(method, jsargs);
+                    ret = c.call(method, jsargs);
                 }
                 
                 if (pausecount > initialPauseCount) { pc++; return null; }
@@ -663,7 +663,10 @@ class Interpreter implements ByteCodes, Tokens, Pausable {
         private JS method;
         JS obj;
         public Stub(JS obj, JS method) { this.obj = obj; this.method = method; }
-        public JS call(JS[] args) throws JSExn { return obj.call(method, args); }
+        public JS call(JS method, JS[] args) throws JSExn {
+            if (method==null) return obj.call(this.method, args);
+            return super.call(method, args);
+        }
     }
     
     static final class Stack {
index e2fe3e3..5355580 100644 (file)
@@ -32,11 +32,6 @@ public interface JS extends Pausable {
      *  arguments are invalid. */
     public JS call(JS method, JS[] args) throws JSExn;
 
-    /** Calls this object with the given arguments, running it in an
-     *  <i>unpausable</i> context. An exception is thrown if this
-     *  object is not callable or the arguments are invalid. */
-    public JS call(JS[] args) throws JSExn;
-
     /** Returns the names of the formal arguments, if any.
      *  This function must never return a null object. */
     public String[] getFormalArgs();
@@ -68,9 +63,8 @@ public interface JS extends Pausable {
     public Trap getTrap(JS key) throws JSExn;
 
     // FIXME: consider renaming/removing these
-    public InputStream getInputStream() throws IOException, JSExn;
     public JS unclone();
-    public String coerceToString();
+    public String coerceToString() throws JSExn;
 
 
     // Implementations ////////////////////////////////////////////////////////
@@ -81,11 +75,11 @@ public interface JS extends Pausable {
         private static final String[] emptystr = new String[0];
 
         public JS unclone() { return this; }
-        public JS.Enumeration keys() throws JSExn { throw new JSExn(
-            "object has no key set, class ["+ getClass().getName() +"]"); }
+        public JS.Enumeration keys() throws JSExn {
+            throw new JSExn("object has no key set, class ["+ getClass().getName() +"]"); }
         public JS get(JS key) throws JSExn { return null; }
-        public void put(JS key, JS val) throws JSExn { throw new JSExn(
-            "'" + key + "' is read only on class ["+ getClass().getName() +"]"); }
+        public void put(JS key, JS val) throws JSExn {
+            throw new JSExn("'" + key + "' is read only on class ["+ getClass().getName() +"]"); }
         public InputStream getInputStream() throws IOException, JSExn { throw new JSExn(
             "object has not associated stream, class ["+ getClass().getName() +"]"); }
 
@@ -93,17 +87,12 @@ public interface JS extends Pausable {
             "object cannot be called, class ["+ getClass().getName() +"]"); }
         public void pause() { throw new NotPausableException(); }
 
-        public JS call(JS[] args) throws JSExn { throw new JSExn( "object cannot be called, class ["+ getClass().getName() +"]"); }
         public JS call(JS method, JS[] args) throws JSExn {
-            if (method == null) return call(args);
+            if (method == null) throw new JSExn( "object cannot be called, class ["+ getClass().getName() +"]");
             throw new JSExn("method not found: " + JSU.str(method) + " class="+this.getClass().getName());
         }
         public String[] getFormalArgs() { return emptystr; }
 
-        public void declare(JS key) throws JSExn { throw new JSExn(
-            "object cannot declare key: "+ JSU.str(key)); }
-        public void undeclare(JS key) throws JSExn { } // FIXME throw error?
-
         public JS putAndTriggerTraps(JS key, JS val) throws JSExn { throw new JSExn(
             "'" + key + "' is trap read only on class ["+ getClass().getName() +"]"); }
         public JS getAndTriggerTraps(JS key) throws JSExn { return null; } // FIXME throw errors?
@@ -117,8 +106,8 @@ public interface JS extends Pausable {
             //Log.warn(this, "'" + JSU.str(key) + "' is not trappable on class ["+ getClass().getName() +"]");
             return null;
         }
-
-        public String coerceToString() { return "object"; }
+        public String coerceToString() throws JSExn {
+            throw new JSExn("cannot coerce a " + getClass().getName() + " to a string"); }
     }
 
     public interface Cloneable {}
@@ -126,39 +115,29 @@ public interface JS extends Pausable {
     public static class Clone implements JS {
         protected final JS clonee;
         public Clone(JS clonee) throws JSExn {
-            if (!(clonee instanceof Cloneable)) throw new JSExn(
-                clonee.getClass().getName() + " is not implement cloneable");
+            if (!(clonee instanceof Cloneable)) throw new JSExn(clonee.getClass().getName() + " is not implement cloneable");
             this.clonee = clonee;
         }
-
         public JS unclone() { return clonee.unclone(); }
         public boolean equals(Object o) { return clonee.equals(o); }
-
         public Enumeration keys() throws JSExn { return clonee.keys(); }
         public JS get(JS k) throws JSExn { return clonee.get(k); }
         public void put(JS k, JS v) throws JSExn { clonee.put(k, v); }
-        public InputStream getInputStream() throws IOException, JSExn {
-            return clonee.getInputStream(); }
+
+        // FIMXE: cloning Fountains...
+        //public InputStream getInputStream() throws IOException, JSExn { return clonee.getInputStream(); }
 
         public Object run(Object o) throws Exception { return clonee.run(o); }
         public void pause() { clonee.pause(); }
-
         public JS call(JS m, JS[] a) throws JSExn { return clonee.call(m, a); }
-        public JS call(JS[] a) throws JSExn { return clonee.call(a); }
         public String[] getFormalArgs() { return clonee.getFormalArgs(); }
-
-        public JS putAndTriggerTraps(JS k, JS v) throws JSExn {
-            return clonee.putAndTriggerTraps(k, v); }
-        public JS getAndTriggerTraps(JS k) throws JSExn {
-            return clonee.getAndTriggerTraps(k); }
-        public JS justTriggerTraps(JS k, JS v) throws JSExn {
-            return clonee.justTriggerTraps(k, v); }
-
+        public JS putAndTriggerTraps(JS k, JS v) throws JSExn { return clonee.putAndTriggerTraps(k, v); }
+        public JS getAndTriggerTraps(JS k) throws JSExn { return clonee.getAndTriggerTraps(k); }
+        public JS justTriggerTraps(JS k, JS v) throws JSExn { return clonee.justTriggerTraps(k, v); }
         public void addTrap(JS k, JS f) throws JSExn { clonee.addTrap(k, f); }
         public void delTrap(JS k, JS f) throws JSExn { clonee.delTrap(k, f); }
         public Trap getTrap(JS k) throws JSExn { return clonee.getTrap(k); }
-
-        public String coerceToString() { return clonee.coerceToString(); }
+        public String coerceToString() throws JSExn { return clonee.coerceToString(); }
     }
 
     public static class Obj extends Basket.HashMap implements JS {
@@ -168,17 +147,14 @@ public interface JS extends Pausable {
         public Obj() { super(3, 4, 0.75F); }
 
         public JS unclone() { return this; }
-        public InputStream getInputStream() throws IOException, JSExn { throw new JSExn(
-            "object has not associated stream, class ["+ getClass().getName() +"]"); }
 
-        public Object run(Object o) throws Exception { throw new JSExn(
-            "object cannot be called, class ["+ getClass().getName() +"]"); }
+        public Object run(Object o) throws Exception {
+            throw new JSExn("object cannot be called, class ["+ getClass().getName() +"]"); }
         public void pause() { throw new NotPausableException(); }
 
-        public JS call(JS[] args) throws JSExn { throw new JSExn(
-            "object cannot be called, class ["+ getClass().getName() +"]"); }
-        public JS call(JS method, JS[] args) throws JSExn { throw new JSExn(
-            "method not found: " + JSU.str(method)); }
+        public JS call(JS method, JS[] args) throws JSExn {
+            if (method==null) throw new JSExn("cannot call a " + getClass().getName());
+            throw new JSExn("method not found: " + JSU.str(method)); }
         public String[] getFormalArgs() { return emptystr; }
 
         public Enumeration keys() throws JSExn {
@@ -196,19 +172,10 @@ public interface JS extends Pausable {
         public JS get(JS key) throws JSExn { return (JS)super.get(key, 0); }
         public void put(JS key, JS val) throws JSExn { super.put(key, val, 0); }
 
-        /*public boolean hasValue(JS key, JS value) {
-            int i = indexOf(key); return i >= 0 && entries[i + 1] != null; }
-        public boolean hasTrap(JS key, JS trap) {
-            int i = indexOf(key); return i >= 0 && entries[i + 2] != null; }*/
-
         public JS putAndTriggerTraps(JS key, JS val) throws JSExn {
             Trap t = (Trap)super.get(key, 1);
-            if (t != null && (t = t.write()) != null)
-                val = (JS)new Interpreter(t, val, false).run(null);
+            if (t != null && (t = t.write()) != null) val = (JS)new Interpreter(t, val, false).run(null);
             put(key, val); // HACK: necessary for subclasses overriding put()
-            /*if (i < 0) i = put(i, key);
-            if (val == null) entries[i + 1] = holder;
-            else entries[i + 1] = val;*/
             return val;
         }
         public JS getAndTriggerTraps(JS key) throws JSExn {
@@ -239,7 +206,8 @@ public interface JS extends Pausable {
 
         public Trap getTrap(JS key) throws JSExn { return (Trap)super.get(key, 1); }
 
-        public String coerceToString() { return "object"; }
+        public String coerceToString() throws JSExn {
+            throw new JSExn("cannot coerce a " + getClass().getName() + " to a string"); }
 
         private static final class Placeholder implements Serializable {}
 
@@ -247,21 +215,14 @@ public interface JS extends Pausable {
             private final JS target, key, function;
             private Trap next;
             TrapHolder(JS t, JS k, JS f, Trap n) { target = t; key = k; function = f; next = n; }
-
             public JS key() { return key; }
             public JS target() { return target; }
             public JS function() { return function; }
-
-            public boolean isReadTrap()  {
-                return function.getFormalArgs() == null || function.getFormalArgs().length == 0; }
-            public boolean isWriteTrap() {
-                return function.getFormalArgs() != null && function.getFormalArgs().length != 0; }
-
+            public boolean isReadTrap()  { return function.getFormalArgs() == null || function.getFormalArgs().length == 0; }
+            public boolean isWriteTrap() { return function.getFormalArgs() != null && function.getFormalArgs().length != 0; }
             public Trap next() { return next; }
-            public Trap nextRead() {
-                Trap t = next; while (t != null && t.isWriteTrap()) t = t.next(); return t; }
-            public Trap nextWrite() {
-                Trap t = next; while (t != null && t.isReadTrap()) t = t.next(); return t; }
+            public Trap nextRead() { Trap t = next; while (t != null && t.isWriteTrap()) t = t.next(); return t; }
+            public Trap nextWrite() { Trap t = next; while (t != null && t.isReadTrap()) t = t.next(); return t; }
             public Trap read() { return isReadTrap() ? this : nextRead(); }
             public Trap write() { return isWriteTrap() ? this : nextWrite(); }
         }
@@ -296,8 +257,7 @@ public interface JS extends Pausable {
         protected abstract boolean _hasNext();
         protected abstract JS _next() throws JSExn;
 
-        public final boolean hasNext() {
-            return _hasNext() || (parent != null ? parent.hasNext() : false); }
+        public final boolean hasNext() { return _hasNext() || (parent != null ? parent.hasNext() : false); }
         public final JS next() throws JSExn {
             if (_hasNext()) return _next();
             if (parent == null) throw new NoSuchElementException("reached end of set");
@@ -328,8 +288,7 @@ public interface JS extends Pausable {
         public static final class RandomAccessList extends Enumeration {
             private final List l;
             private int pos = 0, size;
-            public RandomAccessList(Enumeration parent, List list) {
-                super(parent); l = list; size = l.size(); }
+            public RandomAccessList(Enumeration parent, List list) { super(parent); l = list; size = l.size(); }
             protected boolean _hasNext() { return  pos < size; }
             protected JS _next() { return (JS)l.get(pos++); }
         }
index 9124761..993e057 100644 (file)
@@ -54,14 +54,12 @@ public class JSArray extends Basket.Array implements JS, Basket.CompareFunc {
         size(i + 1); while (size() < i) add(null);
         set(i, val);
     }
-    public InputStream getInputStream() { return null; }
 
     public String[] getFormalArgs() { return empty; }
     public String coerceToString() { return "array"; } // FIXME
 
-    public Object run(Object o) throws JSExn { return call(null); }
+    public Object run(Object o) throws JSExn { return call(null, null); }
     public void pause() throws NotPausableException { throw new NotPausableException(); }
-    public JS call(JS[] args) throws JSExn { throw new JSExn("can not call an array as a function"); }
     public JS call(JS method, JS[] args) throws JSExn {
         //#switch(JSU.str(method))
         case "pop": return size() == 0 ? null : (JS)remove(size() - 1);
@@ -80,7 +78,6 @@ public class JSArray extends Basket.Array implements JS, Basket.CompareFunc {
             return JSU.N(size());
         case "splice": return splice(args);
         //#end
-
         throw new JSExn("arrays have no function: "+JSU.str(method));
     }
 
@@ -182,7 +179,7 @@ public class JSArray extends Basket.Array implements JS, Basket.CompareFunc {
     public int compare(Object a, Object b) throws JSExn.Wrapper {
         try {
             sortargs[0] = (JS)a; sortargs[1] = (JS)b;
-            return JSU.toInt(sort.call(sortargs));
+            return JSU.toInt(sort.call(null, sortargs));
         } catch (JSExn e) { throw new JSExn.Wrapper(e);
         } finally { sortargs[0] = null; sortargs[1] = null; }
     }
index a20d075..9fa3630 100644 (file)
@@ -2,12 +2,16 @@ package org.ibex.js;
 
 import java.io.Reader;
 import java.io.IOException;
+import java.io.InputStream;
 import org.ibex.util.*;
 
 public class JSU {
 
     public static final JS[] emptyArgs = new JS[] { };
 
+    // FIXME
+    public static InputStream getInputStream(JS j) { return null; }
+
     /** returns a Pausable which will restart the context;
      *  expects a value to be pushed onto the stack when unpaused. */
     public static Pausable pause() throws Pausable.NotPausableException {