2004/01/19 00:16:54
[org.ibex.core.git] / src / org / xwt / js / JS.java
index f2a7c20..c82435b 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] 
+// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] 
 package org.xwt.js; 
 
 import org.xwt.util.*; 
@@ -9,26 +9,54 @@ import java.util.*;
 /** The minimum set of functionality required for objects which are manipulated by JavaScript */
 public class JS extends org.xwt.util.BalancedTree { 
 
-    // Static Interpreter Control Methods ///////////////////////////////////////////////////////////////
+    public static final Object METHOD = new Object();
+    public final JS unclone() { return _unclone(); }
+    public Enumeration keys() throws JSExn { return entries == null ? emptyEnumeration : entries.keys(); }
+    public Object get(Object key) throws JSExn { return entries == null ? null : entries.get(key, null); }
+    public void put(Object key, Object val) throws JSExn { (entries==null?entries=new Hash():entries).put(key,null,val); }
+    public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
+        throw new JSExn("attempted to call the null value (method "+method+")");
+    }    
+    public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
+        throw new JSExn("you cannot call this object (class=" + this.getClass().getName() +")");
+    }
 
-    public static int getLine() {
-        Interpreter c = Interpreter.current();
-        return c == null || c.f == null || c.pc < 0 || c.pc >= c.f.size ? -1 : c.f.line[c.pc];
+    JS _unclone() { return this; }
+    public static class Cloneable extends JS {
+        public Cloneable() { }
+        public Object jsclone() throws JSExn {
+            throw new JSExn("cloning not yet implemented");
+        }
     }
 
-    public static String getSourceName() {
-        Interpreter c = Interpreter.current();
-        return c == null || c.f == null ? null : c.f.sourceName;
-    } 
+    public static class Clone extends JS.Cloneable {
+        protected JS.Cloneable clonee = null;
+        JS _unclone() { return clonee.unclone(); }
+        public JS.Cloneable getClonee() { return clonee; }
+        public Clone(JS.Cloneable clonee) { this.clonee = clonee; }
+        public boolean equals(Object o) {
+            if (!(o instanceof JS)) return false;
+            return unclone() == ((JS)o).unclone();
+        }
+        public Enumeration keys() throws JSExn { return clonee.keys(); }
+        public Object get(Object key) throws JSExn { return clonee.get(key); }
+        public void put(Object key, Object val) throws JSExn { clonee.put(key, val); }
+        public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
+            return clonee.callMethod(method, a0, a1, a2, rest, nargs);
+        }    
+        public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
+            return clonee.call(a0, a1, a2, rest, nargs);
+        }
+    }
 
-    public static class PausedException extends Exception { PausedException() { } }
+    // Static Interpreter Control Methods ///////////////////////////////////////////////////////////////
 
-    public static void invokePauseable(JSFunction function) throws JS.PausedException, JSExn {
-        Interpreter i = new Interpreter(function, true, new JSArray());
-        int oldpausecount = i.pausecount;
-        i.resume();
-        if (i.pausecount > oldpausecount) throw new PausedException();
-    }
+    /** log a message with the current JavaScript sourceName/line */
+    public static void log(Object message) { info(message); }
+    public static void debug(Object message) { Log.debug(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); }
+    public static void info(Object message) { Log.info(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); }
+    public static void warn(Object message) { Log.warn(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); }
+    public static void error(Object message) { Log.error(Interpreter.getSourceName() + ":" + Interpreter.getLine(), message); }
 
     public static class NotPauseableException extends Exception { NotPauseableException() { } }
 
@@ -40,10 +68,12 @@ public class JS extends org.xwt.util.BalancedTree {
         return new JS.UnpauseCallback(i);
     }
 
