Closed Bug 306607 Opened 19 years ago Closed 19 years ago

An EvaluatorException is thrown while trying to add a property to a wrapped Java object.

Categories

(Rhino Graveyard :: Core, defect)

x86
Windows 2000
defect
Not set
major

Tracking

(Not tracked)

RESOLVED INVALID

People

(Reporter: Uncle_Tentoes, Assigned: igor)

Details

User-Agent:       Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)
Build Identifier: 

If you attempt to compile and execute the following statement (where 'window' 
is a wrapped Java object), an EvaluatorException is thrown:

window["func"]=function(){ any_code };

In the above, the code comprising the body of the function is irrelevant - 
i.e. 'any_code' can be anything at all (or nothing for that matter).

The message associated with the exception is the following: Java 
class "BugWindow" has no public instance field or method named "func"





Reproducible: Always

Steps to Reproduce:
1. Compile and run the two classes, the sources of which are listed below 

// First class (containing main())
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

/**
 * This class demonstrates that Rhino does not allow a function to be added
 * to a wrapped Java object.
 *
 * @author Walter.Regan
 * @version 1
 */
public class Bugtest {

    static Context cx ;
    static Scriptable scope;

    public void init() {
        cx = Context.enter();
        scope = cx.initStandardObjects();
        // wrap an object of the class BugWindow
        Object wrappedWindow = Context.javaToJS(new BugWindow(), scope);
        ScriptableObject.putProperty(scope, "window", wrappedWindow);

        // define a function to print a message via the wrapped object
        cx.evaluateString(scope, "function alert(str) {window.msg(str)}",
                            "<initialization>", 1, null);

        // output a message to indicate the all is well so far */
        cx.evaluateString(scope, "alert(\"Initialization complete\")",
                            "<testing>", 1, null);

/* ******* BUG: The following statement throws EvaluatorException because "func"
                does not exist. It should not fail because the purpose of
                the statement is to define the non-existent function "func" */
        cx.evaluateString(scope,"window[\"func\"]=function(){alert(\"func\")};",
                          "<defining func>", 1, null);

        // intended to execute the function that was added above, but it never
        // gets executed because the previous line throws and exception.
        cx.evaluateString(scope, "window.func();",
                          "<executing func>", 1, null);
    }

    public static void main(String[] args) {
        Bugtest bugtest = new Bugtest();
        bugtest.init();
    }
}

// Second class
/**
 * This is the class for the java object to be wrapped.
 *
 * @author Walter.Regan
 * @version 1
 */
public class BugWindow extends Object {
    public void msg(String s) {
        System.out.println(s);
    }
}

Actual Results:  
The following output is produced when you compile and run the sources listed in 
this bug report:

Initialization complete
org.mozilla.javascript.EvaluatorException: Java class "BugWindow" has no public 
instance field or method named "func". (<defining func>#1)
	at org.mozilla.javascript.DefaultErrorReporter.runtimeError
(DefaultErrorReporter.java:95)
	at org.mozilla.javascript.Context.reportRuntimeError(Context.java:1054)
	at org.mozilla.javascript.Context.reportRuntimeError(Context.java:1110)
	at org.mozilla.javascript.Context.reportRuntimeError2(Context.java:1080)
	at org.mozilla.javascript.JavaMembers.reportMemberNotFound
(JavaMembers.java:624)
	at org.mozilla.javascript.JavaMembers.put(JavaMembers.java:123)
	at org.mozilla.javascript.NativeJavaObject.put
(NativeJavaObject.java:113)
	at org.mozilla.javascript.ScriptableObject.putProperty
(ScriptableObject.java:1347)
	at org.mozilla.javascript.ScriptRuntime.setObjectElem
(ScriptRuntime.java:1404)
	at org.mozilla.javascript.ScriptRuntime.setObjectElem
(ScriptRuntime.java:1387)
	at org.mozilla.javascript.gen.c3._c0(<defining func>:1)
	at org.mozilla.javascript.gen.c3.call(<defining func>)
	at org.mozilla.javascript.ContextFactory.doTopCall
(ContextFactory.java:304)
	at org.mozilla.javascript.ScriptRuntime.doTopCall
(ScriptRuntime.java:2769)
	at org.mozilla.javascript.gen.c3.call(<defining func>)
	at org.mozilla.javascript.gen.c3.exec(<defining func>)
	at org.mozilla.javascript.Context.evaluateString(Context.java:1220)
	at Bugtest.init(Bugtest.java:35)
	at Bugtest.main(Bugtest.java:46)
Exception in thread "main" 

Expected Results:  
Initialization complete
func

i.e., there should be no exception. (Note: if the window object is defined via 
JavaScript rather than via wrapping a Java object, window["func"]=function(){ 
any_code } works without and exception.

I am currently using rhino1_6R1 but I have seen the same behaviour with 
rhino1_6R2pre
This is not a bug, but a well-defined feature.

In Rhino JavaScript code can ONLY access the existing methods and fields of Java
objects. 
Status: NEW → RESOLVED
Closed: 19 years ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.