Skip to content

Apache Lucene open-source search software

Rewrite expressions module with Java 24 classfile API (#14597)

Uwe Schindler1b5c25f3d48c
diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml
index 9e702a98e31..5047f70b353 100644
--- a/gradle/libs.versions.toml
+++ b/gradle/libs.versions.toml
@@ -66,7 +66,6 @@ zstd = "1.5.7-2"
[libraries]
antlr-core = { module = "org.antlr:antlr4", version.ref = "antlr" }
antlr-runtime = { module = "org.antlr:antlr4-runtime", version.ref = "antlr" }
-asm-commons = { module = "org.ow2.asm:asm-commons", version.ref = "asm" }
asm-core = { module = "org.ow2.asm:asm", version.ref = "asm" }
assertj = { module = "org.assertj:assertj-core", version.ref = "assertj" }
commons-codec = { module = "commons-codec:commons-codec", version.ref = "commons-codec" }
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index aa9fab13575..3b9e4aedbe2 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -29,7 +29,9 @@ New Features
Improvements
---------------------
-(No changes)
+* GITHUB#14597: Rewrite expressions compiler to use Java 24+ Classfile library instead of ASM.
+ This avoids clashes with other ASM versions on classpath because it removes the dependency.
+ (Uwe Schindler)
Optimizations
---------------------
diff --git a/lucene/expressions/build.gradle b/lucene/expressions/build.gradle
index f129006a8ba..cfe7af7cae8 100644
--- a/lucene/expressions/build.gradle
+++ b/lucene/expressions/build.gradle
@@ -23,11 +23,6 @@ dependencies {
moduleApi project(':lucene:core')
moduleImplementation project(':lucene:codecs')
-
moduleImplementation deps.antlr.runtime
-
- moduleImplementation deps.asm.core
- moduleImplementation deps.asm.commons
-
moduleTestImplementation project(':lucene:test-framework')
}
diff --git a/lucene/expressions/src/java/module-info.java b/lucene/expressions/src/java/module-info.java
index f950d2dc30e..da81e5a4756 100644
--- a/lucene/expressions/src/java/module-info.java
+++ b/lucene/expressions/src/java/module-info.java
@@ -17,8 +17,6 @@
@SuppressWarnings({"requires-automatic"})
module org.apache.lucene.expressions {
- requires org.objectweb.asm;
- requires org.objectweb.asm.commons;
requires org.antlr.antlr4.runtime;
requires org.apache.lucene.core;
requires org.apache.lucene.codecs;
diff --git a/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java b/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java
index 62c1cc8ccbb..e876efbc127 100644
--- a/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java
+++ b/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java
@@ -18,7 +18,23 @@ package org.apache.lucene.expressions.js;
import java.io.IOException;
import java.io.Reader;
+import java.lang.classfile.ClassBuilder;
+import java.lang.classfile.ClassFile;
+import java.lang.classfile.ClassFile.ClassHierarchyResolverOption;
+import java.lang.classfile.ClassHierarchyResolver;
+import java.lang.classfile.CodeBuilder;
+import java.lang.classfile.CodeBuilder.BlockCodeBuilder;
+import java.lang.classfile.Label;
+import java.lang.classfile.Opcode;
+import java.lang.classfile.TypeKind;
+import java.lang.classfile.attribute.ExceptionsAttribute;
+import java.lang.classfile.instruction.BranchInstruction;
+import java.lang.classfile.instruction.OperatorInstruction;
+import java.lang.constant.ClassDesc;
import java.lang.constant.ConstantDescs;
+import java.lang.constant.DirectMethodHandleDesc;
+import java.lang.constant.DynamicConstantDesc;
+import java.lang.constant.MethodTypeDesc;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandleInfo;
import java.lang.invoke.MethodHandles;
@@ -35,11 +51,12 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
+import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
-import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.BaseErrorListener;
+import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.RecognitionException;
@@ -50,14 +67,6 @@ import org.apache.lucene.expressions.Expression;
import org.apache.lucene.expressions.js.JavascriptParser.ExpressionContext;
import org.apache.lucene.search.DoubleValues;
import org.apache.lucene.util.IOUtils;
-import org.objectweb.asm.ClassWriter;
-import org.objectweb.asm.ConstantDynamic;
-import org.objectweb.asm.Handle;
-import org.objectweb.asm.Label;
-import org.objectweb.asm.Opcodes;
-import org.objectweb.asm.Type;
-import org.objectweb.asm.commons.GeneratorAdapter;
-import org.objectweb.asm.commons.Method;
/**
* An expression compiler for javascript expressions.
@@ -97,44 +106,35 @@ public final class JavascriptCompiler {
private static final Lookup LOOKUP = MethodHandles.lookup();
- private static final int CLASSFILE_VERSION = Opcodes.V21;
-
private static final MethodType MT_EXPRESSION_CTOR_LOOKUP =
MethodType.methodType(void.class, String.class, String[].class);
// We use the same class name for all generated classes (they are hidden anyways).
// The source code is displayed as "source file name" in stack trace.
- private static final String COMPILED_EXPRESSION_CLASS =
- JavascriptCompiler.class.getName() + "$CompiledExpression";
- private static final String COMPILED_EXPRESSION_INTERNAL =
- COMPILED_EXPRESSION_CLASS.replace('.', '/');
-
- private static final Type EXPRESSION_TYPE = Type.getType(Expression.class),
- FUNCTION_VALUES_TYPE = Type.getType(DoubleValues.class),
- METHOD_HANDLE_TYPE = Type.getType(MethodHandle.class),
- JAVASCRIPT_COMPILER_TYPE = Type.getType(JavascriptCompiler.class),
- THROWABLE_TYPE = Type.getType(Throwable.class);
- private static final Method
- EXPRESSION_CTOR = getAsmMethod(void.class, "<init>", String.class, String[].class),
- EVALUATE_METHOD = getAsmMethod(double.class, "evaluate", DoubleValues[].class),
- DOUBLE_VAL_METHOD = getAsmMethod(double.class, "doubleValue"),
- PATCH_STACK_METHOD =
- getAsmMethod(Throwable.class, "patchStackTrace", Throwable.class, Expression.class);
- private static final Type[] EVALUATE_EXCEPTIONS = new Type[] {Type.getType(IOException.class)};
- private static final Handle DYNAMIC_CONSTANT_BOOTSTRAP_HANDLE =
- new Handle(
- Opcodes.H_INVOKESTATIC,
- JAVASCRIPT_COMPILER_TYPE.getInternalName(),
+ private static final ClassDesc
+ CD_CompiledExpression =
+ ClassDesc.of(JavascriptCompiler.class.getName() + "$CompiledExpression"),
+ CD_Expression = Expression.class.describeConstable().get(),
+ CD_DoubleValues = DoubleValues.class.describeConstable().get(),
+ CD_JavascriptCompiler = JavascriptCompiler.class.describeConstable().get();
+ private static final MethodTypeDesc
+ MTD_EXPRESSION_CTOR =
+ MethodTypeDesc.of(
+ ConstantDescs.CD_void, ConstantDescs.CD_String, ConstantDescs.CD_String.arrayType()),
+ MTD_EVALUATE = MethodTypeDesc.of(ConstantDescs.CD_double, CD_DoubleValues.arrayType()),
+ MTD_DOUBLE_VAL = MethodTypeDesc.of(ConstantDescs.CD_double),
+ MTD_PATCH_STACK =
+ MethodTypeDesc.of(ConstantDescs.CD_Throwable, ConstantDescs.CD_Throwable, CD_Expression);
+
+ private static final DirectMethodHandleDesc BSM_COMPILER_DYNAMIC_CONSTANT =
+ ConstantDescs.ofConstantBootstrap(
+ CD_JavascriptCompiler,
"dynamicConstantBootstrap",
- MethodType.methodType(
- MethodHandle.class, Lookup.class, String.class, Class.class, String.class)
- .toMethodDescriptorString(),
- false);
-
- /** create an ASM Method object from return type, method name, and parameters. */
- private static Method getAsmMethod(Class<?> rtype, String name, Class<?>... ptypes) {
- return new Method(name, MethodType.methodType(rtype, ptypes).toMethodDescriptorString());
- }
+ ConstantDescs.CD_MethodHandle,
+ ConstantDescs.CD_String);
+
+ private static final ExceptionsAttribute ATTR_THROWS_IOEXCEPTION =
+ ExceptionsAttribute.ofSymbols(IOException.class.describeConstable().get());
final String sourceText;
final Map<String, MethodHandle> functions;
@@ -232,21 +232,24 @@ public final class JavascriptCompiler {
*/
private Expression compileExpression() throws ParseException {
final Map<String, Integer> externalsMap = new LinkedHashMap<>();
- final ClassWriter classWriter =
- new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
-
+ final byte[] classFile;
try {
- generateClass(getAntlrParseTree(), classWriter, externalsMap);
+ classFile =
+ ClassFile.of(
+ ClassHierarchyResolverOption.of(ClassHierarchyResolver.ofClassLoading(LOOKUP)))
+ .build(
+ CD_CompiledExpression,
+ writer -> generateClass(getAntlrParseTree(), writer, externalsMap));
} catch (RuntimeException re) {
- if (re.getCause() instanceof ParseException) {
- throw (ParseException) re.getCause();
+ // Unwrap the ParseException from RuntimeException
+ if (re.getCause() instanceof ParseException cause) {
+ throw cause;
}
throw re;
}
try {
- final Lookup lookup =
- LOOKUP.defineHiddenClassWithClassData(classWriter.toByteArray(), functions, true);
+ final Lookup lookup = LOOKUP.defineHiddenClassWithClassData(classFile, functions, true);
return invokeConstructor(lookup, lookup.lookupClass(), externalsMap);
} catch (ReflectiveOperationException exception) {
throw new IllegalStateException(
@@ -272,12 +275,10 @@ public final class JavascriptCompiler {
* Parses the sourceText into an ANTLR 4 parse tree
*
* @return The ANTLR parse tree
- * @throws ParseException on failure to parse
*/
- private ParseTree getAntlrParseTree() throws ParseException {
- final ANTLRInputStream antlrInputStream = new ANTLRInputStream(sourceText);
+ private ParseTree getAntlrParseTree() {
final JavascriptErrorHandlingLexer javascriptLexer =
- new JavascriptErrorHandlingLexer(antlrInputStream);
+ new JavascriptErrorHandlingLexer(CharStreams.fromString(sourceText));
javascriptLexer.removeErrorListeners();
final JavascriptParser javascriptParser =
new JavascriptParser(new CommonTokenStream(javascriptLexer));
@@ -303,17 +304,16 @@ public final class JavascriptCompiler {
final int charPositionInLine,
final String msg,
final RecognitionException e) {
- throw new RuntimeException(
- new ParseException(
- "line ("
- + line
- + "), offset ("
- + charPositionInLine
- + "), symbol ("
- + offendingSymbol
- + ") "
- + msg,
- charPositionInLine));
+ throw newWrappedParseException(
+ "line ("
+ + line
+ + "), offset ("
+ + charPositionInLine
+ + "), symbol ("
+ + offendingSymbol
+ + ") "
+ + msg,
+ charPositionInLine);
}
});
@@ -323,45 +323,57 @@ public final class JavascriptCompiler {
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
}
- /** Sends the bytecode of class file to {@link ClassWriter}. */
+ /** Sends the bytecode of class file to {@link ClassBuilder}. */
private void generateClass(
final ParseTree parseTree,
- final ClassWriter classWriter,
- final Map<String, Integer> externalsMap)
- throws ParseException {
- classWriter.visit(
- CLASSFILE_VERSION,
- Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL,
- COMPILED_EXPRESSION_INTERNAL,
- null,
- EXPRESSION_TYPE.getInternalName(),
- null);
-
- final GeneratorAdapter constructor =
- new GeneratorAdapter(Opcodes.ACC_PUBLIC, EXPRESSION_CTOR, null, null, classWriter);
- constructor.loadThis();
- constructor.loadArgs();
- constructor.invokeConstructor(EXPRESSION_TYPE, EXPRESSION_CTOR);
- constructor.returnValue();
- constructor.endMethod();
-
- final GeneratorAdapter gen =
- new GeneratorAdapter(
- Opcodes.ACC_PUBLIC, EVALUATE_METHOD, null, EVALUATE_EXCEPTIONS, classWriter);
-
- // add a try/catch block to rewrite stack trace of any Throwable
- final Label beginTry = gen.newLabel(), endTry = gen.newLabel(), catchHandler = gen.newLabel();
- gen.visitTryCatchBlock(beginTry, endTry, catchHandler, THROWABLE_TYPE.getInternalName());
- gen.mark(beginTry);
+ final ClassBuilder builder,
+ final Map<String, Integer> externalsMap) {
+ builder.withFlags(ClassFile.ACC_PUBLIC | ClassFile.ACC_FINAL).withSuperclass(CD_Expression);
+
+ // constructor:
+ builder.withMethodBody(
+ ConstantDescs.INIT_NAME,
+ MTD_EXPRESSION_CTOR,
+ ClassFile.ACC_PUBLIC,
+ gen -> {
+ gen.aload(0); // this
+ gen.aload(gen.parameterSlot(0));
+ gen.aload(gen.parameterSlot(1));
+ gen.invokespecial(CD_Expression, ConstantDescs.INIT_NAME, MTD_EXPRESSION_CTOR);
+ gen.return_();
+ });
+ // evaluate method:
+ final Consumer<BlockCodeBuilder> mainBlock =
+ gen -> {
+ newClassFileGeneratorVisitor(gen, externalsMap).visit(parseTree);
+ gen.dreturn();
+ };
+ final Consumer<BlockCodeBuilder> rethrowBlock =
+ gen -> {
+ gen.aload(0); // this
+ gen.invokestatic(CD_JavascriptCompiler, "patchStackTrace", MTD_PATCH_STACK);
+ gen.athrow();
+ };
+ final Consumer<CodeBuilder> body =
+ gen -> gen.trying(mainBlock, c -> c.catching(ConstantDescs.CD_Throwable, rethrowBlock));
+ builder.withMethod(
+ "evaluate",
+ MTD_EVALUATE,
+ ClassFile.ACC_PUBLIC,
+ mth -> mth.with(ATTR_THROWS_IOEXCEPTION).withCode(body));
+ }
+
+ private JavascriptBaseVisitor<Void> newClassFileGeneratorVisitor(
+ CodeBuilder gen, final Map<String, Integer> externalsMap) {
// to completely hide the ANTLR visitor we use an anonymous impl:
- new JavascriptBaseVisitor<Void>() {
- private final Deque<Type> typeStack = new ArrayDeque<>();
+ return new JavascriptBaseVisitor<Void>() {
+ private final Deque<TypeKind> typeStack = new ArrayDeque<>();
private final Map<String, Integer> constantsMap = new HashMap<>();
@Override
public Void visitCompile(JavascriptParser.CompileContext ctx) {
- typeStack.push(Type.DOUBLE_TYPE);
+ typeStack.push(TypeKind.DOUBLE);
visit(ctx.expression());
typeStack.pop();
@@ -382,8 +394,8 @@ public final class JavascriptCompiler {
} else if (ctx.OCTAL() != null) {
pushLong(Long.parseLong(ctx.OCTAL().getText().substring(1), 8));
} else if (ctx.DECIMAL() != null) {
- gen.push(Double.parseDouble(ctx.DECIMAL().getText()));
- gen.cast(Type.DOUBLE_TYPE, typeStack.peek());
+ gen.loadConstant(Double.parseDouble(ctx.DECIMAL().getText()));
+ gen.conversion(TypeKind.DOUBLE, typeStack.peek());
} else {
throw new IllegalStateException("Unknown operation specified: " + ctx.getText());
}
@@ -398,110 +410,96 @@ public final class JavascriptCompiler {
boolean parens = ctx.LP() != null && ctx.RP() != null;
MethodHandle mh = parens ? functions.get(text) : null;
- try {
- if (mh != null) {
- final int arity = mh.type().parameterCount();
-
- if (arguments != arity) {
- throw new ParseException(
- "Invalid expression '"
- + sourceText
- + "': Expected ("
- + arity
- + ") arguments for function call ("
- + text
- + "), but found ("
- + arguments
- + ").",
- ctx.start.getStartIndex());
- }
-
- // place dynamic constant with MethodHandle on top of stack
- gen.visitLdcInsn(
- new ConstantDynamic(
- "func" + constantsMap.computeIfAbsent(text, _ -> constantsMap.size()),
- METHOD_HANDLE_TYPE.getDescriptor(),
- DYNAMIC_CONSTANT_BOOTSTRAP_HANDLE,
- text));
-
- // add arguments:
- typeStack.push(Type.DOUBLE_TYPE);
- for (int argument = 0; argument < arguments; ++argument) {
- visit(ctx.expression(argument));
- }
- typeStack.pop();
-
- // invoke MethodHandle of function:
- gen.visitMethodInsn(
- Opcodes.INVOKEVIRTUAL,
- METHOD_HANDLE_TYPE.getInternalName(),
- "invokeExact",
- mh.type().descriptorString(),
- false);
-
- gen.cast(Type.DOUBLE_TYPE, typeStack.peek());
- } else if (!parens || arguments == 0 && text.contains(".")) {
- int index;
-
- text = normalizeQuotes(ctx.getText());
- index = externalsMap.computeIfAbsent(text, _ -> externalsMap.size());
-
- gen.loadArg(0);
- gen.push(index);
- gen.arrayLoad(FUNCTION_VALUES_TYPE);
- gen.invokeVirtual(FUNCTION_VALUES_TYPE, DOUBLE_VAL_METHOD);
- gen.cast(Type.DOUBLE_TYPE, typeStack.peek());
- } else {
- throw new ParseException(
+ if (mh != null) {
+ final int arity = mh.type().parameterCount();
+
+ if (arguments != arity) {
+ throw newWrappedParseException(
"Invalid expression '"
+ sourceText
- + "': Unrecognized function call ("
+ + "': Expected ("
+ + arity
+ + ") arguments for function call ("
+ text
+ + "), but found ("
+ + arguments
+ ").",
ctx.start.getStartIndex());
}
- return null;
- } catch (ParseException e) {
- // The API doesn't allow checked exceptions here, so propagate up the stack. This is
- // unwrapped
- // in getAntlrParseTree.
- throw new RuntimeException(e);
+
+ // place dynamic constant with MethodHandle on top of stack
+ final var constantDesc =
+ DynamicConstantDesc.ofNamed(
+ BSM_COMPILER_DYNAMIC_CONSTANT,
+ "func" + constantsMap.computeIfAbsent(text, _ -> constantsMap.size()),
+ ConstantDescs.CD_MethodHandle,
+ text);
+ gen.loadConstant(constantDesc);
+
+ // add arguments:
+ typeStack.push(TypeKind.DOUBLE);
+ for (int argument = 0; argument < arguments; ++argument) {
+ visit(ctx.expression(argument));
+ }
+ typeStack.pop();
+
+ // invoke MethodHandle of function:
+ gen.invokevirtual(
+ ConstantDescs.CD_MethodHandle,
+ "invokeExact",
+ MethodTypeDesc.ofDescriptor(mh.type().descriptorString()));
+
+ gen.conversion(TypeKind.DOUBLE, typeStack.peek());
+ } else if (!parens || arguments == 0 && text.contains(".")) {
+ text = normalizeQuotes(ctx.getText());
+ final int index = externalsMap.computeIfAbsent(text, _ -> externalsMap.size());
+
+ gen.aload(gen.parameterSlot(0));
+ gen.loadConstant(index);
+ gen.arrayLoad(TypeKind.REFERENCE);
+ gen.invokevirtual(CD_DoubleValues, "doubleValue", MTD_DOUBLE_VAL);
+ gen.conversion(TypeKind.DOUBLE, typeStack.peek());
+ } else {
+ throw newWrappedParseException(
+ "Invalid expression '" + sourceText + "': Unrecognized function call (" + text + ").",
+ ctx.start.getStartIndex());
}
+ return null;
}
@Override
public Void visitUnary(JavascriptParser.UnaryContext ctx) {
if (ctx.BOOLNOT() != null) {
- Label labelNotTrue = new Label();
- Label labelNotReturn = new Label();
+ Label labelNotTrue = gen.newLabel();
+ Label labelNotReturn = gen.newLabel();
- typeStack.push(Type.INT_TYPE);
+ typeStack.push(TypeKind.INT);
visit(ctx.expression());
typeStack.pop();
- gen.visitJumpInsn(Opcodes.IFEQ, labelNotTrue);
+ gen.ifeq(labelNotTrue);
pushBoolean(false);
- gen.goTo(labelNotReturn);
- gen.visitLabel(labelNotTrue);
+ gen.goto_(labelNotReturn);
+ gen.labelBinding(labelNotTrue);
pushBoolean(true);
- gen.visitLabel(labelNotReturn);
+ gen.labelBinding(labelNotReturn);
} else if (ctx.BWNOT() != null) {
- typeStack.push(Type.LONG_TYPE);
+ typeStack.push(TypeKind.LONG);
visit(ctx.expression());
typeStack.pop();
- gen.push(-1L);
- gen.visitInsn(Opcodes.LXOR);
- gen.cast(Type.LONG_TYPE, typeStack.peek());
+ gen.loadConstant(-1L);
+ gen.lxor();
+ gen.conversion(TypeKind.LONG, typeStack.peek());
} else if (ctx.ADD() != null) {
visit(ctx.expression());
} else if (ctx.SUB() != null) {
- typeStack.push(Type.DOUBLE_TYPE);
+ typeStack.push(TypeKind.DOUBLE);
visit(ctx.expression());
typeStack.pop();
- gen.visitInsn(Opcodes.DNEG);
- gen.cast(Type.DOUBLE_TYPE, typeStack.peek());
+ gen.dneg();
+ gen.conversion(TypeKind.DOUBLE, typeStack.peek());
} else {
throw new IllegalStateException("Unknown operation specified: " + ctx.getText());
@@ -512,14 +510,14 @@ public final class JavascriptCompiler {
@Override
public Void visitMuldiv(JavascriptParser.MuldivContext ctx) {
- int opcode;
+ Opcode opcode;
if (ctx.MUL() != null) {
- opcode = Opcodes.DMUL;
+ opcode = Opcode.DMUL;
} else if (ctx.DIV() != null) {
- opcode = Opcodes.DDIV;
+ opcode = Opcode.DDIV;
} else if (ctx.REM() != null) {
- opcode = Opcodes.DREM;
+ opcode = Opcode.DREM;
} else {
throw new IllegalStateException("Unknown operation specified: " + ctx.getText());
}
@@ -531,12 +529,12 @@ public final class JavascriptCompiler {
@Override
public Void visitAddsub(JavascriptParser.AddsubContext ctx) {
- int opcode;
+ Opcode opcode;
if (ctx.ADD() != null) {
- opcode = Opcodes.DADD;
+ opcode = Opcode.DADD;
} else if (ctx.SUB() != null) {
- opcode = Opcodes.DSUB;
+ opcode = Opcode.DSUB;
} else {
throw new IllegalStateException("Unknown operation specified: " + ctx.getText());
}
@@ -548,14 +546,14 @@ public final class JavascriptCompiler {
@Override
public Void visitBwshift(JavascriptParser.BwshiftContext ctx) {
- int opcode;
+ Opcode opcode;
if (ctx.LSH() != null) {
- opcode = Opcodes.LSHL;
+ opcode = Opcode.LSHL;
} else if (ctx.RSH() != null) {
- opcode = Opcodes.LSHR;
+ opcode = Opcode.LSHR;
} else if (ctx.USH() != null) {
- opcode = Opcodes.LUSHR;
+ opcode = Opcode.LUSHR;
} else {
throw new IllegalStateException("Unknown operation specified: " + ctx.getText());
}
@@ -567,16 +565,16 @@ public final class JavascriptCompiler {
@Override
public Void visitBoolcomp(JavascriptParser.BoolcompContext ctx) {
- int opcode;
+ Opcode opcode;
if (ctx.LT() != null) {
- opcode = GeneratorAdapter.LT;
+ opcode = Opcode.IFLT;
} else if (ctx.LTE() != null) {
- opcode = GeneratorAdapter.LE;
+ opcode = Opcode.IFLE;
} else if (ctx.GT() != null) {
- opcode = GeneratorAdapter.GT;
+ opcode = Opcode.IFGT;
} else if (ctx.GTE() != null) {
- opcode = GeneratorAdapter.GE;
+ opcode = Opcode.IFGE;
} else {
throw new IllegalStateException("Unknown operation specified: " + ctx.getText());
}
@@ -588,12 +586,12 @@ public final class JavascriptCompiler {
@Override
public Void visitBooleqne(JavascriptParser.BooleqneContext ctx) {
- int opcode;
+ Opcode opcode;
if (ctx.EQ() != null) {
- opcode = GeneratorAdapter.EQ;
+ opcode = Opcode.IFEQ;
} else if (ctx.NE() != null) {
- opcode = GeneratorAdapter.NE;
+ opcode = Opcode.IFNE;
} else {
throw new IllegalStateException("Unknown operation specified: " + ctx.getText());
}
@@ -603,174 +601,170 @@ public final class JavascriptCompiler {
return null;
}
+ private void pushCond(Opcode operator, ExpressionContext left, ExpressionContext right) {
+ Label labelTrue = gen.newLabel();
+ Label labelReturn = gen.newLabel();
+
+ typeStack.push(TypeKind.DOUBLE);
+ visit(left);
+ visit(right);
+ typeStack.pop();
+
+ if (operator == Opcode.IFGE || operator == Opcode.IFGT) {
+ gen.dcmpl();
+ } else {
+ gen.dcmpg();
+ }
+ gen.with(BranchInstruction.of(operator, labelTrue));
+ pushBoolean(false);
+ gen.goto_(labelReturn);
+ gen.labelBinding(labelTrue);
+ pushBoolean(true);
+ gen.labelBinding(labelReturn);
+ }
+
@Override
public Void visitBwand(JavascriptParser.BwandContext ctx) {
- pushBitwise(Opcodes.LAND, ctx.expression(0), ctx.expression(1));
+ pushBitwise(Opcode.LAND, ctx.expression(0), ctx.expression(1));
return null;
}
@Override
public Void visitBwxor(JavascriptParser.BwxorContext ctx) {
- pushBitwise(Opcodes.LXOR, ctx.expression(0), ctx.expression(1));
+ pushBitwise(Opcode.LXOR, ctx.expression(0), ctx.expression(1));
return null;
}
@Override
public Void visitBwor(JavascriptParser.BworContext ctx) {
- pushBitwise(Opcodes.LOR, ctx.expression(0), ctx.expression(1));
+ pushBitwise(Opcode.LOR, ctx.expression(0), ctx.expression(1));
return null;
}
@Override
public Void visitBooland(JavascriptParser.BoolandContext ctx) {
- Label andFalse = new Label();
- Label andEnd = new Label();
+ Label andFalse = gen.newLabel();
+ Label andEnd = gen.newLabel();
- typeStack.push(Type.INT_TYPE);
+ typeStack.push(TypeKind.INT);
visit(ctx.expression(0));
- gen.visitJumpInsn(Opcodes.IFEQ, andFalse);
+ gen.ifeq(andFalse);
visit(ctx.expression(1));
- gen.visitJumpInsn(Opcodes.IFEQ, andFalse);
+ gen.ifeq(andFalse);
typeStack.pop();
pushBoolean(true);
- gen.goTo(andEnd);
- gen.visitLabel(andFalse);
+ gen.goto_(andEnd);
+ gen.labelBinding(andFalse);
pushBoolean(false);
- gen.visitLabel(andEnd);
+ gen.labelBinding(andEnd);
return null;
}
@Override
public Void visitBoolor(JavascriptParser.BoolorContext ctx) {
- Label orTrue = new Label();
- Label orEnd = new Label();
+ Label orTrue = gen.newLabel();
+ Label orEnd = gen.newLabel();
- typeStack.push(Type.INT_TYPE);
+ typeStack.push(TypeKind.INT);
visit(ctx.expression(0));
- gen.visitJumpInsn(Opcodes.IFNE, orTrue);
+ gen.ifne(orTrue);
visit(ctx.expression(1));
- gen.visitJumpInsn(Opcodes.IFNE, orTrue);
+ gen.ifne(orTrue);
typeStack.pop();
pushBoolean(false);
- gen.goTo(orEnd);
- gen.visitLabel(orTrue);
+ gen.goto_(orEnd);
+ gen.labelBinding(orTrue);
pushBoolean(true);
- gen.visitLabel(orEnd);
+ gen.labelBinding(orEnd);
return null;
}
@Override
public Void visitConditional(JavascriptParser.ConditionalContext ctx) {
- Label condFalse = new Label();
- Label condEnd = new Label();
+ Label condFalse = gen.newLabel();
+ Label condEnd = gen.newLabel();
- typeStack.push(Type.INT_TYPE);
+ typeStack.push(TypeKind.INT);
visit(ctx.expression(0));
typeStack.pop();
- gen.visitJumpInsn(Opcodes.IFEQ, condFalse);
+ gen.ifeq(condFalse);
visit(ctx.expression(1));
- gen.goTo(condEnd);
- gen.visitLabel(condFalse);
+ gen.goto_(condEnd);
+ gen.labelBinding(condFalse);
visit(ctx.expression(2));
- gen.visitLabel(condEnd);
+ gen.labelBinding(condEnd);
return null;
}
- private void pushArith(int operator, ExpressionContext left, ExpressionContext right) {
- pushBinaryOp(operator, left, right, Type.DOUBLE_TYPE, Type.DOUBLE_TYPE, Type.DOUBLE_TYPE);
+ private void pushArith(Opcode operator, ExpressionContext left, ExpressionContext right) {
+ pushBinaryOp(operator, left, right, TypeKind.DOUBLE, TypeKind.DOUBLE, TypeKind.DOUBLE);
}
- private void pushShift(int operator, ExpressionContext left, ExpressionContext right) {
- pushBinaryOp(operator, left, right, Type.LONG_TYPE, Type.INT_TYPE, Type.LONG_TYPE);
+ private void pushShift(Opcode operator, ExpressionContext left, ExpressionContext right) {
+ pushBinaryOp(operator, left, right, TypeKind.LONG, TypeKind.INT, TypeKind.LONG);
}
- private void pushBitwise(int operator, ExpressionContext left, ExpressionContext right) {
- pushBinaryOp(operator, left, right, Type.LONG_TYPE, Type.LONG_TYPE, Type.LONG_TYPE);
+ private void pushBitwise(Opcode operator, ExpressionContext left, ExpressionContext right) {
+ pushBinaryOp(operator, left, right, TypeKind.LONG, TypeKind.LONG, TypeKind.LONG);
}
private void pushBinaryOp(
- int operator,
+ Opcode operator,
ExpressionContext left,
ExpressionContext right,
- Type leftType,
- Type rightType,
- Type returnType) {
+ TypeKind leftType,
+ TypeKind rightType,
+ TypeKind returnType) {
typeStack.push(leftType);
visit(left);
typeStack.pop();
typeStack.push(rightType);
visit(right);
typeStack.pop();
- gen.visitInsn(operator);
- gen.cast(returnType, typeStack.peek());
- }
-
- private void pushCond(int operator, ExpressionContext left, ExpressionContext right) {
- Label labelTrue = new Label();
- Label labelReturn = new Label();
-
- typeStack.push(Type.DOUBLE_TYPE);
- visit(left);
- visit(right);
- typeStack.pop();
-
- gen.ifCmp(Type.DOUBLE_TYPE, operator, labelTrue);
- pushBoolean(false);
- gen.goTo(labelReturn);
- gen.visitLabel(labelTrue);
- pushBoolean(true);
- gen.visitLabel(labelReturn);
+ gen.with(OperatorInstruction.of(operator));
+ gen.conversion(returnType, typeStack.peek());
}
private void pushBoolean(boolean truth) {
- switch (typeStack.peek().getSort()) {
- case Type.INT:
- gen.push(truth);
+ switch (typeStack.peek()) {
+ case TypeKind.INT:
+ gen.loadConstant(truth ? 1 : 0);
break;
- case Type.LONG:
- gen.push(truth ? 1L : 0L);
+ case TypeKind.LONG:
+ gen.loadConstant(truth ? 1L : 0L);
break;
- case Type.DOUBLE:
- gen.push(truth ? 1. : 0.);
+ case TypeKind.DOUBLE:
+ gen.loadConstant(truth ? 1. : 0.);
break;
+ // $CASES-OMITTED$
default:
throw new IllegalStateException("Invalid expected type: " + typeStack.peek());
}
}
private void pushLong(long i) {
- switch (typeStack.peek().getSort()) {
- case Type.INT:
- gen.push((int) i);
+ switch (typeStack.peek()) {
+ case TypeKind.INT:
+ gen.loadConstant((int) i);
break;
- case Type.LONG:
- gen.push(i);
+ case TypeKind.LONG:
+ gen.loadConstant(i);
break;
- case Type.DOUBLE:
- gen.push((double) i);
+ case TypeKind.DOUBLE:
+ gen.loadConstant((double) i);
break;
+ // $CASES-OMITTED$
default:
throw new IllegalStateException("Invalid expected type: " + typeStack.peek());
}
}
- }.visit(parseTree);
-
- gen.mark(endTry);
- gen.returnValue();
-
- gen.mark(catchHandler);
- gen.loadThis();
- gen.invokeStatic(JAVASCRIPT_COMPILER_TYPE, PATCH_STACK_METHOD);
- gen.throwException();
- gen.endMethod();
-
- classWriter.visitEnd();
+ };
}
static String normalizeQuotes(String text) {
@@ -813,6 +807,15 @@ public final class JavascriptCompiler {
return start;
}
+ /**
+ * Returns a new {@link ParseException} wrapped in a {@link RuntimeException} because the API
+ * doesn't allow checked exceptions, so propagate up the stack. This is unwrapped in {@link
+ * #compileExpression()}.
+ */
+ static RuntimeException newWrappedParseException(String text, int position) {
+ return new RuntimeException(new ParseException(text, position));
+ }
+
/**
* The default set of functions available to expressions.
*
diff --git a/lucene/licenses/asm-9.8.jar.sha1 b/lucene/licenses/asm-9.8.jar.sha1
deleted file mode 100644
index a0b92ac4b0d..00000000000
--- a/lucene/licenses/asm-9.8.jar.sha1
+++ /dev/null
@@ -1 +0,0 @@
-dc19ecb3f7889b7860697215cae99c0f9b6f6b4b
diff --git a/lucene/licenses/asm-LICENSE-BSD_LIKE.txt b/lucene/licenses/asm-LICENSE-BSD_LIKE.txt
deleted file mode 100644
index afb064f2f26..00000000000
--- a/lucene/licenses/asm-LICENSE-BSD_LIKE.txt
+++ /dev/null
@@ -1,26 +0,0 @@
-Copyright (c) 2012 France Télécom
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-3. Neither the name of the copyright holders nor the names of its
- contributors may be used to endorse or promote products derived from
- this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
-LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
-THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/lucene/licenses/asm-NOTICE.txt b/lucene/licenses/asm-NOTICE.txt
deleted file mode 100644
index 8d1c8b69c3f..00000000000
--- a/lucene/licenses/asm-NOTICE.txt
+++ /dev/null
@@ -1 +0,0 @@
-
diff --git a/lucene/licenses/asm-commons-9.8.jar.sha1 b/lucene/licenses/asm-commons-9.8.jar.sha1
deleted file mode 100644
index 250575d90d8..00000000000
--- a/lucene/licenses/asm-commons-9.8.jar.sha1
+++ /dev/null
@@ -1 +0,0 @@
-36e4d212970388e5bd2c5180292012502df461bb
diff --git a/lucene/licenses/asm-commons-LICENSE-BSD_LIKE.txt b/lucene/licenses/asm-commons-LICENSE-BSD_LIKE.txt
deleted file mode 100644
index afb064f2f26..00000000000
--- a/lucene/licenses/asm-commons-LICENSE-BSD_LIKE.txt
+++ /dev/null
@@ -1,26 +0,0 @@
-Copyright (c) 2012 France Télécom
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-3. Neither the name of the copyright holders nor the names of its
- contributors may be used to endorse or promote products derived from
- this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
-LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
-THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/lucene/licenses/asm-commons-NOTICE.txt b/lucene/licenses/asm-commons-NOTICE.txt
deleted file mode 100644
index 8d1c8b69c3f..00000000000
--- a/lucene/licenses/asm-commons-NOTICE.txt
+++ /dev/null
@@ -1 +0,0 @@
-
diff --git a/lucene/licenses/asm-tree-9.8.jar.sha1 b/lucene/licenses/asm-tree-9.8.jar.sha1
deleted file mode 100644
index d0aa1d7a9bd..00000000000
--- a/lucene/licenses/asm-tree-9.8.jar.sha1
+++ /dev/null
@@ -1 +0,0 @@
-018419ca5b77a2f81097c741e7872e6ab8d2f40d
diff --git a/versions.lock b/versions.lock
index 92ef736d3e6..3483064f0cd 100644
--- a/versions.lock
+++ b/versions.lock
@@ -21,9 +21,6 @@
"org.hamcrest:hamcrest:3.0" : "fa9ef26b,refs=4",
"org.locationtech.spatial4j:spatial4j:0.8" : "cbc357ab,refs=4",
"org.openjdk.jmh:jmh-core:1.37" : "85a1e4c6,refs=2",
- "org.ow2.asm:asm:9.8" : "d9953130,refs=4",
- "org.ow2.asm:asm-commons:9.8" : "d9953130,refs=4",
- "org.ow2.asm:asm-tree:9.8" : "d9953130,refs=4",
"org.slf4j:slf4j-api:2.0.17" : "2f760bab,refs=4",
"ua.net.nlp:morfologik-ukrainian-search:4.9.1" : "fe494320,refs=3",
"xerces:xercesImpl:2.12.2" : "5ce8cdc6,refs=2"
@@ -73,9 +70,6 @@
"org.locationtech.spatial4j:spatial4j:0.8" : "1d5a4b2b,refs=4",
"org.openjdk.jmh:jmh-core:1.37" : "152d9f78,refs=3",
"org.openjdk.jmh:jmh-generator-annprocess:1.37" : "ecaf1d73,refs=1",
- "org.ow2.asm:asm:9.8" : "6fbc4021,refs=5",
- "org.ow2.asm:asm-commons:9.8" : "6fbc4021,refs=5",
- "org.ow2.asm:asm-tree:9.8" : "6fbc4021,refs=5",
"org.pcollections:pcollections:4.0.1" : "6897bc09,refs=38",
"org.slf4j:slf4j-api:2.0.17" : "b91715f0,refs=6",
"ua.net.nlp:morfologik-ukrainian-search:4.9.1" : "cb00cecf,refs=5",