Skip to content

Commit 19e9931

Browse files
committed
Clean up ModuleScope.
1 parent bd5c374 commit 19e9931

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.lang.reflect.Member;
1515
import java.lang.reflect.Method;
1616
import java.lang.reflect.Modifier;
17-
import org.mozilla.javascript.commonjs.module.ModuleScope;
1817
import org.mozilla.javascript.lc.type.TypeInfo;
1918
import org.mozilla.javascript.lc.type.TypeInfoFactory;
2019

@@ -399,22 +398,8 @@ public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] ar
399398
thisObj = ((Delegator) thisObj).getDelegee();
400399
}
401400
if (!clazz.isInstance(thisObj)) {
402-
boolean compatible = false;
403-
if (thisObj == scope || thisObj instanceof ModuleScope) {
404-
Scriptable parentScope = getDeclarationScope();
405-
if (scope != parentScope) {
406-
// Call with dynamic scope for standalone function,
407-
// use parentScope as thisObj
408-
compatible = clazz.isInstance(parentScope);
409-
if (compatible) {
410-
thisObj = parentScope;
411-
}
412-
}
413-
}
414-
if (!compatible) {
415-
// Couldn't find an object to call this on.
416-
throw ScriptRuntime.typeErrorById("msg.incompat.call", functionName);
417-
}
401+
// Couldn't find an object to call this on.
402+
throw ScriptRuntime.typeErrorById("msg.incompat.call", functionName);
418403
}
419404
}
420405

rhino/src/main/java/org/mozilla/javascript/commonjs/module/ModuleScope.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
package org.mozilla.javascript.commonjs.module;
66

77
import java.net.URI;
8+
import org.mozilla.javascript.Scriptable;
89
import org.mozilla.javascript.ScriptableObject;
910
import org.mozilla.javascript.TopLevel;
1011

1112
/**
1213
* A top-level module scope. This class provides methods to retrieve the module's source and base
1314
* URIs in order to resolve relative module IDs and check sandbox constraints.
1415
*/
15-
public class ModuleScope extends TopLevel.GlobalThis {
16+
public class ModuleScope extends ScriptableObject {
1617
private static final long serialVersionUID = 1L;
1718
private final URI uri;
1819
private final URI base;
@@ -24,8 +25,8 @@ private ModuleScope(URI uri, URI base) {
2425

2526
public static ScriptableObject createModuleScope(TopLevel global, URI uri, URI base) {
2627
var moduleScope = new ModuleScope(uri, base);
27-
var scope = global.createIsolate(moduleScope);
28-
return scope;
28+
moduleScope.setParentScope(global);
29+
return moduleScope;
2930
}
3031

3132
public URI getUri() {
@@ -35,4 +36,21 @@ public URI getUri() {
3536
public URI getBase() {
3637
return base;
3738
}
39+
40+
@Override
41+
public String getClassName() {
42+
return "module";
43+
}
44+
45+
/** Search up the chain of scopes to find a module scope. */
46+
public static ModuleScope findModuleScope(Scriptable scope) {
47+
Scriptable current = scope;
48+
while (current != null) {
49+
if (current instanceof ModuleScope) {
50+
return (ModuleScope) current;
51+
}
52+
current = current.getParentScope();
53+
}
54+
return null;
55+
}
3856
}

rhino/src/main/java/org/mozilla/javascript/commonjs/module/Require.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.mozilla.javascript.Scriptable;
1818
import org.mozilla.javascript.ScriptableObject;
1919
import org.mozilla.javascript.TopLevel;
20+
import org.mozilla.javascript.Undefined;
2021

2122
/**
2223
* Implements the require() function as defined by <a
@@ -176,12 +177,18 @@ public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] ar
176177
if (args == null || args.length < 1) {
177178
throw ScriptRuntime.throwError(cx, scope, "require() needs one argument");
178179
}
180+
// Top scope from the point of view of the callee
181+
TopLevel topScope = ScriptableObject.getTopLevelScope(scope);
182+
if (thisObj == null || Undefined.isUndefined(thisObj)) {
183+
thisObj = topScope.getGlobalThis();
184+
}
185+
ModuleScope moduleScope = ModuleScope.findModuleScope(scope);
179186

180187
String id = (String) Context.jsToJava(args[0], String.class);
181188
URI uri = null;
182189
URI base = null;
183190
if (id.startsWith("./") || id.startsWith("../")) {
184-
if (!(thisObj instanceof ModuleScope)) {
191+
if (moduleScope == null) {
185192
throw ScriptRuntime.throwError(
186193
cx,
187194
scope,
@@ -190,7 +197,6 @@ public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] ar
190197
+ "\" when require() is used outside of a module");
191198
}
192199

193-
ModuleScope moduleScope = (ModuleScope) thisObj;
194200
base = moduleScope.getBase();
195201
URI current = moduleScope.getUri();
196202
uri = current.resolve(id);

0 commit comments

Comments
 (0)