Skip to content

Commit

Permalink
Remove obsolete "ObjArray" class
Browse files Browse the repository at this point in the history
Depending on context, replace this venerable class with either ArrayList
or ArrayDeque.
  • Loading branch information
gbrail committed Aug 13, 2024
1 parent 0bd385d commit f3c64ee
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 461 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.mozilla.javascript.Callable;
import org.mozilla.javascript.Context;
Expand All @@ -22,7 +24,6 @@
import org.mozilla.javascript.ImporterTopLevel;
import org.mozilla.javascript.Kit;
import org.mozilla.javascript.NativeCall;
import org.mozilla.javascript.ObjArray;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
Expand Down Expand Up @@ -388,15 +389,16 @@ private String getNormalizedUrl(DebuggableScript fnOrScript) {

/** Returns an array of all functions in the given script. */
private static DebuggableScript[] getAllFunctions(DebuggableScript function) {
ObjArray functions = new ObjArray();
ArrayList<DebuggableScript> functions = new ArrayList<>();
collectFunctions_r(function, functions);
DebuggableScript[] result = new DebuggableScript[functions.size()];
functions.toArray(result);
return result;
}

/** Helper function for {@link #getAllFunctions(DebuggableScript)}. */
private static void collectFunctions_r(DebuggableScript function, ObjArray array) {
private static void collectFunctions_r(
DebuggableScript function, List<DebuggableScript> array) {
array.add(function);
for (int i = 0; i != function.getFunctionCount(); ++i) {
collectFunctions_r(function.getFunction(i), array);
Expand Down Expand Up @@ -912,7 +914,7 @@ public void handleCompilationDone(Context cx, DebuggableScript fnOrScript, Strin
public static class ContextData {

/** The stack frames. */
private ObjArray frameStack = new ObjArray();
private ArrayList<StackFrame> frameStack = new ArrayList<>();

/** Whether the debugger should break at the next line in this context. */
private boolean breakNextLine;
Expand Down Expand Up @@ -947,12 +949,12 @@ public StackFrame getFrame(int frameNumber) {

/** Pushes a stack frame on to the stack. */
private void pushFrame(StackFrame frame) {
frameStack.push(frame);
frameStack.add(frame);
}

/** Pops a stack frame from the stack. */
private void popFrame() {
frameStack.pop();
frameStack.remove(frameStack.size() - 1);
}
}

Expand Down
16 changes: 8 additions & 8 deletions rhino/src/main/java/org/mozilla/classfile/ClassFileWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import org.mozilla.javascript.Kit;
import org.mozilla.javascript.ObjArray;
import org.mozilla.javascript.UintMap;

/**
Expand Down Expand Up @@ -211,7 +211,7 @@ public void addVariableDescriptor(String name, String type, int startPC, int reg
int descriptorIndex = itsConstantPool.addUtf8(type);
int[] chunk = {nameIndex, descriptorIndex, startPC, register};
if (itsVarDescriptors == null) {
itsVarDescriptors = new ObjArray();
itsVarDescriptors = new ArrayList<>();
}
itsVarDescriptors.add(chunk);
}
Expand Down Expand Up @@ -852,7 +852,7 @@ public void addInvokeDynamic(
BootstrapEntry bsmEntry = new BootstrapEntry(bsm, bsmArgs);

if (itsBootstrapMethods == null) {
itsBootstrapMethods = new ObjArray();
itsBootstrapMethods = new ArrayList<>();
}
int bootstrapIndex = itsBootstrapMethods.indexOf(bsmEntry);
if (bootstrapIndex == -1) {
Expand Down Expand Up @@ -4487,9 +4487,9 @@ public String toString() {
private int itsMaxStack;
private int itsMaxLocals;

private ObjArray itsMethods = new ObjArray();
private ObjArray itsFields = new ObjArray();
private ObjArray itsInterfaces = new ObjArray();
private ArrayList<ClassFileMethod> itsMethods = new ArrayList<>();
private ArrayList<ClassFileField> itsFields = new ArrayList<>();
private ArrayList<Short> itsInterfaces = new ArrayList<>();

private int itsFlags;
private int itsThisClassIndex;
Expand All @@ -4504,8 +4504,8 @@ public String toString() {
private static final int MIN_FIXUP_TABLE_SIZE = 40;
private long[] itsFixupTable;
private int itsFixupTableTop;
private ObjArray itsVarDescriptors;
private ObjArray itsBootstrapMethods;
private ArrayList<int[]> itsVarDescriptors;
private ArrayList<BootstrapEntry> itsBootstrapMethods;
private int itsBootstrapMethodsLength = 0;

private char[] tmpCharBuffer = new char[64];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.mozilla.javascript;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.mozilla.javascript.ast.AstNode;
Expand Down Expand Up @@ -47,7 +48,7 @@ class CodeGenerator extends Icode {
// fixupTable[i] = (label_index << 32) | fixup_site
private long[] fixupTable;
private int fixupTableTop;
private ObjArray literalIds = new ObjArray();
private ArrayList<Object> literalIds = new ArrayList<>();

private int exceptionTableTop;

Expand Down
3 changes: 2 additions & 1 deletion rhino/src/main/java/org/mozilla/javascript/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -2712,7 +2713,7 @@ public static boolean isCurrentContextStrict() {

// For the interpreter to store information about previous invocations
// interpreter invocations
ObjArray previousInterpreterInvocations;
Deque<Object> previousInterpreterInvocations;

// For instruction counting (interpreter only)
int instructionCount;
Expand Down
11 changes: 8 additions & 3 deletions rhino/src/main/java/org/mozilla/javascript/ImporterTopLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package org.mozilla.javascript;

import java.util.ArrayList;

/**
* Class ImporterTopLevel
*
Expand Down Expand Up @@ -129,7 +131,9 @@ private static Object[] getNativeJavaPackages(Scriptable scope) {
synchronized (scope) {
if (scope instanceof ScriptableObject) {
ScriptableObject so = (ScriptableObject) scope;
ObjArray importedPackages = (ObjArray) so.getAssociatedValue(AKEY);
@SuppressWarnings("unchecked")
ArrayList<Object> importedPackages =
(ArrayList<Object>) so.getAssociatedValue(AKEY);
if (importedPackages != null) {
return importedPackages.toArray();
}
Expand Down Expand Up @@ -194,9 +198,10 @@ private static void importPackage(ScriptableObject scope, NativeJavaPackage pkg)
return;
}
synchronized (scope) {
ObjArray importedPackages = (ObjArray) scope.getAssociatedValue(AKEY);
@SuppressWarnings("unchecked")
ArrayList<Object> importedPackages = (ArrayList<Object>) scope.getAssociatedValue(AKEY);
if (importedPackages == null) {
importedPackages = new ObjArray();
importedPackages = new ArrayList<>();
scope.associateValue(AKEY, importedPackages);
}
for (int j = 0; j != importedPackages.size(); j++) {
Expand Down
3 changes: 2 additions & 1 deletion rhino/src/main/java/org/mozilla/javascript/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.PrintStream;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -1179,7 +1180,7 @@ private static Object interpretLoop(Context cx, CallFrame frame, Object throwabl
// save the top frame from the previous interpretLoop
// invocation on the stack
if (cx.previousInterpreterInvocations == null) {
cx.previousInterpreterInvocations = new ObjArray();
cx.previousInterpreterInvocations = new ArrayDeque<>();
}
cx.previousInterpreterInvocations.push(cx.lastInterpreterFrame);
}
Expand Down
11 changes: 6 additions & 5 deletions rhino/src/main/java/org/mozilla/javascript/JavaMembers.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ public int hashCode() {
}
}

@SuppressWarnings("unchecked")
private void reflect(
Context cx, Scriptable scope, boolean includeProtected, boolean includePrivate) {
// We reflect methods first, because we want overloaded field/method
Expand All @@ -436,14 +437,14 @@ private void reflect(
if (value == null) {
ht.put(name, method);
} else {
ObjArray overloadedMethods;
if (value instanceof ObjArray) {
overloadedMethods = (ObjArray) value;
ArrayList<Object> overloadedMethods;
if (value instanceof ArrayList) {
overloadedMethods = (ArrayList<Object>) value;
} else {
if (!(value instanceof Method)) Kit.codeBug();
// value should be instance of Method as at this stage
// staticMembers and members can only contain methods
overloadedMethods = new ObjArray();
overloadedMethods = new ArrayList<>();
overloadedMethods.add(value);
ht.put(name, overloadedMethods);
}
Expand All @@ -463,7 +464,7 @@ private void reflect(
methodBoxes = new MemberBox[1];
methodBoxes[0] = new MemberBox((Method) value);
} else {
ObjArray overloadedMethods = (ObjArray) value;
ArrayList<Object> overloadedMethods = (ArrayList<Object>) value;
int N = overloadedMethods.size();
if (N < 2) Kit.codeBug();
methodBoxes = new MemberBox[N];
Expand Down
31 changes: 16 additions & 15 deletions rhino/src/main/java/org/mozilla/javascript/NodeTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

package org.mozilla.javascript;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import org.mozilla.javascript.ast.FunctionNode;
import org.mozilla.javascript.ast.Jump;
Expand Down Expand Up @@ -43,8 +45,8 @@ public final void transform(ScriptNode tree, boolean inStrictMode, CompilerEnvir
}

private void transformCompilationUnit(ScriptNode tree, boolean inStrictMode) {
loops = new ObjArray();
loopEnds = new ObjArray();
loops = new ArrayDeque<>();
loopEnds = new ArrayDeque<>();

// to save against upchecks if no finally blocks are used.
hasFinally = false;
Expand Down Expand Up @@ -162,8 +164,8 @@ private void transformCompilationUnit_r(
*/
if (!hasFinally) break; // skip the whole mess.
Node unwindBlock = null;
for (int i = loops.size() - 1; i >= 0; i--) {
Node n = (Node) loops.get(i);
// Iterate from the top of the stack (most recently inserted) and down
for (Node n : loops) {
int elemtype = n.getType();
if (elemtype == Token.TRY || elemtype == Token.WITH) {
Node unwind;
Expand Down Expand Up @@ -208,15 +210,14 @@ private void transformCompilationUnit_r(
Jump jumpStatement = jump.getJumpStatement();
if (jumpStatement == null) Kit.codeBug();

for (int i = loops.size(); ; ) {
if (i == 0) {
// Parser/IRFactory ensure that break/continue
// always has a jump statement associated with it
// which should be found
throw Kit.codeBug();
}
--i;
Node n = (Node) loops.get(i);
if (loops.isEmpty()) {
// Parser/IRFactory ensure that break/continue
// always has a jump statement associated with it
// which should be found
throw Kit.codeBug();
}
// Iterate from the top of the stack (most recently inserted) and down
for (Node n : loops) {
if (n == jumpStatement) {
break;
}
Expand Down Expand Up @@ -549,7 +550,7 @@ private static Node replaceCurrent(Node parent, Node previous, Node current, Nod
return replacement;
}

private ObjArray loops;
private ObjArray loopEnds;
private Deque<Node> loops;
private Deque<Node> loopEnds;
private boolean hasFinally;
}
Loading

0 comments on commit f3c64ee

Please sign in to comment.