2003/11/13 05:04:22
[org.ibex.core.git] / src / org / xwt / js / JSScope.java
diff --git a/src/org/xwt/js/JSScope.java b/src/org/xwt/js/JSScope.java
new file mode 100644 (file)
index 0000000..bdf8233
--- /dev/null
@@ -0,0 +1,132 @@
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] 
+package org.xwt.js; 
+
+import org.xwt.util.*; 
+import java.io.*;
+import java.util.*;
+
+/** Implementation of a JavaScript JSScope */
+public class JSScope extends JSCallable { 
+    private JSScope parentJSScope;
+    private static final Object NULL_PLACEHOLDER = new Object();
+    public JSScope(JSScope parentJSScope) {
+        if (parentJSScope == this) throw new Error("can't make a scope its own parent!");
+        this.parentJSScope = parentJSScope;
+    }
+
+    public boolean isTransparent() { return false; }
+    public void declare(String s) { super.put(s, NULL_PLACEHOLDER); }
+    public JSScope getParentJSScope() { return parentJSScope; }
+    public boolean has(Object key) { return super.get(key) != null; }
+
+    public Object get(Object key) {
+        Object o = super.get(key);
+        if (o != null) return o == NULL_PLACEHOLDER ? null : o;
+        else return parentJSScope == null ? null : parentJSScope.get(key);
+    }
+
+    public void put(Object key, Object val) {
+        if (parentJSScope != null && !has(key)) parentJSScope.put(key, val);
+        else super.put(key, val == null ? NULL_PLACEHOLDER : val);
+    }
+
+    public static class Global extends JSScope {
+        private final static Double NaN = new Double(Double.NaN);
+        private final static Double POSITIVE_INFINITY = new Double(Double.POSITIVE_INFINITY);
+    
+        public Global(JSScope parent) {
+            super(parent);
+        }
+        public Object get(Object key) {
+            if(key.equals("NaN")) return NaN;
+            if(key.equals("Infinity")) return POSITIVE_INFINITY;
+            if(key.equals("undefined")) return null;
+            return super.get(key);
+        }
+
+        public Object call1(Object method, Object arg0) {
+            //#switch(method)
+            case "parseInt": return parseInt(arg0, 0);
+            case "isNaN": { double d = toDouble(arg0); return d == d ? F : T; }
+            case "isFinite": { double d = toDouble(arg0); return (d == d && !Double.isFinite(d)) ? T : F; }
+            case "decodeURI": throw new JS.Exn("unimplemented");
+            case "decodeURIComponent": throw new JS.Exn("unimplemented");
+            case "encodeURI": throw new JS.Exn("unimplemented");
+            case "encodeURIComponent": throw new JS.Exn("unimplemented");
+            case "escape": throw new JS.Exn("unimplemented");
+            case "unescape": throw new JS.Exn("unimplemented");
+            case "stringFromCharCode": return stringFromCharCode(arg0);
+            //#end
+            return null;
+        }
+
+        public Object call2(Object method, Object arg1, Object arg2) {
+            if (method.equals("parseInt")) return parseInt(arg1, arg2);
+            return null;
+        }
+
+        private Object stringFromCharCode(Object arg) {
+            char buf[] = new char[args.length()];
+            for(int i=0;i<args.length();i++) buf[i] = (char)(JS.toInt(arg) & 0xffff);
+            return new String(buf);
+        }
+
+        private Object parseInt(Object arg, int radix) {
+            String s = (String)arg;
+            int start = 0;
+            int length = s.length();
+            int sign = 1;
+            long n = 0;
+            if(radix != 0 && (radix < 2 || radix > 36)) return NaN;
+            while(start < length && Character.isWhitespace(s.charAt(start))) start++;
+            if((length >= start+1) && (s.charAt(start) == '+' || s.charAt(start) == '-')) {
+                sign = s.charAt(start) == '+' ? 1 : -1;
+                start++;
+            }
+            if(radix == 0 && length >= start+1 && s.charAt(start) == '0') {
+                start++;
+                if(length >= start+1 && (s.charAt(start) == 'x' || s.charAt(start) == 'X')) {
+                    start++;
+                    radix = 16;
+                } else {
+                    radix = 8;
+                    if(length == start || Character.digit(s.charAt(start+1),8)==-1) return new Integer(0);
+                }
+            }
+            if(radix == 0) radix = 10;
+            if(length == start || Character.digit(s.charAt(start),radix) == -1) return NaN;
+            // try the fast way first
+            try {
+                String s2 = start == 0 ? s : s.substring(start);
+                return new Integer(sign*Integer.parseInt(s2,radix));
+            } catch(NumberFormatException e) { }
+            // fall through to a slower but emca-compliant method
+            for(int i=start;i<length;i++) {
+                int digit = Character.digit(s.charAt(i),radix);
+                if(digit < 0) break;
+                n = n*radix + digit;
+                if(n < 0) return NaN; // overflow;
+            }
+            if(n <= Integer.MAX_VALUE) return new Integer(sign*(int)n);
+            return new Long((long)sign*n);
+        }
+
+        private Object parseFloat(Object arg) {
+            String s = (String)arg;
+            int start = 0;
+            int length = s.length();
+            while(start < length && Character.isWhitespace(s.charAt(0))) start++;
+            int end = length;
+            // as long as the string has no trailing garbage,this is fast, its slow with
+            // trailing garbage
+            while(start < end) {
+                try {
+                    return new Double(s.substring(start,length));
+                } catch(NumberFormatException e) { }
+                end--;
+            }
+            return NaN;
+        }
+    }
+}
+