X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2Fjs%2FJSScope.java;h=a504d8d6c5f9ffeac16a05582fe6560988a8671a;hb=6648a20efb79368af2fb16d1694afa685c04a0fc;hp=44bfec1462a54341da05dad53cf3cd613c4b0de6;hpb=8c1756ef3fd42cc2f324baf47e13a83f51045efe;p=org.ibex.core.git diff --git a/src/org/xwt/js/JSScope.java b/src/org/xwt/js/JSScope.java index 44bfec1..a504d8d 100644 --- a/src/org/xwt/js/JSScope.java +++ b/src/org/xwt/js/JSScope.java @@ -1,42 +1,46 @@ -// 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.*; import java.io.*; import java.util.*; -/** Implementation of a JavaScript JSScope */ -public class JSScope extends JSCallable { - private JSScope parentJSScope; +// FIXME: should allow parentScope to be a JS, not a JSScope +/** 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 JSScope top() { + JSScope s = this; + while(s.parentScope != null) s = s.parentScope; + return s; + } 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) { + public Global() { super(null); } + public Object get(Object key) throws JSExn { //#switch(key) case "NaN": return NaN; case "Infinity": return POSITIVE_INFINITY; @@ -56,47 +60,41 @@ public class JSScope extends JSCallable { return super.get(key); } - public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) { + public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn { switch(nargs) { case 0: { //#switch(method) case "stringFromCharCode": - JSArray args = new JSArray(); - for(int i=0; i