Closed Bug 13417 Opened 25 years ago Closed 25 years ago

Rhino: use of 'this' in methods called from JavaAdapter

Categories

(Core :: JavaScript Engine, defect, P3)

All
Windows NT
defect

Tracking

()

RESOLVED FIXED

People

(Reporter: norrisboyd, Assigned: norrisboyd)

Details

Subject:
        Rhino and overloaded drawImage
   Date:
        Wed, 08 Sep 1999 15:06:31 -0400
   From:
        Andrew Wason <aw@softcom.com>
     To:
        norris@netscape.com
    CC:
        Howard Lin <howard@softcom.com>




I'm getting an EvaluatorException for this:
g.drawImage(bgImg,0,0,this);

The message is:

org.mozilla.javascript.EvaluatorException: The choice of Java method
sun.awt.windows.WGraphics.drawImage matching JavaScript argument types
(sun.awt.windows.WImage,number,number,object) is ambiguous; candidate
methods are: void
drawImage(java.awt.image.BufferedImage,java.awt.image.BufferedImageOp,int,in
t), boolean drawImage(java.awt.Image,int,int,java.awt.image.ImageObserver)

See the attached output and JS file.

Andrew
--
Andrew Wason
SoftCom, Inc.
aw@softcom.com



   bug3.js

          Name:
                bug3.js
          Type:
                Plain Text (text/plain)




   bug3.txt

          Name:
                bug3.txt
           Type:
                Plain Text (text/plain)


bug3.js:
importPackage(Packages.javax.swing);
importPackage(java.awt);
importPackage(java.net);
importPackage(java.lang);

var bgImg = Toolkit.getDefaultToolkit().getImage(new
        URL("http://www.mozilla.org/rhino/rhino50.jpg"));

var foo = new JComponent() {
        paint: function(g) {
                g.drawImage(bgImg,0,0,this);
                super.paint(g);
        }
};

var f = new Frame();
f.add(foo, BorderLayout.CENTER);
f.setSize(300,300);
f.setVisible(true);

bug3.txt:
org.mozilla.javascript.EvaluatorException: The choice of Java method sun.awt.win
        at org.mozilla.javascript.DefaultErrorReporter.runtimeError(DefaultError
        at org.mozilla.javascript.Context.reportRuntimeError(Context.java:484)
        at org.mozilla.javascript.Context.reportRuntimeError(Context.java:500)
        at org.mozilla.javascript.NativeJavaMethod.findFunction(NativeJavaMethod
        at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java, C
        at org.mozilla.javascript.ScriptRuntime.call(ScriptRuntime.java:1256)
        at org.mozilla.javascript.Interpreter.interpret(Interpreter.java, Compil
        at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.j
        at org.mozilla.javascript.NativeClosure.call(NativeClosure.java:56)
        at org.mozilla.javascript.ScriptRuntime.call(ScriptRuntime.java:1256)
        at org.mozilla.javascript.FlattenedObject.call(FlattenedObject.java:234)
        at org.mozilla.javascript.FlattenedObject.callMethod(FlattenedObject.jav
        at org.mozilla.javascript.JavaAdapter.callMethod(JavaAdapter.java:156)
        at adapter0.paint(<adapter>)
        at java.awt.Container.paint(Container.java:773)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:117)
        at java.awt.Component.dispatchEventImpl(Component.java:2447)
        at java.awt.Container.dispatchEventImpl(Container.java:1035)
        at java.awt.Window.dispatchEventImpl(Window.java:749)
        at java.awt.Component.dispatchEvent(Component.java:2307)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:287)
        at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java:10
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:92)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:83)
Exception occurred during event dispatching:
java.lang.Error: The choice of Java method sun.awt.windows.WGraphics.drawImage m
        at org.mozilla.javascript.JavaAdapter.callMethod(JavaAdapter.java:162)
        at adapter0.paint(<adapter>)
        at java.awt.Container.paint(Container.java:773)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:117)
        at java.awt.Component.dispatchEventImpl(Component.java:2447)
        at java.awt.Container.dispatchEventImpl(Container.java:1035)
        at java.awt.Window.dispatchEventImpl(Window.java:749)
        at java.awt.Component.dispatchEvent(Component.java:2307)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:287)
        at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java:10
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:92)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:83)
Assignee: norris → rogerl
Status: NEW → ASSIGNED
Assignee: rogerl → norris
Status: ASSIGNED → NEW
This is that bug I was trying to forward to you just before some nice guy yanked
my network connection.
The problem is that the user is trying to use 'this' to satisfy the
ImageObserver parameter in the drawImage call - but the 'this' is the JS object
not the Java object he wishes it were (same applies to his use of 'super' in the
statement following).
*** Bug 13415 has been marked as a duplicate of this bug. ***
Status: NEW → ASSIGNED
Summary: Rhino: overloaded drawImage → Rhino: use of 'this' in methods called from JavaAdapter
Now get a verify error:

