-
Notifications
You must be signed in to change notification settings - Fork 850
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
rPraml
wants to merge
7
commits into
mozilla:master
Choose a base branch
from
FOCONIS:fix-use-correct-thisObj-interfaceadapter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
893a8f0
When InterfaceAdapter is used, the wrong thisObj is used
rPraml fedcf81
Merge branch 'master-upstream' into fix-use-correct-thisObj-interface…
rPraml 1c76527
Refactored test
rPraml 4546065
Merge branch 'master-upstream' into fix-use-correct-thisObj-interface…
rPraml 7b9cade
add more realistic test with java-lang standards
rPraml e2699bd
remove wrong comment
rPraml 70bba4d
Spotless
rPraml File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
testsrc/org/mozilla/javascript/tests/JavaAdapterInvokeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package org.mozilla.javascript.tests; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mozilla.javascript.Context; | ||
import org.mozilla.javascript.Scriptable; | ||
|
||
public class JavaAdapterInvokeTest { | ||
Context cx = null; | ||
Scriptable topScope = null; | ||
|
||
@Before | ||
public void enterContext() { | ||
cx = Context.enter(); | ||
cx.setOptimizationLevel(-1); | ||
rPraml marked this conversation as resolved.
Show resolved
Hide resolved
|
||
topScope = cx.initStandardObjects(); | ||
} | ||
|
||
@After | ||
public void exitContext() { | ||
Context.exit(); | ||
} | ||
|
||
public interface AdapterInterface { | ||
int m1(int i); | ||
|
||
int m2(); | ||
} | ||
|
||
public static class AdapterClass { | ||
private AdapterInterface adapter; | ||
|
||
public AdapterClass(AdapterInterface adapter) { | ||
this.adapter = adapter; | ||
} | ||
|
||
public int doIt(int i) { | ||
return this.adapter.m1(i) + this.adapter.m2(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testInvoke() throws NoSuchMethodException { | ||
String testCode = | ||
"'use strict'\n" | ||
+ "var impl = {" | ||
+ " m1: function(i) { return i + 1 },\n" | ||
+ " m2: function() { return 7 }\n" | ||
+ "}\n" | ||
+ "adapter = new Packages." | ||
+ AdapterClass.class.getName() | ||
+ "(impl)\n" | ||
+ "adapter.doIt(42)"; | ||
|
||
Number result = (Number) cx.evaluateString(topScope, testCode, "", 1, null); | ||
Assert.assertEquals(50, result.intValue()); | ||
} | ||
|
||
@Test | ||
public void testInvokeWithPrototype() throws NoSuchMethodException { | ||
String testCode = | ||
"'use strict'\n" | ||
+ "function Obj() {}\n" | ||
+ "Obj.prototype.m1 = function(i) { return i + 1 }\n" | ||
+ "Obj.prototype.m2 = function() { return 7 }\n" | ||
+ "var impl = new Obj()\n" | ||
+ "adapter = new Packages." | ||
+ AdapterClass.class.getName() | ||
+ "(impl)\n" | ||
+ "adapter.doIt(42)"; | ||
|
||
Number result = (Number) cx.evaluateString(topScope, testCode, "", 1, null); | ||
Assert.assertEquals(50, result.intValue()); | ||
} | ||
|
||
@Test | ||
public void testInvokeWithPrototypeAndCtor() throws NoSuchMethodException { | ||
String testCode = | ||
"'use strict'\n" | ||
+ "function Obj() { this.myObj = {one: 1} }\n" | ||
+ "Obj.prototype.m1 = function(i) { return i + this.myObj.one }\n" | ||
+ "Obj.prototype.m2 = function() { return 7 }\n" | ||
+ "var impl = new Obj()\n" | ||
+ "adapter = new Packages." | ||
+ AdapterClass.class.getName() | ||
+ "(impl)\n" | ||
+ "adapter.doIt(42)"; | ||
|
||
Number result = (Number) cx.evaluateString(topScope, testCode, "", 1, null); | ||
Assert.assertEquals(50, result.intValue()); | ||
} | ||
|
||
@Test | ||
public void testInvokeJsOnly() throws NoSuchMethodException { | ||
String testCode = | ||
"'use strict'\n" | ||
+ "function Obj() { this.myObj = {one: 1} }\n" | ||
rPraml marked this conversation as resolved.
Show resolved
Hide resolved
|
||
+ "Obj.prototype.m1 = function(i) { return i + this.myObj.one }\n" | ||
+ "Obj.prototype.m2 = function() { return 7 }\n" | ||
+ "function Adapter(adapter) { this.adapter = adapter }\n" | ||
+ "Adapter.prototype.doIt = function(i) { return this.adapter.m1(i) + this.adapter.m2() }\n" | ||
+ "var impl = new Obj()\n" | ||
+ "adapter = new Adapter(impl)\n" | ||
+ "adapter.doIt(42)"; | ||
|
||
Number result = (Number) cx.evaluateString(topScope, testCode, "", 1, null); | ||
Assert.assertEquals(50, result.intValue()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thisObject
is an instance ofJavaAdapterInvokeTest.AdapterClass
and we want to call a method ontarget