Skip to content

Commit

Permalink
Allow passing Undefined.instance to ScriptRuntime.evalSpecial
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabergia authored and gbrail committed Oct 31, 2024
1 parent 838b6ca commit efa49df
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3044,7 +3044,11 @@ public static Object evalSpecial(
Script script = cx.compileString(x.toString(), evaluator, reporter, sourceName, 1, null);
evaluator.setEvalScriptFlag(script);
Callable c = (Callable) script;
return c.call(cx, scope, (Scriptable) thisArg, ScriptRuntime.emptyArgs);
Scriptable thisObject =
thisArg == Undefined.instance
? Undefined.SCRIPTABLE_UNDEFINED
: (Scriptable) thisArg;
return c.call(cx, scope, thisObject, ScriptRuntime.emptyArgs);
}

/** The typeof operator */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.mozilla.javascript;

import static org.junit.Assert.*;

import org.junit.Test;
import org.mozilla.javascript.tests.Utils;

public class ScriptRuntimeEvalSpecialTest {
@Test
public void worksWithAnObject() {
canUseEvalSpecialWithThisSetTo(new NativeObject());
}

@Test
public void worksWithNull() {
canUseEvalSpecialWithThisSetTo(null);
}

@Test
public void worksWithUndefined() {
canUseEvalSpecialWithThisSetTo(Undefined.instance);
}

private static void canUseEvalSpecialWithThisSetTo(Object thisArg) {
Utils.runWithAllOptimizationLevels(
cx -> {
ScriptableObject scope = cx.initStandardObjects();
Object o =
ScriptRuntime.evalSpecial(
cx, scope, thisArg, new Object[] {"true"}, "", 0);
assertEquals(true, o);
return null;
});
}
}

0 comments on commit efa49df

Please sign in to comment.