js: "bug3.js", line 9: error instantiating (java.lang.VerifyError: (class: adapt
er0, method: super$paint signature: (Ljava/awt/Graphics;)V) Falling off the end
of the code): class javax.swing.JComponent is interface or abstract


Subject:
        JavaAdater.generateSuper() is in
   Date:
        Fri, 24 Sep 1999 11:22:29 -0700
   From:
        beard@netscape.com (Patrick Beard)
     To:
        Norris Boyd <norris@netscape.com>




Norris -

As discussed yesterday, I've implemented the facility for calling
"super.method()". The method name is currently called "super$method".
Since Java implements some thunk methods with '$' in identifiers,
this seems reasonable, because it won't conflict with programmer
generated names. So, here's an example I've written to test it:

/*
   super.js

   Demonstrates calling overridden methods in a JavaAdapter class.

   by Patrick C. Beard.
  */

var runnable = new java.lang.Runnable() {
        hashCode: function() {
                return this.super$hashCode();
        },

        equals: function(obj) {
                return this.super$equals(obj);
        },

        run: function() {
                print('run here.');
        }
};

- Patrick

// Patrick C. Beard
// Java Runtime Enthusiast -- "Will invoke interfaces for food."
// mailto:beard@netscape.com
Status: ASSIGNED → RESOLVED
Closed: 25 years ago
Resolution: --- → FIXED
Patrick and I fixed this. See his post in the netscape.public.mozilla.jseng
newsgroup:

Subject:
             [Rhino] Speaking of JS Components...
        Date:
             Fri, 24 Sep 1999 19:14:58 -0700
       From:
             Patrick Beard <beard@netscape.com>
 Organization:
             Netscape Communications
 Newsgroups:
             netscape.public.mozilla.jseng




We now have the ability to do pretty much first-class subclassing of
Java classes from Rhino. Whereas before you could specify a set of Java
interfaces to implement using a JavaScript object, now you can sub-class
real-live Java classes, and call superclass methods from within methods
overridden by JavaScript. Here's a trivial example:

importPackage(java.util);

var enum = new Enumeration() {
   // override Object.hashCode:

   hashCode: function() {
      return this.super$hashCode() + 1;
   },

   // implement the java.util.Enumeration interface:

   hasMoreElements: function() {
      return this.equals(this);
   },

   n: 0,

   nextElement: function {
      return this.n++;
   }
}

For now, you must access the overridden method using "super$method" but
I hope to eventually support "super.method" to get at superclass
methods. Obviously, that should be the only way, otherwise your script
can do things that even Java programs aren't allowed to do.

Another recent change is that the prototype chain of the JavaScript
object includes the LiveConnect wrapper of the adapter object. So,
inside the JavaScript object, you can access superclass methods that
aren't overridden using "this.method", demonstrated in the
hasMoreElements property above, which is calling java.lang.Object.equals.

- Patrick

--
// Patrick C. Beard
// Netscape Communications Corporation
You need to log in before you can comment on or make changes to this bug.