Skip to content

Commit c1ee781

Browse files
authored
Moving TopLevel away from being an IdScriptbaleObject
Clean up this code to align with latest Rhino coding practices.
1 parent f3bda3a commit c1ee781

File tree

2 files changed

+34
-94
lines changed

2 files changed

+34
-94
lines changed

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

Lines changed: 33 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@
4747
public class ImporterTopLevel extends TopLevel {
4848
private static final long serialVersionUID = -9095380847465315412L;
4949

50-
private static final Object IMPORTER_TAG = "Importer";
51-
5250
public ImporterTopLevel() {}
5351

5452
public ImporterTopLevel(Context cx) {
@@ -65,8 +63,28 @@ public String getClassName() {
6563
}
6664

6765
public static void init(Context cx, Scriptable scope, boolean sealed) {
68-
ImporterTopLevel obj = new ImporterTopLevel();
69-
obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed);
66+
init(cx, scope, sealed, false);
67+
}
68+
69+
public static void init(Context cx, Scriptable scope, boolean sealed, boolean isTopScope) {
70+
LambdaConstructor ctor =
71+
new LambdaConstructor(scope, "ImporterTopLevel", 0, ImporterTopLevel::js_construct);
72+
73+
ctor.definePrototypeMethod(scope, "importClass", 1, ImporterTopLevel::js_importClass);
74+
ctor.definePrototypeMethod(scope, "importPackage", 1, ImporterTopLevel::js_importPackage);
75+
76+
if (sealed) {
77+
ctor.sealObject();
78+
}
79+
80+
var proto = (Scriptable) ctor.getPrototypeProperty();
81+
82+
if (isTopScope) {
83+
scope.put("importClass", scope, proto.get("importClass", proto));
84+
scope.put("importPackage", scope, proto.get("importPackage", proto));
85+
}
86+
87+
ScriptableObject.defineProperty(scope, "JavaImporter", ctor, DONTENUM);
7088
}
7189

7290
public void initStandardObjects(Context cx, boolean sealed) {
@@ -77,10 +95,9 @@ public void initStandardObjects(Context cx, boolean sealed) {
7795
// If seal is true then exportAsJSClass(cx, seal) would seal
7896
// this obj. Since this is scope as well, it would not allow
7997
// to add variables.
80-
IdFunctionObject ctor = exportAsJSClass(MAX_PROTOTYPE_ID, this, false);
81-
if (sealed) {
82-
ctor.sealObject();
83-
}
98+
99+
init(cx, this, sealed, true);
100+
84101
// delete "constructor" defined by exportAsJSClass so "constructor"
85102
// name would refer to Object.constructor
86103
// and not to JavaImporter.prototype.constructor.
@@ -147,10 +164,10 @@ private static Object[] getNativeJavaPackages(Scriptable scope) {
147164
*/
148165
@Deprecated
149166
public void importPackage(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
150-
js_importPackage(this, args);
167+
js_importPackage(cx, funObj.getDeclarationScope(), this, args);
151168
}
152169

153-
private Object js_construct(Scriptable scope, Object[] args) {
170+
private static Scriptable js_construct(Context cx, Scriptable scope, Object[] args) {
154171
ImporterTopLevel result = new ImporterTopLevel();
155172
for (int i = 0; i != args.length; ++i) {
156173
Object arg = args[i];
@@ -169,28 +186,29 @@ private Object js_construct(Scriptable scope, Object[] args) {
169186
// JavaImporter without new and still get properly
170187
// initialized object.
171188
result.setParentScope(scope);
172-
result.setPrototype(this);
173189
return result;
174190
}
175191

176-
private static Object js_importClass(Scriptable scope, Object[] args) {
192+
private static Object js_importClass(
193+
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
177194
for (int i = 0; i != args.length; i++) {
178195
Object arg = args[i];
179196
if (!(arg instanceof NativeJavaClass)) {
180197
throw Context.reportRuntimeErrorById("msg.not.class", Context.toString(arg));
181198
}
182-
importClass(scope, (NativeJavaClass) arg);
199+
importClass((ScriptableObject) thisObj, (NativeJavaClass) arg);
183200
}
184201
return Undefined.instance;
185202
}
186203

187-
private static Object js_importPackage(ScriptableObject scope, Object[] args) {
204+
private static Object js_importPackage(
205+
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
188206
for (int i = 0; i != args.length; i++) {
189207
Object arg = args[i];
190208
if (!(arg instanceof NativeJavaPackage)) {
191209
throw Context.reportRuntimeErrorById("msg.not.pkg", Context.toString(arg));
192210
}
193-
importPackage(scope, (NativeJavaPackage) arg);
211+
importPackage((ScriptableObject) thisObj, (NativeJavaPackage) arg);
194212
}
195213
return Undefined.instance;
196214
}
@@ -229,84 +247,6 @@ private static void importClass(Scriptable scope, NativeJavaClass cl) {
229247
scope.put(n, scope, cl);
230248
}
231249

232-
@Override
233-
protected void initPrototypeId(int id) {
234-
String s;
235-
int arity;
236-
switch (id) {
237-
case Id_constructor:
238-
arity = 0;
239-
s = "constructor";
240-
break;
241-
case Id_importClass:
242-
arity = 1;
243-
s = "importClass";
244-
break;
245-
case Id_importPackage:
246-
arity = 1;
247-
s = "importPackage";
248-
break;
249-
default:
250-
throw new IllegalArgumentException(String.valueOf(id));
251-
}
252-
initPrototypeMethod(IMPORTER_TAG, id, s, arity);
253-
}
254-
255-
@Override
256-
public Object execIdCall(
257-
IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
258-
if (!f.hasTag(IMPORTER_TAG)) {
259-
return super.execIdCall(f, cx, scope, thisObj, args);
260-
}
261-
int id = f.methodId();
262-
switch (id) {
263-
case Id_constructor:
264-
return js_construct(scope, args);
265-
266-
case Id_importClass:
267-
return js_importClass(realScope(scope, thisObj, f), args);
268-
269-
case Id_importPackage:
270-
return js_importPackage(realScope(scope, thisObj, f), args);
271-
}
272-
throw new IllegalArgumentException(String.valueOf(id));
273-
}
274-
275-
private ScriptableObject realScope(Scriptable scope, Scriptable thisObj, IdFunctionObject f) {
276-
if (topScopeFlag) {
277-
// when used as top scope importPackage and importClass are global
278-
// function that ignore thisObj. We use the top level scope
279-
// which might not be the same as 'this' when used shared scopes
280-
thisObj = ScriptableObject.getTopLevelScope(scope);
281-
}
282-
return ensureType(thisObj, ScriptableObject.class, f);
283-
}
284-
285-
@Override
286-
protected int findPrototypeId(String s) {
287-
int id;
288-
switch (s) {
289-
case "constructor":
290-
id = Id_constructor;
291-
break;
292-
case "importClass":
293-
id = Id_importClass;
294-
break;
295-
case "importPackage":
296-
id = Id_importPackage;
297-
break;
298-
default:
299-
id = 0;
300-
break;
301-
}
302-
return id;
303-
}
304-
305-
private static final int Id_constructor = 1,
306-
Id_importClass = 2,
307-
Id_importPackage = 3,
308-
MAX_PROTOTYPE_ID = 3;
309-
310250
private static final String AKEY = "importedPackages";
311251
private boolean topScopeFlag;
312252
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* dynamic scopes) embeddings should explicitly call {@link #cacheBuiltins(Scriptable, boolean)} to
3030
* initialize the class cache for each top-level scope.
3131
*/
32-
public class TopLevel extends IdScriptableObject {
32+
public class TopLevel extends ScriptableObject {
3333

3434
private static final long serialVersionUID = -4648046356662472260L;
3535

0 commit comments

Comments
 (0)