Skip to content

Commit f93786d

Browse files
authored
Fix this in CommonJS require
In modules included by `require`, `this` should be an empty object, but instead it was set to the same as the scope. To fix this, we need to add a new parameter to the Script.exec() function so that "scope" and "this" can both be specified. The old function has been deprecated.
1 parent 9e6dd8c commit f93786d

28 files changed

Lines changed: 93 additions & 41 deletions

File tree

benchmarks/src/jmh/java/org/mozilla/javascript/benchmarks/BuiltinBenchmark.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void init()
7474

7575
@Benchmark
7676
public Object annotatedClassMethods(AnnotatedClassState state) {
77-
return state.testScript.exec(state.cx, state.scope);
77+
return state.testScript.exec(state.cx, state.scope, state.scope);
7878
}
7979

8080
public static class AnnotatedClass extends ScriptableObject {
@@ -138,7 +138,7 @@ public void init()
138138

139139
@Benchmark
140140
public Object idClassMethods(IdClassState state) {
141-
return state.testScript.exec(state.cx, state.scope);
141+
return state.testScript.exec(state.cx, state.scope, state.scope);
142142
}
143143

144144
public static class IdClass extends IdScriptableObject {
@@ -331,7 +331,7 @@ public void init()
331331

332332
@Benchmark
333333
public Object dumbLambdaClassMethods(DumbLambdaState state) {
334-
return state.testScript.exec(state.cx, state.scope);
334+
return state.testScript.exec(state.cx, state.scope, state.scope);
335335
}
336336

337337
private static class DumbLambdaClass extends ScriptableObject {

benchmarks/src/jmh/java/org/mozilla/javascript/benchmarks/SunSpiderBenchmark.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void tearDown() {
4444
}
4545

4646
Object run() {
47-
return script.exec(cx, scope);
47+
return script.exec(cx, scope, scope);
4848
}
4949
}
5050

examples/src/main/java/DynamicScopes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static void runScripts(Context cx, Script script) {
8989

9090
// Now we can execute the precompiled script against the scope
9191
// to define x variable and f function in the shared scope.
92-
script.exec(cx, sharedScope);
92+
script.exec(cx, sharedScope, sharedScope);
9393

9494
// Now we spawn some threads that execute a script that calls the
9595
// function 'f'. The scope chain looks like this:

rhino-engine/src/main/java/org/mozilla/javascript/engine/RhinoScriptEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public CompiledScript compile(Reader script) throws ScriptException {
163163
Object eval(Script script, ScriptContext sc) throws ScriptException {
164164
try (Context cx = ctxFactory.enterContext()) {
165165
Scriptable scope = initScope(cx, sc);
166-
Object ret = script.exec(cx, scope);
166+
Object ret = script.exec(cx, scope, scope);
167167
return Context.jsToJava(ret, Object.class);
168168
} catch (RhinoException re) {
169169
throw new ScriptException(

rhino-tools/src/main/java/org/mozilla/javascript/tools/shell/Global.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public static void loadClass(Context cx, Scriptable thisObj, Object[] args, Func
312312
throw reportRuntimeError("msg.must.implement.Script");
313313
}
314314
Script script = (Script) clazz.getDeclaredConstructor().newInstance();
315-
script.exec(cx, thisObj);
315+
script.exec(cx, thisObj, thisObj);
316316
}
317317

318318
private static Class<?> getClass(Object[] args) {
@@ -1157,7 +1157,7 @@ public void run() {
11571157
@Override
11581158
public Object run(Context cx) {
11591159
if (f != null) return f.call(cx, scope, scope, args);
1160-
else return s.exec(cx, scope);
1160+
else return s.exec(cx, scope, scope);
11611161
}
11621162

11631163
ContextFactory factory;

rhino-tools/src/main/java/org/mozilla/javascript/tools/shell/Main.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ static void evalInlineScript(Context cx, String scriptText) {
201201
try {
202202
Script script = cx.compileString(scriptText, "<command>", 1, null);
203203
if (script != null) {
204-
script.exec(cx, getShellScope());
204+
Scriptable scope = getShellScope();
205+
script.exec(cx, scope, scope);
205206
}
206207
} catch (RhinoException rex) {
207208
ToolErrorReporter.reportException(cx.getErrorReporter(), rex);
@@ -481,7 +482,7 @@ public static void processSource(Context cx, String filename) throws IOException
481482
String finalSource = source.toString();
482483
Script script = cx.compileString(finalSource, "<stdin>", lineno, null);
483484
if (script != null) {
484-
Object result = script.exec(cx, scope);
485+
Object result = script.exec(cx, scope, scope);
485486
// Avoid printing out undefined or function definitions.
486487
if (result != Context.getUndefinedValue()
487488
&& !(result instanceof Function
@@ -580,7 +581,7 @@ static void processFileSecure(Context cx, Scriptable scope, String path, Object
580581
}
581582

582583
if (script != null) {
583-
script.exec(cx, scope);
584+
script.exec(cx, scope, scope);
584585
}
585586
}
586587

rhino/src/main/java/org/mozilla/javascript/Context.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ public final Object evaluateString(
12401240
Scriptable scope, String source, String sourceName, int lineno, Object securityDomain) {
12411241
Script script = compileString(source, sourceName, lineno, securityDomain);
12421242
if (script != null) {
1243-
return script.exec(this, scope);
1243+
return script.exec(this, scope, scope);
12441244
}
12451245
return null;
12461246
}
@@ -1265,7 +1265,7 @@ public final Object evaluateReader(
12651265
throws IOException {
12661266
Script script = compileReader(in, sourceName, lineno, securityDomain);
12671267
if (script != null) {
1268-
return script.exec(this, scope);
1268+
return script.exec(this, scope, scope);
12691269
}
12701270
return null;
12711271
}

rhino/src/main/java/org/mozilla/javascript/InterpretedFunction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] ar
8888
}
8989

9090
@Override
91-
public Object exec(Context cx, Scriptable scope) {
91+
public Object exec(Context cx, Scriptable scope, Scriptable thisObj) {
9292
if (!isScript()) {
9393
// Can only be applied to scripts
9494
throw new IllegalStateException();
@@ -98,9 +98,9 @@ public Object exec(Context cx, Scriptable scope) {
9898
// It will go through "call" path. but they are equivalent
9999
ret =
100100
ScriptRuntime.doTopCall(
101-
this, cx, scope, scope, ScriptRuntime.emptyArgs, isStrict());
101+
this, cx, scope, thisObj, ScriptRuntime.emptyArgs, isStrict());
102102
} else {
103-
ret = Interpreter.interpret(this, cx, scope, scope, ScriptRuntime.emptyArgs);
103+
ret = Interpreter.interpret(this, cx, scope, thisObj, ScriptRuntime.emptyArgs);
104104
}
105105
cx.processMicrotasks();
106106
return ret;

rhino/src/main/java/org/mozilla/javascript/JavaAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ public static Scriptable runScript(final Script script) {
577577
.call(
578578
cx -> {
579579
ScriptableObject global = ScriptRuntime.getGlobal(cx);
580-
script.exec(cx, global);
580+
script.exec(cx, global, global);
581581
return global;
582582
});
583583
}

rhino/src/main/java/org/mozilla/javascript/NativeScript.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public String getClassName() {
8686
@Override
8787
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
8888
if (script != null) {
89-
return script.exec(cx, scope);
89+
return script.exec(cx, scope, thisObj);
9090
}
9191
return Undefined.instance;
9292
}

0 commit comments

Comments
 (0)