Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When InterfaceAdapter is used, the wrong thisObj is used #1453

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

rPraml
Copy link
Contributor

@rPraml rPraml commented Feb 28, 2024

I had an issue when I pass a JS object to Java class and an InterfaceAdapter is constructed:

Java interface

public interface AdapterInterface {
        int m1(int i);
        int m2();
}

Corresponding impl. in javascript:

function Obj() { this.myObj = {one: 1} }
Obj.prototype.m1 = function(i) { return i + this.myObj.one } // this.myObj is undefined
Obj.prototype.m2 = function() { return 7 }

When I pass this object in my AdapterClass, I get the error: this.myObj is undefined
This PR provides a test and a possible fix

@@ -152,9 +152,8 @@ Object invokeImpl(
}
}
}
Scriptable thisObj = wf.wrapAsJavaObject(cx, topScope, thisObject, null);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thisObject is an instance of JavaAdapterInvokeTest.AdapterClass and we want to call a method on target

@gbrail
Copy link
Collaborator

gbrail commented Apr 27, 2024

This seems logical to me but it's a pretty fundamental thing to the Java embedding support and I'd like to see someone who works with this stuff regularly take a look before merging it.

@p-bakker
Copy link
Collaborator

Not a regular user of code that uses code constructs like the testcases, calling a java method passing as argument a JavaScript objects that implements a java interface, but the change looks ok to me

@p-bakker p-bakker added the Java Interop Issues related to the interaction between Java and JavaScript label Jul 21, 2024
@gbrail
Copy link
Collaborator

gbrail commented Aug 18, 2024

I'd love to see a few of the questions answered on the PR above and then see if it makes sense to merge this.

+ "function Obj() { this.myObj = {one: 1} }\n"
+ "Obj.prototype.seven = 7\n"
+ "Obj.prototype.m1 = function(i) { return i + this.myObj.one }\n"
+ "Obj.prototype.m2 = function() { return this.seven }\n"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test shows the problem. this.myObj and this.seven were undefined, when the js object is passed in an InterfaceAdapter
The other tests are all passing without the fix and are for completeness, not to break anything else

@rPraml
Copy link
Contributor Author

rPraml commented Aug 22, 2024

@gbrail I resolved the merge conflicts and updated the test with some documentation

@rPraml
Copy link
Contributor Author

rPraml commented Aug 22, 2024

Hmm. Seems that there might be a flaky test

ozillaSuiteTest > [3846, js=testsrc/tests/js1_5/Regress/regress-211590.js, opt=0] > runMozillaTest[3846, js=testsrc/tests/js1_5/Regress/regress-211590.js, opt=0] STANDARD_OUT
    In "testsrc/tests/js1_5/Regress/regress-211590.js":
     FAILED! Math.random should be random : Expected value 'between 48% and 52%', Actual value ' is 47.990' 
     FAILED! Math.random should be random : Expected value 'between 48% and 52%', Actual value ' is 47.990'

Will investigate later

@p-bakker p-bakker marked this pull request as draft September 8, 2024 06:52
@p-bakker
Copy link
Collaborator

p-bakker commented Sep 8, 2024

Converted this to a draft as it currently requires more work as the pipeline fails

Please remove the draft status once the pipeline passed and the PR can be considered for merging,.

This way it allows the committers to focus on ready-to-merge PRs

@rPraml
Copy link
Contributor Author

rPraml commented Sep 24, 2024

Not a regular user of code that uses code constructs like the testcases, calling a java method passing as argument a JavaScript objects that implements a java interface, but the change looks ok to me

I've added a more realistic testcase where i use var runnable = new MyRunnable() and pass this object as constuctor argument for java.lang.Thread:
https://github.com/mozilla/rhino/pull/1453/files#diff-05cf7412910fd42af1934af105265073d54b9fd36c8d433d53b3e51a4fae8650R106
(without the fix, the test fails)

@rPraml rPraml marked this pull request as ready for review September 24, 2024 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Java Interop Issues related to the interaction between Java and JavaScript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants