Use common API to extract value associated with top scope prototype chain

not to duplicate code
This commit is contained in:
igor%mir2.org 2004-08-14 13:57:39 +00:00
Родитель b198395e8d
Коммит 682b343169
3 изменённых файлов: 45 добавлений и 27 удалений

Просмотреть файл

@ -81,21 +81,13 @@ public class ClassCache
*/
public static ClassCache get(Scriptable scope)
{
scope = ScriptableObject.getTopLevelScope(scope);
Scriptable obj = scope;
do {
if (obj instanceof ScriptableObject) {
ScriptableObject so = (ScriptableObject)obj;
ClassCache lc = (ClassCache)so.getAssociatedValue(AKEY);
if (lc != null) {
return lc;
}
}
obj = obj.getPrototype();
} while (obj != null);
// ALERT: warn somehow about wrong cache usage ?
return new ClassCache();
ClassCache cache;
cache = (ClassCache)ScriptableObject.getTopScopeValue(scope, AKEY);
if (cache == null) {
// XXX warn somehow about wrong cache usage ?
cache = new ClassCache();
}
return cache;
}
/**

Просмотреть файл

@ -114,6 +114,7 @@ public class ScriptRuntime {
};
private static final Object LIBRARY_SCOPE_KEY = new Object();
private static final Object CONTEXT_FACTORY_KEY = new Object();
public static ScriptableObject initStandardObjects(Context cx,
ScriptableObject scope,
@ -122,7 +123,7 @@ public class ScriptRuntime {
if (scope == null) {
scope = new NativeObject();
}
scope.associateValue(LIBRARY_SCOPE_KEY, LIBRARY_SCOPE_KEY);
scope.associateValue(LIBRARY_SCOPE_KEY, scope);
(new ClassCache()).associate(scope);
@ -170,17 +171,13 @@ public class ScriptRuntime {
public static ScriptableObject getLibraryScope(Scriptable scope)
{
scope = ScriptableObject.getTopLevelScope(scope);
do {
if (scope instanceof ScriptableObject) {
ScriptableObject so = (ScriptableObject)scope;
if (null != so.getAssociatedValue(LIBRARY_SCOPE_KEY)) {
return so;
}
}
scope = scope.getPrototype();
} while (scope != null);
throw new IllegalStateException("Failed to find library scope");
ScriptableObject libScope;
libScope = (ScriptableObject)ScriptableObject.
getTopScopeValue(scope, LIBRARY_SCOPE_KEY);
if (libScope == null) {
throw new IllegalStateException("Failed to find library scope");
}
return libScope;
}
public static Boolean wrapBoolean(boolean b)

Просмотреть файл

@ -1551,6 +1551,35 @@ public abstract class ScriptableObject implements Scriptable, Serializable,
return h.get(key);
}
/**
* Get arbitrary application-specific value associated with the top scope
* of the given scope.
* The method first calls {@link #getTopLevelScope(Scriptable scope)}
* and then searches the prototype chain of the top scope for the first
* object containing the associated value with the given key.
*
* @param scope the starting scope.
* @param key key object to select particular value.
* @see #getAssociatedValue(Object key)
*/
public static Object getTopScopeValue(Scriptable scope, Object key)
{
scope = ScriptableObject.getTopLevelScope(scope);
for (;;) {
if (scope instanceof ScriptableObject) {
ScriptableObject so = (ScriptableObject)scope;
Object value = so.getAssociatedValue(key);
if (value != null) {
return value;
}
}
scope = scope.getPrototype();
if (scope == null) {
return null;
}
}
}
/**
* Associate arbitrary application-specific value with this object.
* Value can only be associated with the given object and key only once.