Skip to content

Commit c1ac6c1

Browse files
aardvark179gbrail
authored andcommitted
Start using the new scopes hierarchy internally.
1 parent fe87bc9 commit c1ac6c1

81 files changed

Lines changed: 397 additions & 326 deletions

File tree

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/Matrix.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.mozilla.javascript.Context;
1010
import org.mozilla.javascript.Scriptable;
1111
import org.mozilla.javascript.ScriptableObject;
12+
import org.mozilla.javascript.VarScope;
1213

1314
/**
1415
* Matrix: An example host object class that implements the Scriptable interface.
@@ -188,14 +189,14 @@ public void setPrototype(Scriptable prototype) {
188189

189190
/** Get parent. */
190191
@Override
191-
public Scriptable getParentScope() {
192+
public VarScope getParentScope() {
192193
return parent;
193194
}
194195

195196
/** Set parent. */
196197
@Override
197198
public void setParentScope(Scriptable parent) {
198-
this.parent = parent;
199+
this.parent = (VarScope) parent;
199200
}
200201

201202
/**
@@ -239,5 +240,6 @@ public boolean hasInstance(Scriptable value) {
239240
private int dim;
240241

241242
private List<Object> list;
242-
private Scriptable prototype, parent;
243+
private Scriptable prototype;
244+
private VarScope parent;
243245
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@
88
import org.mozilla.javascript.Context;
99
import org.mozilla.javascript.Scriptable;
1010
import org.mozilla.javascript.ScriptableObject;
11+
import org.mozilla.javascript.VarScope;
1112

1213
/**
1314
* This class makes the Bindings object into a Scriptable. That way, we can query and modify the
1415
* contents of the Bindings on demand.
1516
*/
1617
public class BindingsObject extends ScriptableObject {
1718
private final Bindings bindings;
19+
private final VarScope parentScope;
1820

19-
BindingsObject(Bindings bindings) {
21+
BindingsObject(VarScope parentScope, Bindings bindings) {
2022
if (bindings == null) {
2123
throw new IllegalArgumentException("Bindings must not be null");
2224
}
2325
this.bindings = bindings;
26+
this.parentScope = parentScope;
2427
}
2528

2629
@Override
@@ -38,7 +41,7 @@ public Object get(String name, Scriptable start) {
3841

3942
@Override
4043
public void put(String name, Scriptable start, Object value) {
41-
bindings.put(name, Context.javaToJS(value, start));
44+
bindings.put(name, Context.javaToJS(value, parentScope));
4245
}
4346

4447
@Override

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,15 @@ private Scriptable initScope(Context cx, ScriptContext sc) throws ScriptExceptio
9999
var globalScope =
100100
TopLevel.createIsolate(
101101
topLevelScope,
102-
new BindingsObject(sc.getBindings(ScriptContext.GLOBAL_SCOPE)));
102+
new BindingsObject(
103+
topLevelScope, sc.getBindings(ScriptContext.GLOBAL_SCOPE)));
103104
return TopLevel.createIsolate(
104-
globalScope, new BindingsObject(sc.getBindings(ScriptContext.ENGINE_SCOPE)));
105+
globalScope,
106+
new BindingsObject(globalScope, sc.getBindings(ScriptContext.ENGINE_SCOPE)));
105107
} else {
106108
return TopLevel.createIsolate(
107-
topLevelScope, new BindingsObject(sc.getBindings(ScriptContext.ENGINE_SCOPE)));
109+
topLevelScope,
110+
new BindingsObject(topLevelScope, sc.getBindings(ScriptContext.ENGINE_SCOPE)));
108111
}
109112
}
110113

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import org.mozilla.javascript.Context;
1616
import org.mozilla.javascript.ContextFactory;
1717
import org.mozilla.javascript.Kit;
18+
import org.mozilla.javascript.ScopeObject;
1819
import org.mozilla.javascript.Scriptable;
19-
import org.mozilla.javascript.ScriptableObject;
2020
import org.mozilla.javascript.commonjs.module.ModuleScope;
2121
import org.mozilla.javascript.tools.shell.Global;
2222

