forked from mozilla/rhino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed a bug with interpreter peeling introduced in mozilla#1510
- Loading branch information
1 parent
cbb0e2f
commit 0c0274c
Showing
2 changed files
with
88 additions
and
0 deletions.
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
86 changes: 86 additions & 0 deletions
86
...c/test/java/org/mozilla/javascript/tests/InterpreterFunctionPeelingNonRegressionTest.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,86 @@ | ||
package org.mozilla.javascript.tests; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mozilla.javascript.LambdaConstructor.CONSTRUCTOR_FUNCTION; | ||
|
||
import org.junit.Test; | ||
import org.mozilla.javascript.Context; | ||
import org.mozilla.javascript.LambdaConstructor; | ||
import org.mozilla.javascript.LambdaFunction; | ||
import org.mozilla.javascript.ScriptRuntime; | ||
import org.mozilla.javascript.Scriptable; | ||
import org.mozilla.javascript.ScriptableObject; | ||
|
||
public class InterpreterFunctionPeelingNonRegressionTest { | ||
@Test | ||
public void testLambdaFunction() { | ||
try (var cx = Context.enter()) { | ||
cx.setOptimizationLevel(-1); | ||
Scriptable scope = cx.initStandardObjects(); | ||
|
||
LambdaFunction makePerson = | ||
new LambdaFunction( | ||
scope, | ||
"makePerson", | ||
1, | ||
(cx1, scope1, thisObj, args) -> | ||
new Person(ScriptRuntime.toString(args[0]))); | ||
scope.put("makePerson", scope, makePerson); | ||
|
||
String source = | ||
"function testLambdaFunction() {\n" | ||
+ " return makePerson('Andrea');\n" | ||
+ "}\n" | ||
+ "testLambdaFunction().name"; | ||
Object value = cx.evaluateString(scope, source, "test", 1, null); | ||
assertEquals("Andrea", value); | ||
} | ||
} | ||
|
||
@Test | ||
public void testLambdaConstructor() { | ||
try (var cx = Context.enter()) { | ||
cx.setOptimizationLevel(-1); | ||
Scriptable scope = cx.initStandardObjects(); | ||
|
||
LambdaConstructor personCtor = | ||
new LambdaConstructor( | ||
scope, | ||
"Person", | ||
1, | ||
CONSTRUCTOR_FUNCTION, | ||
(cx1, scope1, args) -> new Person(ScriptRuntime.toString(args[0]))); | ||
scope.put("Person", scope, personCtor); | ||
|
||
String source = | ||
"function testLambdaConstructor() {\n" + | ||
" return Person('Andrea');\n" + | ||
"}\n" + | ||
"testLambdaConstructor().name"; | ||
Object value = cx.evaluateString(scope, source, "test", 1, null); | ||
assertEquals("Andrea", value); | ||
} | ||
} | ||
|
||
static class Person extends ScriptableObject { | ||
private final String name; | ||
|
||
public Person(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String getClassName() { | ||
return "Person"; | ||
} | ||
|
||
@Override | ||
public Object get(String name, Scriptable start) { | ||
if (name.equals("name")) { | ||
return this.name; | ||
} else { | ||
return super.get(name, start); | ||
} | ||
} | ||
} | ||
} |