зеркало из https://github.com/mozilla/pjs.git
Use common API to extract value associated with top scope prototype chain
not to duplicate code
This commit is contained in:
Родитель
b198395e8d
Коммит
682b343169
|
@ -81,21 +81,13 @@ public class ClassCache
|
||||||
*/
|
*/
|
||||||
public static ClassCache get(Scriptable scope)
|
public static ClassCache get(Scriptable scope)
|
||||||
{
|
{
|
||||||
scope = ScriptableObject.getTopLevelScope(scope);
|
ClassCache cache;
|
||||||
Scriptable obj = scope;
|
cache = (ClassCache)ScriptableObject.getTopScopeValue(scope, AKEY);
|
||||||
do {
|
if (cache == null) {
|
||||||
if (obj instanceof ScriptableObject) {
|
// XXX warn somehow about wrong cache usage ?
|
||||||
ScriptableObject so = (ScriptableObject)obj;
|
cache = new ClassCache();
|
||||||
ClassCache lc = (ClassCache)so.getAssociatedValue(AKEY);
|
|
||||||
if (lc != null) {
|
|
||||||
return lc;
|
|
||||||
}
|
}
|
||||||
}
|
return cache;
|
||||||
obj = obj.getPrototype();
|
|
||||||
} while (obj != null);
|
|
||||||
|
|
||||||
// ALERT: warn somehow about wrong cache usage ?
|
|
||||||
return new ClassCache();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -114,6 +114,7 @@ public class ScriptRuntime {
|
||||||
};
|
};
|
||||||
|
|
||||||
private static final Object LIBRARY_SCOPE_KEY = new Object();
|
private static final Object LIBRARY_SCOPE_KEY = new Object();
|
||||||
|
private static final Object CONTEXT_FACTORY_KEY = new Object();
|
||||||
|
|
||||||
public static ScriptableObject initStandardObjects(Context cx,
|
public static ScriptableObject initStandardObjects(Context cx,
|
||||||
ScriptableObject scope,
|
ScriptableObject scope,
|
||||||
|
@ -122,7 +123,7 @@ public class ScriptRuntime {
|
||||||
if (scope == null) {
|
if (scope == null) {
|
||||||
scope = new NativeObject();
|
scope = new NativeObject();
|
||||||
}
|
}
|
||||||
scope.associateValue(LIBRARY_SCOPE_KEY, LIBRARY_SCOPE_KEY);
|
scope.associateValue(LIBRARY_SCOPE_KEY, scope);
|
||||||
|
|
||||||
(new ClassCache()).associate(scope);
|
(new ClassCache()).associate(scope);
|
||||||
|
|
||||||
|
@ -170,18 +171,14 @@ public class ScriptRuntime {
|
||||||
|
|
||||||
public static ScriptableObject getLibraryScope(Scriptable scope)
|
public static ScriptableObject getLibraryScope(Scriptable scope)
|
||||||
{
|
{
|
||||||
scope = ScriptableObject.getTopLevelScope(scope);
|
ScriptableObject libScope;
|
||||||
do {
|
libScope = (ScriptableObject)ScriptableObject.
|
||||||
if (scope instanceof ScriptableObject) {
|
getTopScopeValue(scope, LIBRARY_SCOPE_KEY);
|
||||||
ScriptableObject so = (ScriptableObject)scope;
|
if (libScope == null) {
|
||||||
if (null != so.getAssociatedValue(LIBRARY_SCOPE_KEY)) {
|
|
||||||
return so;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
scope = scope.getPrototype();
|
|
||||||
} while (scope != null);
|
|
||||||
throw new IllegalStateException("Failed to find library scope");
|
throw new IllegalStateException("Failed to find library scope");
|
||||||
}
|
}
|
||||||
|
return libScope;
|
||||||
|
}
|
||||||
|
|
||||||
public static Boolean wrapBoolean(boolean b)
|
public static Boolean wrapBoolean(boolean b)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1551,6 +1551,35 @@ public abstract class ScriptableObject implements Scriptable, Serializable,
|
||||||
return h.get(key);
|
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.
|
* Associate arbitrary application-specific value with this object.
|
||||||
* Value can only be associated with the given object and key only once.
|
* Value can only be associated with the given object and key only once.
|
||||||
|
|
Загрузка…
Ссылка в новой задаче