From efa49df66aff6e467189328d87bd3f0ba12ddb68 Mon Sep 17 00:00:00 2001 From: "andrea.bergia" Date: Tue, 29 Oct 2024 14:27:00 +0100 Subject: [PATCH] Allow passing `Undefined.instance` to `ScriptRuntime.evalSpecial` --- .../org/mozilla/javascript/ScriptRuntime.java | 6 +++- .../ScriptRuntimeEvalSpecialTest.java | 35 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 rhino/src/test/java/org/mozilla/javascript/ScriptRuntimeEvalSpecialTest.java diff --git a/rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java b/rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java index 0b5389d7f5..ea7be1f238 100644 --- a/rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java +++ b/rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java @@ -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 */ diff --git a/rhino/src/test/java/org/mozilla/javascript/ScriptRuntimeEvalSpecialTest.java b/rhino/src/test/java/org/mozilla/javascript/ScriptRuntimeEvalSpecialTest.java new file mode 100644 index 0000000000..b841dabe16 --- /dev/null +++ b/rhino/src/test/java/org/mozilla/javascript/ScriptRuntimeEvalSpecialTest.java @@ -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; + }); + } +}