X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2Fjs%2FJSScope.java;h=3e797f3d4e3808923c0969c964b517e5d52f0819;hb=6261c41b2ac9d182d8c3541e8e0e5fd00062fa43;hp=54dc44650dc232d32f30ed6f4fb95373e6f46a39;hpb=d3ccf61eac01b22a5133b876b9e19581015de13e;p=org.ibex.core.git diff --git a/src/org/xwt/js/JSScope.java b/src/org/xwt/js/JSScope.java index 54dc446..3e797f3 100644 --- a/src/org/xwt/js/JSScope.java +++ b/src/org/xwt/js/JSScope.java @@ -5,74 +5,87 @@ import org.xwt.util.*; import java.io.*; import java.util.*; -/** Implementation of a JavaScript JSScope */ -public class JSScope extends JSCallable { - private JSScope parentJSScope; +/** Implementation of a JavaScript Scope */ +public class JSScope extends JS { + + private JSScope parentScope; + 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 JSScope(JSScope parentScope) { this.parentScope = parentScope; } + public void declare(String s) throws JSExn { super.put(s, NULL_PLACEHOLDER); } + public JSScope getParentScope() { return parentScope; } - public Object get(Object key) { + public Object get(Object key) throws JSExn { Object o = super.get(key); if (o != null) return o == NULL_PLACEHOLDER ? null : o; - else return parentJSScope == null ? null : parentJSScope.get(key); + else return parentScope == null ? null : parentScope.get(key); } - public void put(Object key, Object val) { - if (parentJSScope != null && !has(key)) parentJSScope.put(key, val); + public boolean has(Object key) throws JSExn { return super.get(key) != null; } + public void put(Object key, Object val) throws JSExn { + if (parentScope != null && !has(key)) parentScope.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 call(Object method, JSArray args) { - if (method.equals("stringFromCharCode")) return stringFromCharCode(args); - return super.call(method, args); - } - public Object call1(Object method, Object arg0) { - //#switch(method) - case "parseInt": return parseInt(arg0, N(0)); - case "isNaN": { double d = toDouble(arg0); return d == d ? F : T; } - case "isFinite": { double d = toDouble(arg0); return (d == d && !Double.isInfinite(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"); + public Global() { super(null); } + public Object get(Object key) throws JSExn { + //#switch(key) + case "NaN": return NaN; + case "Infinity": return POSITIVE_INFINITY; + case "undefined": return null; + case "stringFromCharCode": return METHOD; + case "parseInt": return METHOD; + case "isNaN": return METHOD; + case "isFinite": return METHOD; + case "decodeURI": return METHOD; + case "decodeURIComponent": return METHOD; + case "encodeURI": return METHOD; + case "encodeURIComponent": return METHOD; + case "escape": return METHOD; + case "unescape": return METHOD; + case "parseInt": return METHOD; //#end - return super.call1(method, arg0); - } - - public Object call2(Object method, Object arg1, Object arg2) { - if (method.equals("parseInt")) return parseInt(arg1, arg2); - return super.call2(method, arg1, arg2); + return super.get(key); } - private Object stringFromCharCode(JSArray args) { - char buf[] = new char[args.length()]; - for(int i=0;i