-    public static class UnpauseCallback {
+    public static class UnpauseCallback implements Scheduler.Task {
         Interpreter i;
         UnpauseCallback(Interpreter i) { this.i = i; }
-        public void unpause(Object o) throws PausedException, JSExn {
+        public void perform() throws JSExn { unpause(null); }
+        public void unpause(Object o) throws JSExn {
+            // FIXME: if o instanceof JSExn, throw it into the JSworld
             i.stack.push(o);
             i.resume();
         }
@@ -84,10 +114,8 @@ public class JS extends org.xwt.util.BalancedTree {
 
         // NOTE: There are about 3 pages of rules in ecma262 about string to number conversions
         //       We aren't even close to following all those rules.  We probably never will be.
-        if (o instanceof String)
-            try { return N((String)o); } catch (NumberFormatException e) { return N(Double.NaN); }
+        if (o instanceof String) try { return N((String)o); } catch (NumberFormatException e) { return N(Double.NaN); }
         if (o instanceof Boolean) return ((Boolean)o).booleanValue() ? N(1) : ZERO;
-        if (o instanceof JS) return ((JS)o).coerceToNumber();
         throw new Error("toNumber() got object of type " + o.getClass().getName() + " which we don't know how to handle");
     }
 
@@ -96,16 +124,16 @@ public class JS extends org.xwt.util.BalancedTree {
         if(o == null) return "null";
         if(o instanceof String) return (String) o;
         if(o instanceof Integer || o instanceof Long || o instanceof Boolean) return o.toString();
-        if(o instanceof JS) return ((JS)o).coerceToString();
+        if(o instanceof JSArray) return o.toString();
+        if(o instanceof JSDate) return o.toString();
         if(o instanceof Double || o instanceof Float) {
             double d = ((Number)o).doubleValue();
             if((int)d == d) return Integer.toString((int)d);
             return o.toString();
         }
-        return o.toString();
+        throw new RuntimeException("can't coerce that!");
     }
 
-    
     // Instance Methods ////////////////////////////////////////////////////////////////////
 
     public static final Integer ZERO = new Integer(0);
@@ -144,22 +172,23 @@ public class JS extends org.xwt.util.BalancedTree {
         };
     
     private Hash entries = null;
-    public Enumeration keys() throws JSExn {
-        return entries == null ? emptyEnumeration : entries.keys();
+
+    public static JS fromReader(String sourceName, int firstLine, Reader sourceCode) throws IOException {
+        return JSFunction._fromReader(sourceName, firstLine, sourceCode);
     }
-    public Object get(Object key) throws JSExn { return entries == null ? null : entries.get(key, null); }
-    public void put(Object key, Object val) throws JSExn {
-        if (entries == null) entries = new Hash();
-        entries.put(key, null, val);
+
+    // HACK: caller can't know if the argument is a JSFunction or not...
+    public static JS cloneWithNewParentScope(JS j, JSScope s) {
+        return ((JSFunction)j)._cloneWithNewParentScope(s);
     }
 
 
     // Trap support //////////////////////////////////////////////////////////////////////////////
 
     /** override and return true to allow placing traps on this object.
-        *  if isRead true, this is a read trap, otherwise write trap
-        **/
-    protected boolean isTrappable(Object name, boolean isRead) { return false; }
+     *  if isRead true, this is a read trap, otherwise write trap
+     **/
+    protected boolean isTrappable(Object name, boolean isRead) { return true; }
 
     /** performs a put, triggering traps if present; traps are run in an unpauseable interpreter */
     public void putAndTriggerTraps(Object key, Object value) throws JSExn {
@@ -204,24 +233,4 @@ public class JS extends org.xwt.util.BalancedTree {
     }
 
 
-    // Call Support //////////////////////////////////////////////////////////////////////////////
-
-    // return this from get() if the key was actually a method.
-    public static final Object METHOD = new Object();
-    public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
-        throw new JSExn("attempted to call the null value (method "+method+")");
-    }    
-    public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
-        throw new JSExn("you cannot call this object (class=" + this.getClass().getName() +")");
-    }
-
-
-    // Typing Support //////////////////////////////////////////////////////////////////////////////
-
-    public Number coerceToNumber() { throw new JSRuntimeExn("tried to coerce a JavaScript object to a Number"); }
-    public String coerceToString() { throw new JSRuntimeExn("tried to coerce a JavaScript object to a String"); }
-    public boolean coerceToBoolean() { throw new JSRuntimeExn("tried to coerce a JavaScript object to a Boolean"); }
-
-    public String typeName() { return "object"; }
-
 }