Skip to content

Commit

Permalink
Fixed a bug with interpreter peeling introduced in mozilla#1510
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabergia committed Aug 22, 2024
1 parent cbb0e2f commit 0c0274c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
2 changes: 2 additions & 0 deletions rhino/src/main/java/org/mozilla/javascript/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,8 @@ private static Object interpretLoop(Context cx, CallFrame frame, Object throwabl
ArrowFunction afun = (ArrowFunction) fun;
fun = afun.getTargetFunction();
funThisObj = afun.getCallThis(cx);
} else if (fun instanceof LambdaConstructor) {
break;
} else if (fun instanceof LambdaFunction) {
fun = ((LambdaFunction) fun).getTarget();
} else if (fun instanceof BoundFunction) {
Expand Down
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);
}
}
}
}

0 comments on commit 0c0274c

Please sign in to comment.