Skip to content

Commit

Permalink
Merge branch 'master' into integer-operations
Browse files Browse the repository at this point in the history
  • Loading branch information
gbrail committed Aug 18, 2024
2 parents ec3ad24 + cbb0e2f commit a12b5eb
Show file tree
Hide file tree
Showing 30 changed files with 1,012 additions and 1,477 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/scorecard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ jobs:
# Upload the results to GitHub's code scanning dashboard (optional).
# Commenting out will disable upload of results to your repo's Code Scanning dashboard
- name: "Upload to code-scanning"
uses: github/codeql-action/upload-sarif@eb055d739abdc2e8de2e5f4ba1a8b246daa779aa # v3.26.0
uses: github/codeql-action/upload-sarif@429e1977040da7a23b6822b13c129cd1ba93dbb2 # v3.26.2
with:
sarif_file: results.sarif
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import org.mozilla.javascript.Kit;
import org.mozilla.javascript.UintMap;

/**
* ClassFileWriter
Expand Down Expand Up @@ -230,7 +230,7 @@ public void startMethod(String methodName, String type, short flags) {
short methodNameIndex = itsConstantPool.addUtf8(methodName);
short typeIndex = itsConstantPool.addUtf8(type);
itsCurrentMethod = new ClassFileMethod(methodName, methodNameIndex, type, typeIndex, flags);
itsJumpFroms = new UintMap();
itsJumpFroms = new HashMap<>();
itsMethods.add(itsCurrentMethod);
addSuperBlockStart(0);
}
Expand Down Expand Up @@ -4347,7 +4347,7 @@ private void finalizeSuperBlockStarts() {
// Necessary for generating type information for dead code, which is
// expected by the Sun verifier. It is only necessary to store a single
// jump source to determine if a block is reachable or not.
private UintMap itsJumpFroms = null;
private HashMap<Integer, Integer> itsJumpFroms = null;

private static final int LineNumberTableSize = 16;
private static final int ExceptionTableSize = 4;
Expand Down
39 changes: 19 additions & 20 deletions rhino/src/main/java/org/mozilla/classfile/ConstantPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

package org.mozilla.classfile;

import org.mozilla.javascript.ObjToIntMap;
import org.mozilla.javascript.UintMap;
import java.util.HashMap;

final class ConstantPool {
ConstantPool(ClassFileWriter cfw) {
Expand Down Expand Up @@ -84,7 +83,7 @@ int addConstant(double k) {

int addConstant(String k) {
int utf8Index = 0xFFFF & addUtf8(k);
int theIndex = itsStringConstHash.getInt(utf8Index, -1);
int theIndex = itsStringConstHash.getOrDefault(utf8Index, -1);
if (theIndex == -1) {
theIndex = itsTopIndex++;
ensure(3);
Expand Down Expand Up @@ -156,7 +155,7 @@ int getUtfEncodingLimit(String s, int start, int end) {
}

short addUtf8(String k) {
int theIndex = itsUtf8Hash.get(k, -1);
int theIndex = itsUtf8Hash.getOrDefault(k, -1);
if (theIndex == -1) {
int strLen = k.length();
boolean tooBigString;
Expand Down Expand Up @@ -223,12 +222,12 @@ private short addNameAndType(String name, String type) {
}

short addClass(String className) {
int theIndex = itsClassHash.get(className, -1);
int theIndex = itsClassHash.getOrDefault(className, -1);
if (theIndex == -1) {
String slashed = className;
if (className.indexOf('.') > 0) {
slashed = ClassFileWriter.getSlashedForm(className);
theIndex = itsClassHash.get(slashed, -1);
theIndex = itsClassHash.getOrDefault(slashed, -1);
if (theIndex != -1) {
itsClassHash.put(className, theIndex);
}
Expand All @@ -253,7 +252,7 @@ short addClass(String className) {
short addFieldRef(String className, String fieldName, String fieldType) {
FieldOrMethodRef ref = new FieldOrMethodRef(className, fieldName, fieldType);

int theIndex = itsFieldRefHash.get(ref, -1);
int theIndex = itsFieldRefHash.getOrDefault(ref, -1);
if (theIndex == -1) {
short ntIndex = addNameAndType(fieldName, fieldType);
short classIndex = addClass(className);
Expand All @@ -272,7 +271,7 @@ short addFieldRef(String className, String fieldName, String fieldType) {
short addMethodRef(String className, String methodName, String methodType) {
FieldOrMethodRef ref = new FieldOrMethodRef(className, methodName, methodType);

int theIndex = itsMethodRefHash.get(ref, -1);
int theIndex = itsMethodRefHash.getOrDefault(ref, -1);
if (theIndex == -1) {
short ntIndex = addNameAndType(methodName, methodType);
short classIndex = addClass(className);
Expand Down Expand Up @@ -304,7 +303,7 @@ short addInterfaceMethodRef(String className, String methodName, String methodTy
short addInvokeDynamic(String methodName, String methodType, int bootstrapIndex) {
ConstantEntry entry =
new ConstantEntry(CONSTANT_InvokeDynamic, bootstrapIndex, methodName, methodType);
int theIndex = itsConstantHash.get(entry, -1);
int theIndex = itsConstantHash.getOrDefault(entry, -1);

if (theIndex == -1) {
short nameTypeIndex = addNameAndType(methodName, methodType);
Expand All @@ -321,7 +320,7 @@ short addInvokeDynamic(String methodName, String methodType, int bootstrapIndex)
}

short addMethodHandle(ClassFileWriter.MHandle mh) {
int theIndex = itsConstantHash.get(mh, -1);
int theIndex = itsConstantHash.getOrDefault(mh, -1);

if (theIndex == -1) {
short ref;
Expand All @@ -345,15 +344,15 @@ short addMethodHandle(ClassFileWriter.MHandle mh) {
}

Object getConstantData(int index) {
return itsConstantData.getObject(index);
return itsConstantData.get(index);
}

void setConstantData(int index, Object data) {
itsConstantData.put(index, data);
}

byte getConstantType(int index) {
return (byte) itsPoolTypes.getInt(index, 0);
return itsPoolTypes.getOrDefault(index, (byte) 0);
}

private void ensure(int howMuch) {
Expand All @@ -372,16 +371,16 @@ private void ensure(int howMuch) {

private static final int MAX_UTF_ENCODING_SIZE = 65535;

private UintMap itsStringConstHash = new UintMap();
private ObjToIntMap itsUtf8Hash = new ObjToIntMap();
private ObjToIntMap itsFieldRefHash = new ObjToIntMap();
private ObjToIntMap itsMethodRefHash = new ObjToIntMap();
private ObjToIntMap itsClassHash = new ObjToIntMap();
private ObjToIntMap itsConstantHash = new ObjToIntMap();
private final HashMap<Integer, Integer> itsStringConstHash = new HashMap<>();
private final HashMap<String, Integer> itsUtf8Hash = new HashMap<>();
private final HashMap<FieldOrMethodRef, Integer> itsFieldRefHash = new HashMap<>();
private final HashMap<FieldOrMethodRef, Integer> itsMethodRefHash = new HashMap<>();
private final HashMap<String, Integer> itsClassHash = new HashMap<>();
private final HashMap<Object, Integer> itsConstantHash = new HashMap<>();

private int itsTop;
private int itsTopIndex;
private UintMap itsConstantData = new UintMap();
private UintMap itsPoolTypes = new UintMap();
private final HashMap<Integer, Object> itsConstantData = new HashMap<>();
private final HashMap<Integer, Byte> itsPoolTypes = new HashMap<>();
private byte[] itsPool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public enum IterativeOperation {
SOME,
FIND,
FIND_INDEX,
FIND_LAST,
FIND_LAST_INDEX,
}

public enum ReduceOperation {
Expand All @@ -35,7 +37,10 @@ public static Object iterativeMethod(
Object[] args) {
Scriptable o = ScriptRuntime.toObject(cx, scope, thisObj);

if (IterativeOperation.FIND == operation || IterativeOperation.FIND_INDEX == operation) {
if (IterativeOperation.FIND == operation
|| IterativeOperation.FIND_INDEX == operation
|| IterativeOperation.FIND_LAST == operation
|| IterativeOperation.FIND_LAST_INDEX == operation) {
requireObjectCoercible(cx, o, fun);
}

Expand All @@ -62,12 +67,29 @@ public static Object iterativeMethod(
array = cx.newArray(scope, resultLength);
}
long j = 0;
for (long i = 0; i < length; i++) {
long start =
(operation == IterativeOperation.FIND_LAST
|| operation == IterativeOperation.FIND_LAST_INDEX)
? length - 1
: 0;
long end =
(operation == IterativeOperation.FIND_LAST
|| operation == IterativeOperation.FIND_LAST_INDEX)
? -1
: length;
long increment =
(operation == IterativeOperation.FIND_LAST
|| operation == IterativeOperation.FIND_LAST_INDEX)
? -1
: +1;
for (long i = start; i != end; i += increment) {
Object[] innerArgs = new Object[3];
Object elem = getRawElem(o, i);
if (elem == NOT_FOUND) {
if (operation == IterativeOperation.FIND
|| operation == IterativeOperation.FIND_INDEX) {
|| operation == IterativeOperation.FIND_INDEX
|| operation == IterativeOperation.FIND_LAST
|| operation == IterativeOperation.FIND_LAST_INDEX) {
elem = Undefined.instance;
} else {
continue;
Expand All @@ -93,6 +115,7 @@ public static Object iterativeMethod(
if (ScriptRuntime.toBoolean(result)) return Boolean.TRUE;
break;
case FIND:
case FIND_LAST:
if (ScriptRuntime.toBoolean(result)) return elem;
break;
case FIND_INDEX:
Expand All @@ -110,6 +133,7 @@ public static Object iterativeMethod(
case SOME:
return Boolean.FALSE;
case FIND_INDEX:
case FIND_LAST_INDEX:
return ScriptRuntime.wrapNumber(-1);
case FOR_EACH:
default:
Expand Down
28 changes: 14 additions & 14 deletions rhino/src/main/java/org/mozilla/javascript/CodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.mozilla.javascript.ast.AstNode;
import org.mozilla.javascript.ast.AstRoot;
import org.mozilla.javascript.ast.Block;
Expand Down Expand Up @@ -39,16 +41,16 @@ class CodeGenerator extends Icode {
private int lineNumber;
private int doubleTableTop;

private ObjToIntMap strings = new ObjToIntMap(20);
private ObjToIntMap bigInts = new ObjToIntMap(20);
private final HashMap<String, Integer> strings = new HashMap<>();
private final HashMap<BigInteger, Integer> bigInts = new HashMap<>();
private int localTop;
private int[] labelTable;
private int labelTableTop;

// fixupTable[i] = (label_index << 32) | fixup_site
private long[] fixupTable;
private int fixupTableTop;
private ArrayList<Object> literalIds = new ArrayList<>();
private final ArrayList<Object> literalIds = new ArrayList<>();

private int exceptionTableTop;

Expand Down Expand Up @@ -147,10 +149,9 @@ private void generateICodeFromTree(Node tree) {
itsData.itsStringTable = null;
} else {
itsData.itsStringTable = new String[strings.size()];
ObjToIntMap.Iterator iter = strings.newIterator();
for (iter.start(); !iter.done(); iter.next()) {
String str = (String) iter.getKey();
int index = iter.getValue();
for (Map.Entry<String, Integer> e : strings.entrySet()) {
String str = e.getKey();
int index = e.getValue();
if (itsData.itsStringTable[index] != null) Kit.codeBug();
itsData.itsStringTable[index] = str;
}
Expand All @@ -166,10 +167,9 @@ private void generateICodeFromTree(Node tree) {
itsData.itsBigIntTable = null;
} else {
itsData.itsBigIntTable = new BigInteger[bigInts.size()];
ObjToIntMap.Iterator iter = bigInts.newIterator();
for (iter.start(); !iter.done(); iter.next()) {
BigInteger bigInt = (BigInteger) iter.getKey();
int index = iter.getValue();
for (Map.Entry<BigInteger, Integer> e : bigInts.entrySet()) {
BigInteger bigInt = e.getKey();
int index = e.getValue();
if (itsData.itsBigIntTable[index] != null) Kit.codeBug();
itsData.itsBigIntTable[index] = bigInt;
}
Expand Down Expand Up @@ -1343,7 +1343,7 @@ private void resolveGoto(int fromPC, int jumpPC) {
int offsetSite = fromPC + 1;
if (offset != (short) offset) {
if (itsData.longJumps == null) {
itsData.longJumps = new UintMap();
itsData.longJumps = new HashMap<>();
}
itsData.longJumps.put(offsetSite, jumpPC);
offset = 0;
Expand Down Expand Up @@ -1469,7 +1469,7 @@ private void addIndexOp(int op, int index) {
}

private void addStringPrefix(String str) {
int index = strings.get(str, -1);
int index = strings.getOrDefault(str, -1);
if (index == -1) {
index = strings.size();
strings.put(str, index);
Expand All @@ -1489,7 +1489,7 @@ private void addStringPrefix(String str) {
}

private void addBigInt(BigInteger n) {
int index = bigInts.get(n, -1);
int index = bigInts.getOrDefault(n, -1);
if (index == -1) {
index = bigInts.size();
bigInts.put(n, index);
Expand Down
2 changes: 1 addition & 1 deletion rhino/src/main/java/org/mozilla/javascript/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -2675,7 +2675,7 @@ public static boolean isCurrentContextStrict() {
// for Objects, Arrays to tag themselves as being printed out,
// so they don't print themselves out recursively.
// Use ObjToIntMap instead of java.util.HashSet for JDK 1.1 compatibility
ObjToIntMap iterating;
Set<Scriptable> iterating;

Object interpreterSecurityDomain;

Expand Down
14 changes: 10 additions & 4 deletions rhino/src/main/java/org/mozilla/javascript/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import org.mozilla.javascript.ScriptRuntime.NoSuchMethodShim;
Expand Down Expand Up @@ -869,7 +870,7 @@ private static int bytecodeSpan(int bytecode) {
}

static int[] getLineNumbers(InterpreterData data) {
UintMap presentLines = new UintMap();
HashSet<Integer> presentLines = new HashSet<>();

byte[] iCode = data.itsICode;
int iCodeLength = iCode.length;
Expand All @@ -879,12 +880,17 @@ static int[] getLineNumbers(InterpreterData data) {
if (bytecode == Icode_LINE) {
if (span != 3) Kit.codeBug();
int line = getIndex(iCode, pc + 1);
presentLines.put(line, 0);
presentLines.add(line);
}
pc += span;
}

return presentLines.getKeys();
int[] ret = new int[presentLines.size()];
int i = 0;
for (int num : presentLines) {
ret[i++] = num;
}
return ret;
}

@Override
Expand Down Expand Up @@ -2564,7 +2570,7 @@ private static Object interpretLoop(Context cx, CallFrame frame, Object throwabl
// -1 accounts for pc pointing to jump opcode + 1
frame.pc += offset - 1;
} else {
frame.pc = frame.idata.longJumps.getExistingInt(frame.pc);
frame.pc = frame.idata.longJumps.get(frame.pc);
}
if (instructionCounting) {
frame.pcPrevBranch = frame.pc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.Serializable;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Map;
import org.mozilla.javascript.debug.DebuggableScript;

final class InterpreterData implements Serializable, DebuggableScript {
Expand Down Expand Up @@ -83,7 +84,7 @@ private void init() {

Object[] literalIds;

UintMap longJumps;
Map<Integer, Integer> longJumps;

int firstLinePC = -1; // PC for the first LINE icode

Expand Down
Loading

0 comments on commit a12b5eb

Please sign in to comment.