@@ -181,7 +181,7 @@ public static void main(String[] args) {
181181
global.installRequire(cx, List.of(), false);
182182

183183
URI uri = new File(System.getProperty("user.dir")).toURI();
184-
ScriptableObject scope = ModuleScope.createModuleScope(global, uri, null);
184+
ScopeObject scope = ModuleScope.createModuleScope(global, uri, null);
185185

186186
main.setScope(scope);
187187

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.mozilla.javascript.ScriptRuntime;
1919
import org.mozilla.javascript.Scriptable;
2020
import org.mozilla.javascript.ScriptableObject;
21+
import org.mozilla.javascript.VarScope;
2122

2223
/**
2324
* Environment, intended to be instantiated at global scope, provides a natural way to access System
@@ -30,7 +31,7 @@ public class Environment extends ScriptableObject {
3031

3132
private Environment thePrototypeInstance = null;
3233

33-
public static void defineClass(ScriptableObject scope) {
34+
public static void defineClass(VarScope scope) {
3435
try {
3536
ScriptableObject.defineClass(scope, Environment.class);
3637
} catch (Exception e) {
@@ -47,7 +48,7 @@ public Environment() {
4748
if (thePrototypeInstance == null) thePrototypeInstance = this;
4849
}
4950

50-
public Environment(ScriptableObject scope) {
51+
public Environment(VarScope scope) {
5152
setParentScope(scope);
5253
Object ctor = ScriptRuntime.getTopLevelProp(scope, "Environment");
5354
if (ctor != null && ctor instanceof Scriptable) {

rhino-xml/src/main/java/org/mozilla/javascript/xmlimpl/XML.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.mozilla.javascript.ScriptableObject;
1818
import org.mozilla.javascript.SymbolKey;
1919
import org.mozilla.javascript.Undefined;
20+
import org.mozilla.javascript.VarScope;
2021
import org.mozilla.javascript.xml.XMLObject;
2122

2223
class XML extends XMLObjectImpl {
@@ -37,11 +38,7 @@ class XML extends XMLObjectImpl {
3738
}
3839

3940
public static void init(
40-
Context cx,
41-
ScriptableObject scope,
42-
XMLObjectImpl proto,
43-
boolean sealed,
44-
XMLLibImpl lib) {
41+
Context cx, VarScope scope, XMLObjectImpl proto, boolean sealed, XMLLibImpl lib) {
4542
DESCRIPTOR.buildConstructor(
4643
cx,
4744
scope,
@@ -114,7 +111,7 @@ static Object js_hasInstance(
114111
return false;
115112
}
116113

117-
XML(XMLLibImpl lib, Scriptable scope, XMLObject prototype, XmlNode node) {
114+
XML(XMLLibImpl lib, VarScope scope, XMLObject prototype, XmlNode node) {
118115
super(lib, scope, prototype);
119116
initialize(node);
120117
}

rhino-xml/src/main/java/org/mozilla/javascript/xmlimpl/XMLLibImpl.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import org.mozilla.javascript.Ref;
1414
import org.mozilla.javascript.ScriptRuntime;
1515
import org.mozilla.javascript.Scriptable;
16-
import org.mozilla.javascript.ScriptableObject;
1716
import org.mozilla.javascript.Undefined;
17+
import org.mozilla.javascript.VarScope;
1818
import org.mozilla.javascript.Wrapper;
1919
import org.mozilla.javascript.xml.XMLLib;
2020
import org.mozilla.javascript.xml.XMLObject;
@@ -39,11 +39,12 @@ public static org.w3c.dom.Node toDomNode(Object xmlObject) {
3939
}
4040
}
4141

42-
public static void init(Context cx, Scriptable scope, boolean sealed) {
42+
public static void init(Context cx, Scriptable s, boolean sealed) {
43+
VarScope scope = (VarScope) s;
4344
XMLLibImpl lib = new XMLLibImpl(scope);
4445
XMLLib bound = lib.bindToScope(scope);
4546
if (bound == lib) {
46-
lib.exportToScope(cx, (ScriptableObject) scope, sealed);
47+
lib.exportToScope(cx, scope, sealed);
4748
}
4849
}
4950

@@ -97,7 +98,7 @@ public int getPrettyIndent() {
9798
return options.getPrettyIndent();
9899
}
99100

100-
private Scriptable globalScope;
101+
private VarScope globalScope;
101102

102103
private XML xmlPrototype;
103104
private XMLList xmlListPrototype;
@@ -106,7 +107,7 @@ public int getPrettyIndent() {
106107

107108
private XmlProcessor options = new XmlProcessor();
108109

109-
private XMLLibImpl(Scriptable globalScope) {
110+
private XMLLibImpl(VarScope globalScope) {
110111
this.globalScope = globalScope;
111112
}
112113

@@ -130,7 +131,7 @@ XmlProcessor getProcessor() {
130131
return options;
131132
}
132133

133-
private void exportToScope(Context cx, ScriptableObject scope, boolean sealed) {
134+
private void exportToScope(Context cx, VarScope scope, boolean sealed) {
134135
xmlPrototype = newXML(XmlNode.createText(options, ""));
135136
xmlListPrototype = newXMLList();
136137
namespacePrototype = Namespace.create(this.globalScope, null, XmlNode.Namespace.GLOBAL);
@@ -318,7 +319,7 @@ private Ref xmlPrimaryReference(Context cx, XMLName xmlName, Scriptable scope) {
318319
// XML object can only present on scope chain as a wrapper
319320
// of XMLWithScope
320321
if (scope instanceof XMLWithScope) {
321-
xmlObj = (XMLObjectImpl) scope.getPrototype();
322+
xmlObj = (XMLObjectImpl) ((XMLWithScope) scope).getObject();
322323
if (xmlObj.hasXMLProperty(xmlName)) {
323324
break;
324325
}

rhino-xml/src/main/java/org/mozilla/javascript/xmlimpl/XMLList.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.mozilla.javascript.ScriptableObject;
2020
import org.mozilla.javascript.SymbolKey;
2121
import org.mozilla.javascript.Undefined;
22+
import org.mozilla.javascript.VarScope;
2223
import org.mozilla.javascript.xml.XMLObject;
2324

2425
class XMLList extends XMLObjectImpl implements Function {
@@ -79,7 +80,7 @@ private static Object js_constructorCall(
7980
}
8081
}
8182

82-
XMLList(XMLLibImpl lib, Scriptable scope, XMLObject prototype) {
83+
XMLList(XMLLibImpl lib, VarScope scope, XMLObject prototype) {
8384
super(lib, scope, prototype);
8485
_annos = new XmlNode.InternalList();
8586
}

rhino-xml/src/main/java/org/mozilla/javascript/xmlimpl/XMLLoaderImpl.java

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

33
import org.mozilla.javascript.LazilyLoadedCtor;
4-
import org.mozilla.javascript.ScriptableObject;
4+
import org.mozilla.javascript.ScopeObject;
55
import org.mozilla.javascript.xml.XMLLib;
66
import org.mozilla.javascript.xml.XMLLoader;
77

88
public class XMLLoaderImpl implements XMLLoader {
99
@Override
10-
public void load(ScriptableObject scope, boolean sealed) {
10+
public void load(ScopeObject scope, boolean sealed) {
1111
String implClass = XMLLibImpl.class.getName();
12-
new LazilyLoadedCtor(scope, "XML", implClass, sealed, true);
13-
new LazilyLoadedCtor(scope, "XMLList", implClass, sealed, true);
14-
new LazilyLoadedCtor(scope, "Namespace", implClass, sealed, true);
15-
new LazilyLoadedCtor(scope, "QName", implClass, sealed, true);
12+
new LazilyLoadedCtor<>(scope, "XML", implClass, sealed, true);
13+
new LazilyLoadedCtor<>(scope, "XMLList", implClass, sealed, true);
14+
new LazilyLoadedCtor<>(scope, "Namespace", implClass, sealed, true);
15+
new LazilyLoadedCtor<>(scope, "QName", implClass, sealed, true);
1616
}
1717

1818
@Override

rhino-xml/src/main/java/org/mozilla/javascript/xmlimpl/XMLObjectImpl.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
import org.mozilla.javascript.Context;
1414
import org.mozilla.javascript.JSFunction;
1515
import org.mozilla.javascript.Kit;
16-
import org.mozilla.javascript.NativeWith;
1716
import org.mozilla.javascript.Node;
1817
import org.mozilla.javascript.Ref;
1918
import org.mozilla.javascript.ScriptRuntime;
2019
import org.mozilla.javascript.Scriptable;
2120
import org.mozilla.javascript.ScriptableObject;
2221
import org.mozilla.javascript.SymbolScriptable;
2322
import org.mozilla.javascript.Undefined;
23+
import org.mozilla.javascript.VarScope;
24+
import org.mozilla.javascript.WithScope;
2425
import org.mozilla.javascript.xml.XMLObject;
2526

2627
/**
@@ -123,7 +124,7 @@ public static ClassDescriptor.Builder populatePrototypeDescriptor(
123124
0);
124125
}
125126

126-
protected XMLObjectImpl(XMLLibImpl lib, Scriptable scope, XMLObject prototype) {
127+
protected XMLObjectImpl(XMLLibImpl lib, VarScope scope, XMLObject prototype) {
127128
initialize(lib, scope, prototype);
128129
}
129130

@@ -456,12 +457,12 @@ public Ref memberRef(Context cx, Object namespace, Object elem, int memberTypeFl
456457
}
457458

458459
@Override
459-
public NativeWith enterWith(Scriptable scope) {
460+
public WithScope enterWith(VarScope scope) {
460461
return new XMLWithScope(lib, scope, this);
461462
}
462463

463464
@Override
464-
public NativeWith enterDotQuery(Scriptable scope) {
465+
public WithScope enterDotQuery(VarScope scope) {
465466
XMLWithScope xws = new XMLWithScope(lib, scope, this);
466467
xws.initAsDotQuery();
467468
return xws;

0 commit comments

Comments
 (0)