* replaced helma.util.CacheMap with java.util.WeakHashMap for wrapper caching

* do wrapper caching for IPathElement (java object) wrappers
* only wrap instances of helma.util.SystemMap with MapWrappers
* do not wrap Strings.
This commit is contained in:
hns 2003-06-16 18:56:53 +00:00
parent 3691b6d977
commit 5f46bdf3a2

View file

@ -24,6 +24,8 @@ import helma.objectmodel.db.DbMapping;
import helma.objectmodel.db.Relation; import helma.objectmodel.db.Relation;
import helma.scripting.*; import helma.scripting.*;
import helma.util.CacheMap; import helma.util.CacheMap;
import helma.util.SystemMap;
import helma.util.SystemProperties;
import helma.util.Updatable; import helma.util.Updatable;
import org.mozilla.javascript.*; import org.mozilla.javascript.*;
import java.io.*; import java.io.*;
@ -40,7 +42,7 @@ public final class RhinoCore {
Scriptable global; Scriptable global;
// caching table for JavaScript object wrappers // caching table for JavaScript object wrappers
CacheMap wrappercache; WeakHashMap wrappercache;
// table containing JavaScript prototypes // table containing JavaScript prototypes
Hashtable prototypes; Hashtable prototypes;
@ -54,7 +56,8 @@ public final class RhinoCore {
*/ */
public RhinoCore(Application app) { public RhinoCore(Application app) {
this.app = app; this.app = app;
wrappercache = new CacheMap(500, .75f); // wrappercache = new CacheMap(500, .75f);
wrappercache = new WeakHashMap();
prototypes = new Hashtable(); prototypes = new Hashtable();
Context context = Context.enter(); Context context = Context.enter();
@ -510,7 +513,11 @@ public final class RhinoCore {
* interface, the getPrototype method will be used to retrieve the name of the prototype * interface, the getPrototype method will be used to retrieve the name of the prototype
* to use. Otherwise, a Java-Class-to-Script-Prototype mapping is consulted. * to use. Otherwise, a Java-Class-to-Script-Prototype mapping is consulted.
*/ */
public Scriptable getElementWrapper(Object e) { protected Scriptable getElementWrapper(Object e) {
Scriptable w = (Scriptable) wrappercache.get(e);
if (e == null) {
// Gotta find out the prototype name to use for this object... // Gotta find out the prototype name to use for this object...
String prototypeName = app.getPrototypeName(e); String prototypeName = app.getPrototypeName(e);
@ -520,14 +527,19 @@ public final class RhinoCore {
op = getPrototype("hopobject"); op = getPrototype("hopobject");
} }
return new JavaObject(global, e, op, this); w = new JavaObject(global, e, op, this);
wrappercache.put(e, w);
}
return w;
} }
/** /**
* Get a script wrapper for an instance of helma.objectmodel.INode * Get a script wrapper for an instance of helma.objectmodel.INode
*/ */
public Scriptable getNodeWrapper(INode n) { protected Scriptable getNodeWrapper(INode n) {
// FIXME: should this return ESNull.theNull? // FIXME: is this needed? should this return ESNull.theNull?
if (n == null) { if (n == null) {
return null; return null;
} }
@ -577,9 +589,9 @@ public final class RhinoCore {
* Register a new Node wrapper with the wrapper cache. This is used by the * Register a new Node wrapper with the wrapper cache. This is used by the
* Node constructor. * Node constructor.
*/ */
public void putNodeWrapper(INode n, Scriptable esn) { /* public void putNodeWrapper(INode n, Scriptable esn) {
wrappercache.put(n, esn); wrappercache.put(n, esn);
} } */
private synchronized void evaluate(Prototype prototype, Object code) { private synchronized void evaluate(Prototype prototype, Object code) {
if (code instanceof FunctionFile) { if (code instanceof FunctionFile) {
@ -664,10 +676,14 @@ public final class RhinoCore {
return getElementWrapper(obj); return getElementWrapper(obj);
} }
if (obj instanceof Map) { if (obj instanceof SystemMap) {
return new MapWrapper((Map) obj, RhinoCore.this); return new MapWrapper((Map) obj, RhinoCore.this);
} }
if (obj instanceof String) {
return obj;
}
return super.wrap(cx, scope, obj, staticType); return super.wrap(cx, scope, obj, staticType);
} }
@ -678,7 +694,6 @@ public final class RhinoCore {
public Scriptable wrapNewObject(Context cx, Scriptable scope, Object obj) { public Scriptable wrapNewObject(Context cx, Scriptable scope, Object obj) {
// System.err.println ("N-Wrapping: "+obj); // System.err.println ("N-Wrapping: "+obj);
if (obj instanceof INode) { if (obj instanceof INode) {
return getNodeWrapper((INode) obj); return getNodeWrapper((INode) obj);
} }
@ -687,10 +702,6 @@ public final class RhinoCore {
return getElementWrapper(obj); return getElementWrapper(obj);
} }
if (obj instanceof Map) {
return new MapWrapper((Map) obj, RhinoCore.this);
}
return super.wrapNewObject(cx, scope, obj); return super.wrapNewObject(cx, scope, obj);
} }
} }