2003/06/16 08:03:15
[org.ibex.core.git] / src / org / xwt / js / ScopeImpl.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] 
2
3 package org.xwt.js; 
4 import org.xwt.util.*; 
5 import java.io.*;
6 import java.util.*;
7
8 /** A JavaScript Scope */
9 class ScopeImpl extends JS.Obj { 
10     private JS.Scope parentScope;
11     private static Object NULL = new Object();
12     public ScopeImpl(JS.Scope parentScope) { this(parentScope, false); }
13     public ScopeImpl(JS.Scope parentScope, boolean sealed) {
14         super(sealed);
15         if (parentScope == this) throw new Error("can't make a scope its own parent!");
16         this.parentScope = parentScope;
17     }
18     public JS.Scope getParentScope() { return parentScope; }
19     
20     // transparent scopes are not returned by THIS
21     public boolean isTransparent() { return false; }
22     
23     public boolean has(Object key) { return super.get(key) != null; }
24     public Object get(Object key) {
25         if (!has(key)) return parentScope == null ? null : getParentScope().get(key);
26         Object ret = super.get(key); return ret == NULL ? null : ret;
27     }
28     public void put(Object key, Object val) {
29         if (!has(key) && parentScope != null) getParentScope().put(key, val);
30         else super.put(key, val == null ? NULL : val);
31     }
32     public Object[] keys() { throw new Error("you can't enumerate the properties of a Scope"); }
33     public void declare(String s) {
34         if (isTransparent()) getParentScope().declare(s);
35         else super.put(s, NULL);
36     }
37