diff --git a/js/rhino/org/mozilla/javascript/NativeJavaArray.java b/js/rhino/org/mozilla/javascript/NativeJavaArray.java index 099842e527be..8e6f3d1c8ded 100644 --- a/js/rhino/org/mozilla/javascript/NativeJavaArray.java +++ b/js/rhino/org/mozilla/javascript/NativeJavaArray.java @@ -52,21 +52,6 @@ public class NativeJavaArray extends NativeJavaObject { this.array = array; this.length = Array.getLength(array); this.cls = cl.getComponentType(); - setPrototype(ScriptableObject.getClassPrototype(scope, "Array")); - } - - /** - * Returns the prototype of the object. - */ - public Scriptable getPrototype() { - return prototype; - } - - /** - * Sets the prototype of the object. - */ - public void setPrototype(Scriptable m) { - prototype = m; } public boolean has(String id, Scriptable start) { @@ -130,6 +115,15 @@ public class NativeJavaArray extends NativeJavaObject { return cls.isInstance(instance); } + public Scriptable getPrototype() { + if (prototype == null) { + prototype = + ScriptableObject.getClassPrototype(this.getParentScope(), + "Array"); + } + return prototype; + } + Object array; int length; Class cls; diff --git a/js/rhino/org/mozilla/javascript/NativeJavaClass.java b/js/rhino/org/mozilla/javascript/NativeJavaClass.java index fccbcd5bdb0d..7e4506c84ad3 100644 --- a/js/rhino/org/mozilla/javascript/NativeJavaClass.java +++ b/js/rhino/org/mozilla/javascript/NativeJavaClass.java @@ -40,7 +40,7 @@ import java.util.Hashtable; public class NativeJavaClass extends NativeJavaObject implements Function { public NativeJavaClass(Scriptable scope, Class cl) { - super(cl, JavaMembers.lookupClass(scope, cl, cl)); + super(scope, cl, JavaMembers.lookupClass(scope, cl, cl)); fieldAndMethods = members.getFieldAndMethodsObjects(javaObject, false); } @@ -236,21 +236,6 @@ public class NativeJavaClass extends NativeJavaObject implements Function { return false; } - public Scriptable getParentScope() { - return parent; - } - - public void setParentScope(Scriptable scope) { - // beard: - // need at least the top-most scope, so JavaMembers.reflectMethod() - // will work. this fixes a bug where a static field of a class would get - // reflected by JavaMembers.reflect() in the scope of a NativeJavaClass, - // and calls to ScriptableObject.getFunctionPrototype() would return - // null because there was no top-level scope with "Function" defined. - // question: should the package hierarchy form the scope chain or is top-level sufficient? - parent = scope; - } - private Hashtable fieldAndMethods; // beard: need a scope for finding top-level prototypes. diff --git a/js/rhino/org/mozilla/javascript/NativeJavaMethod.java b/js/rhino/org/mozilla/javascript/NativeJavaMethod.java index 3f84338bd198..58148017ccdc 100644 --- a/js/rhino/org/mozilla/javascript/NativeJavaMethod.java +++ b/js/rhino/org/mozilla/javascript/NativeJavaMethod.java @@ -202,6 +202,12 @@ public class NativeJavaMethod extends NativeFunction implements Function { } } + /* + public Object getDefaultValue(Class hint) { + return this; + } + */ + /** * Find the correct function to call given the set of methods * or constructors and the arguments. @@ -232,7 +238,7 @@ public class NativeJavaMethod extends NativeFunction implements Function { Member bestFit = null; Class[] bestFitTypes = null; - java.util.Vector ambiguousMethods = null; + java.util.Vector ambiguousMethods = new java.util.Vector(); for (int i = 0; i < methodsOrCtors.length; i++) { Member member = methodsOrCtors[i]; @@ -264,8 +270,6 @@ public class NativeJavaMethod extends NativeFunction implements Function { if (preference == PREFERENCE_AMBIGUOUS) { if (debug) printDebug("Deferring ", member, args); // add to "ambiguity list" - if (ambiguousMethods == null) - ambiguousMethods = new java.util.Vector(); ambiguousMethods.addElement(member); } else if (preference == PREFERENCE_FIRST_ARG) { @@ -279,12 +283,9 @@ public class NativeJavaMethod extends NativeFunction implements Function { } } - if (ambiguousMethods == null) - return bestFit; - // Compare ambiguous methods with best fit, in case // the current best fit removes the ambiguities. - for (int i = ambiguousMethods.size() - 1; i >= 0; i--) { + for (int i = ambiguousMethods.size() - 1; i >= 0 ; i--) { Member member = (Member)ambiguousMethods.elementAt(i); Class paramTypes[] = hasMethods ? ((Method) member).getParameterTypes() diff --git a/js/rhino/org/mozilla/javascript/NativeJavaObject.java b/js/rhino/org/mozilla/javascript/NativeJavaObject.java index 83da57690de7..affba507de03 100644 --- a/js/rhino/org/mozilla/javascript/NativeJavaObject.java +++ b/js/rhino/org/mozilla/javascript/NativeJavaObject.java @@ -34,8 +34,11 @@ import java.util.Enumeration; */ public class NativeJavaObject implements Scriptable, Wrapper { - - public NativeJavaObject(Object javaObject, JavaMembers members) { + + public NativeJavaObject(Scriptable scope, Object javaObject, + JavaMembers members) + { + this.parent = scope; this.javaObject = javaObject; this.members = members; } @@ -43,6 +46,7 @@ public class NativeJavaObject implements Scriptable, Wrapper { public NativeJavaObject(Scriptable scope, Object javaObject, Class staticType) { + this.parent = scope; this.javaObject = javaObject; Class dynamicType = javaObject != null ? javaObject.getClass() : staticType; @@ -93,17 +97,27 @@ public class NativeJavaObject implements Scriptable, Wrapper { } public Scriptable getPrototype() { + if (javaObject.getClass() == ScriptRuntime.StringClass) { + return ScriptableObject.getClassPrototype(parent, "String"); + } return null; } public void setPrototype(Scriptable prototype) { } + /** + * Returns the parent (enclosing) scope of the object. + */ public Scriptable getParentScope() { - return null; + return parent; } - public void setParentScope(Scriptable parent) { + /** + * Sets the parent (enclosing) scope of the object. + */ + public void setParentScope(Scriptable m) { + parent = m; } public Object[] getIds() { @@ -550,12 +564,16 @@ public class NativeJavaObject implements Scriptable, Wrapper { } } + /** + * The parent scope of this object. + */ + protected Scriptable parent; + protected Object javaObject; protected JavaMembers members; private Hashtable fieldAndMethods; static Class jsObjectClass; static Constructor jsObjectCtor; static Method jsObjectGetScriptable; - } diff --git a/js/rhino/src/org/mozilla/javascript/NativeJavaArray.java b/js/rhino/src/org/mozilla/javascript/NativeJavaArray.java index 099842e527be..8e6f3d1c8ded 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeJavaArray.java +++ b/js/rhino/src/org/mozilla/javascript/NativeJavaArray.java @@ -52,21 +52,6 @@ public class NativeJavaArray extends NativeJavaObject { this.array = array; this.length = Array.getLength(array); this.cls = cl.getComponentType(); - setPrototype(ScriptableObject.getClassPrototype(scope, "Array")); - } - - /** - * Returns the prototype of the object. - */ - public Scriptable getPrototype() { - return prototype; - } - - /** - * Sets the prototype of the object. - */ - public void setPrototype(Scriptable m) { - prototype = m; } public boolean has(String id, Scriptable start) { @@ -130,6 +115,15 @@ public class NativeJavaArray extends NativeJavaObject { return cls.isInstance(instance); } + public Scriptable getPrototype() { + if (prototype == null) { + prototype = + ScriptableObject.getClassPrototype(this.getParentScope(), + "Array"); + } + return prototype; + } + Object array; int length; Class cls; diff --git a/js/rhino/src/org/mozilla/javascript/NativeJavaClass.java b/js/rhino/src/org/mozilla/javascript/NativeJavaClass.java index fccbcd5bdb0d..7e4506c84ad3 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeJavaClass.java +++ b/js/rhino/src/org/mozilla/javascript/NativeJavaClass.java @@ -40,7 +40,7 @@ import java.util.Hashtable; public class NativeJavaClass extends NativeJavaObject implements Function { public NativeJavaClass(Scriptable scope, Class cl) { - super(cl, JavaMembers.lookupClass(scope, cl, cl)); + super(scope, cl, JavaMembers.lookupClass(scope, cl, cl)); fieldAndMethods = members.getFieldAndMethodsObjects(javaObject, false); } @@ -236,21 +236,6 @@ public class NativeJavaClass extends NativeJavaObject implements Function { return false; } - public Scriptable getParentScope() { - return parent; - } - - public void setParentScope(Scriptable scope) { - // beard: - // need at least the top-most scope, so JavaMembers.reflectMethod() - // will work. this fixes a bug where a static field of a class would get - // reflected by JavaMembers.reflect() in the scope of a NativeJavaClass, - // and calls to ScriptableObject.getFunctionPrototype() would return - // null because there was no top-level scope with "Function" defined. - // question: should the package hierarchy form the scope chain or is top-level sufficient? - parent = scope; - } - private Hashtable fieldAndMethods; // beard: need a scope for finding top-level prototypes. diff --git a/js/rhino/src/org/mozilla/javascript/NativeJavaMethod.java b/js/rhino/src/org/mozilla/javascript/NativeJavaMethod.java index 3f84338bd198..58148017ccdc 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeJavaMethod.java +++ b/js/rhino/src/org/mozilla/javascript/NativeJavaMethod.java @@ -202,6 +202,12 @@ public class NativeJavaMethod extends NativeFunction implements Function { } } + /* + public Object getDefaultValue(Class hint) { + return this; + } + */ + /** * Find the correct function to call given the set of methods * or constructors and the arguments. @@ -232,7 +238,7 @@ public class NativeJavaMethod extends NativeFunction implements Function { Member bestFit = null; Class[] bestFitTypes = null; - java.util.Vector ambiguousMethods = null; + java.util.Vector ambiguousMethods = new java.util.Vector(); for (int i = 0; i < methodsOrCtors.length; i++) { Member member = methodsOrCtors[i]; @@ -264,8 +270,6 @@ public class NativeJavaMethod extends NativeFunction implements Function { if (preference == PREFERENCE_AMBIGUOUS) { if (debug) printDebug("Deferring ", member, args); // add to "ambiguity list" - if (ambiguousMethods == null) - ambiguousMethods = new java.util.Vector(); ambiguousMethods.addElement(member); } else if (preference == PREFERENCE_FIRST_ARG) { @@ -279,12 +283,9 @@ public class NativeJavaMethod extends NativeFunction implements Function { } } - if (ambiguousMethods == null) - return bestFit; - // Compare ambiguous methods with best fit, in case // the current best fit removes the ambiguities. - for (int i = ambiguousMethods.size() - 1; i >= 0; i--) { + for (int i = ambiguousMethods.size() - 1; i >= 0 ; i--) { Member member = (Member)ambiguousMethods.elementAt(i); Class paramTypes[] = hasMethods ? ((Method) member).getParameterTypes() diff --git a/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java b/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java index 83da57690de7..affba507de03 100644 --- a/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java +++ b/js/rhino/src/org/mozilla/javascript/NativeJavaObject.java @@ -34,8 +34,11 @@ import java.util.Enumeration; */ public class NativeJavaObject implements Scriptable, Wrapper { - - public NativeJavaObject(Object javaObject, JavaMembers members) { + + public NativeJavaObject(Scriptable scope, Object javaObject, + JavaMembers members) + { + this.parent = scope; this.javaObject = javaObject; this.members = members; } @@ -43,6 +46,7 @@ public class NativeJavaObject implements Scriptable, Wrapper { public NativeJavaObject(Scriptable scope, Object javaObject, Class staticType) { + this.parent = scope; this.javaObject = javaObject; Class dynamicType = javaObject != null ? javaObject.getClass() : staticType; @@ -93,17 +97,27 @@ public class NativeJavaObject implements Scriptable, Wrapper { } public Scriptable getPrototype() { + if (javaObject.getClass() == ScriptRuntime.StringClass) { + return ScriptableObject.getClassPrototype(parent, "String"); + } return null; } public void setPrototype(Scriptable prototype) { } + /** + * Returns the parent (enclosing) scope of the object. + */ public Scriptable getParentScope() { - return null; + return parent; } - public void setParentScope(Scriptable parent) { + /** + * Sets the parent (enclosing) scope of the object. + */ + public void setParentScope(Scriptable m) { + parent = m; } public Object[] getIds() { @@ -550,12 +564,16 @@ public class NativeJavaObject implements Scriptable, Wrapper { } } + /** + * The parent scope of this object. + */ + protected Scriptable parent; + protected Object javaObject; protected JavaMembers members; private Hashtable fieldAndMethods; static Class jsObjectClass; static Constructor jsObjectCtor; static Method jsObjectGetScriptable; - }