Skip to content

Commit e2d7579

Browse files
aardvark179gbrail
authored andcommitted
Move JSCode signatures to use VarScope for scopes.
1 parent 0d3e9d4 commit e2d7579

53 files changed

Lines changed: 172 additions & 147 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/src/main/java/DynamicScopes.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.mozilla.javascript.Context;
88
import org.mozilla.javascript.ContextFactory;
99
import org.mozilla.javascript.Script;
10-
import org.mozilla.javascript.Scriptable;
1110
import org.mozilla.javascript.TopLevel;
1211

1312
/** Example of controlling the JavaScript with multiple scopes and threads. */
@@ -135,7 +134,7 @@ static void runScripts(Context cx, Script script) {
135134

136135
static class PerThread implements Runnable {
137136

138-
PerThread(Scriptable sharedScope, String source, String x) {
137+
PerThread(TopLevel sharedScope, String source, String x) {
139138
this.sharedScope = sharedScope;
140139
this.source = source;
141140
this.x = x;
@@ -147,14 +146,7 @@ public void run() {
147146
Context cx = Context.enter();
148147
try {
149148
// We can share the scope.
150-
Scriptable threadScope = cx.newObject(sharedScope);
151-
threadScope.setPrototype(sharedScope);
152-
153-
// We want "threadScope" to be a new top-level
154-
// scope, so set its parent scope to null. This
155-
// means that any variables created by assignments
156-
// will be properties of "threadScope".
157-
threadScope.setParentScope(null);
149+
var threadScope = TopLevel.createIsolate(sharedScope);
158150

159151
// Create a JavaScript property of the thread scope named
160152
// 'x' and save a value for it.
@@ -165,7 +157,7 @@ public void run() {
165157
}
166158
}
167159

168-
private Scriptable sharedScope;
160+
private TopLevel sharedScope;
169161
private String source;
170162
private String x;
171163
}

examples/src/main/java/Shell.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public static void main(String args[]) {
5151
// This must be done before scripts can be executed.
5252
Shell shell = new Shell();
5353
TopLevel topLevel = new TopLevel(shell);
54+
shell.scope = topLevel;
5455
cx.initStandardObjects(topLevel);
5556

5657
// Define some global functions particular to the shell. Note
@@ -246,7 +247,8 @@ private void processSource(Context cx, String filename) {
246247
// resolved by appending more source.
247248
if (cx.stringIsCompilableUnit(source)) break;
248249
}
249-
Object result = cx.evaluateString(this, source, sourceName, startline, null);
250+
Object result =
251+
cx.evaluateString(this.scope, source, sourceName, startline, null);
250252
if (result != Context.getUndefinedValue()) {
251253
System.err.println(Context.toString(result));
252254
}
@@ -283,7 +285,7 @@ private void processSource(Context cx, String filename) {
283285
// Here we evalute the entire contents of the file as
284286
// a script. Text is printed only if the print() function
285287
// is called.
286-
cx.evaluateReader(this, in, filename, 1, null);
288+
cx.evaluateReader(this.scope, in, filename, 1, null);
287289
} catch (WrappedException we) {
288290
System.err.println(we.getWrappedException().toString());
289291
we.printStackTrace();
@@ -314,4 +316,6 @@ private static void p(String s) {
314316
private Shell() {
315317
// Utility class - prevent instantiation
316318
}
319+
320+
private TopLevel scope;
317321
}

it-android/src/main/java/org/mozilla/javascript/android/TestCase.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import org.mozilla.javascript.Context;
1313
import org.mozilla.javascript.ContextFactory;
1414
import org.mozilla.javascript.ScriptRuntime;
15-
import org.mozilla.javascript.Scriptable;
1615
import org.mozilla.javascript.TopLevel;
16+
import org.mozilla.javascript.VarScope;
1717

1818
/**
1919
* Utility class, that search for testcases in "assets/tests".
@@ -55,14 +55,14 @@ public TestCase(String name, TopLevel global) {
5555
public String run() {
5656
Context cx = factory.enterContext();
5757
try {
58-
Scriptable scope = TopLevel.createIsolate(global);
58+
TopLevel scope = TopLevel.createIsolate(global);
5959
return ScriptRuntime.toString(runTest(cx, scope));
6060
} finally {
6161
Context.exit();
6262
}
6363
}
6464

65-
protected abstract Object runTest(Context cx, Scriptable scope);
65+
protected abstract Object runTest(Context cx, VarScope scope);
6666

6767
@Override
6868
public String toString() {
@@ -78,7 +78,7 @@ public AssetScript(String name, TopLevel global, AssetManager assetManager) {
7878
}
7979

8080
@Override
81-
protected Object runTest(Context cx, Scriptable scope) {
81+
protected Object runTest(Context cx, VarScope scope) {
8282
try (InputStream in = assetManager.open("tests/" + name);
8383
Reader rdr = new InputStreamReader(in, StandardCharsets.UTF_8)) {
8484
return cx.evaluateReader(scope, rdr, name, 1, null);

it-android/src/main/java/org/mozilla/javascript/android/TypeInfoFactoryTestCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.mozilla.javascript.android;
22

33
import org.mozilla.javascript.Context;
4-
import org.mozilla.javascript.Scriptable;
54
import org.mozilla.javascript.TopLevel;
5+
import org.mozilla.javascript.VarScope;
66
import org.mozilla.javascript.lc.type.TypeInfoFactory;
77
import org.mozilla.javascript.lc.type.impl.factory.ClassValueCacheFactory;
88

@@ -15,7 +15,7 @@ public TypeInfoFactoryTestCase(String name, TopLevel global) {
1515
}
1616

1717
@Override
18-
protected Object runTest(Context cx, Scriptable scope) {
18+
protected Object runTest(Context cx, VarScope scope) {
1919
TypeInfoFactory typeInfoFactory = TypeInfoFactory.get(scope);
2020
int apiLevel = android.os.Build.VERSION.SDK_INT;
2121
if (apiLevel >= 34 != typeInfoFactory instanceof ClassValueCacheFactory) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class RhinoScriptEngine extends AbstractScriptEngine implements Compilabl
8585
this.builtins = new Builtins();
8686
}
8787

88-
private VarScope initScope(Context cx, ScriptContext sc) throws ScriptException {
88+
private TopLevel initScope(Context cx, ScriptContext sc) throws ScriptException {
8989
configureContext(cx);
9090

9191
if (topLevelScope == null) {
@@ -115,7 +115,7 @@ private VarScope initScope(Context cx, ScriptContext sc) throws ScriptException
115115
@Override
116116
public Object eval(String script, ScriptContext context) throws ScriptException {
117117
try (Context cx = ctxFactory.enterContext()) {
118-
Scriptable scope = initScope(cx, context);
118+
TopLevel scope = initScope(cx, context);
119119
Object ret = cx.evaluateString(scope, script, getFilename(), 0, null);
120120
return Context.jsToJava(ret, Object.class);
121121
} catch (RhinoException re) {
@@ -127,7 +127,7 @@ public Object eval(String script, ScriptContext context) throws ScriptException
127127
@Override
128128
public Object eval(Reader reader, ScriptContext context) throws ScriptException {
129129
try (Context cx = ctxFactory.enterContext()) {
130-
Scriptable scope = initScope(cx, context);
130+
TopLevel scope = initScope(cx, context);
131131
Object ret = cx.evaluateReader(scope, reader, getFilename(), 0, null);
132132
return Context.jsToJava(ret, Object.class);
133133
} catch (RhinoException re) {
@@ -166,7 +166,7 @@ public CompiledScript compile(Reader script) throws ScriptException {
166166

167167
Object eval(Script script, ScriptContext sc) throws ScriptException {
168168
try (Context cx = ctxFactory.enterContext()) {
169-
Scriptable scope = initScope(cx, sc);
169+
TopLevel scope = initScope(cx, sc);
170170
Object ret = script.exec(cx, scope, scope);
171171
return Context.jsToJava(ret, Object.class);
172172
} catch (RhinoException re) {

rhino-tools/src/main/java/org/mozilla/javascript/tools/debugger/Dim.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ public Object run(Context cx) {
826826

827827
case IPROXY_EVAL_SCRIPT:
828828
{
829-
Scriptable scope = null;
829+
VarScope scope = null;
830830
if (dim.scopeProvider != null) {
831831
scope = dim.scopeProvider.getScope();
832832
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.mozilla.javascript.Kit;
1818
import org.mozilla.javascript.ScopeObject;
1919
import org.mozilla.javascript.Scriptable;
20+
import org.mozilla.javascript.VarScope;
2021
import org.mozilla.javascript.commonjs.module.ModuleScope;
2122
import org.mozilla.javascript.tools.shell.Global;
2223

@@ -77,7 +78,7 @@ public void go() {
7778
}
7879

7980
/** Sets the scope to be used for script evaluation. */
80-
public void setScope(Scriptable scope) {
81+
public void setScope(VarScope scope) {
8182
setScopeProvider(IProxy.newScopeProvider(scope));
8283
}
8384

@@ -238,7 +239,7 @@ private static Main mainEmbeddedImpl(
238239
if (scopeProvider instanceof ScopeProvider) {
239240
main.setScopeProvider((ScopeProvider) scopeProvider);
240241
} else {
241-
Scriptable scope = (Scriptable) scopeProvider;
242+
VarScope scope = (VarScope) scopeProvider;
242243
if (scope instanceof Global) {
243244
Global global = (Global) scope;
244245
global.setIn(main.getIn());
@@ -316,15 +317,15 @@ private static class IProxy implements Runnable, ScopeProvider {
316317
private final int type;
317318

318319
/** The scope object to expose when {@link #type} = {@link #SCOPE_PROVIDER}. */
319-
private Scriptable scope;
320+
private VarScope scope;
320321

321322
/** Creates a new IProxy. */
322323
public IProxy(int type) {
323324
this.type = type;
324325
}
325326

326327
/** Creates a new IProxy that acts as a {@link ScopeProvider}. */
327-
public static ScopeProvider newScopeProvider(Scriptable scope) {
328+
public static ScopeProvider newScopeProvider(VarScope scope) {
328329
IProxy scopeProvider = new IProxy(SCOPE_PROVIDER);
329330
scopeProvider.scope = scope;
330331
return scopeProvider;
@@ -343,7 +344,7 @@ public void run() {
343344

344345
/** Returns the scope for script evaluations. */
345346
@Override
346-
public Scriptable getScope() {
347+
public VarScope getScope() {
347348
if (type != SCOPE_PROVIDER) Kit.codeBug();
348349
if (scope == null) Kit.codeBug();
349350
return scope;

rhino-tools/src/main/java/org/mozilla/javascript/tools/debugger/ScopeProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
66
package org.mozilla.javascript.tools.debugger;
77

8-
import org.mozilla.javascript.Scriptable;
8+
import org.mozilla.javascript.VarScope;
99

1010
/** Interface to provide a scope object for script evaluation to the debugger. */
1111
public interface ScopeProvider {
1212

1313
/** Returns the scope object to be used for script evaluation. */
14-
Scriptable getScope();
14+
VarScope getScope();
1515
}

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
@@ -323,7 +323,7 @@ public static void loadClass(Context cx, Scriptable thisObj, Object[] args, Func
323323
throw reportRuntimeError("msg.must.implement.Script");
324324
}
325325
Script script = (Script) clazz.getDeclaredConstructor().newInstance();
326-
script.exec(cx, thisObj, thisObj);
326+
script.exec(cx, funObj.getDeclarationScope(), thisObj);
327327
}
328328

329329
private static Class<?> getClass(Object[] args) {
@@ -411,7 +411,7 @@ public static Object doctest(Context cx, Scriptable thisObj, Object[] args, Func
411411

412412
@SuppressWarnings("AndroidJdkLibsChecker")
413413
public int runDoctest(
414-
Context cx, Scriptable scope, String session, String sourceName, int lineNumber) {
414+
Context cx, VarScope scope, String session, String sourceName, int lineNumber) {
415415
doctestCanonicalizations = new HashMap<String, String>();
416416
String[] lines = session.split("\r\n?|\n", -1);
417417
String prompt0 = prompts[0].trim();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ public Object run() {
135135
URL url = getUrlObj(filename);
136136
ProtectionDomain staticDomain = getUrlDomain(url);
137137
try {
138-
Main.processFileSecure(cx, scope, url.toExternalForm(), staticDomain);
138+
Main.processFileSecure(
139+
cx, (VarScope) scope, url.toExternalForm(), staticDomain);
139140
} catch (IOException ioex) {
140141
throw new RuntimeException(ioex);
141142
}
@@ -218,7 +219,7 @@ public Object callWithDomain(
218219
final Scriptable scope,
219220
final Scriptable thisObj,
220221
final Object[] args) {
221-
return doAction(securityDomain, () -> script.exec(cx, scope, thisObj));
222+
return doAction(securityDomain, () -> script.exec(cx, (VarScope) scope, thisObj));
222223
}
223224

224225
private Object doAction(Object securityDomain, PrivilegedAction<Object> action) {

0 commit comments

Comments
 (0)