Class JavascriptCompiler
Example:
Expression foo = JavascriptCompiler.compile("((0.3*popularity)/10.0)+(0.7*score)");
See the package documentation for the supported
syntax and default functions.
You can compile with an alternate set of functions via compile(String, Map). For
example:
Map<String,MethodHandle> functions = new HashMap<>();
// add all the default functions
functions.putAll(JavascriptCompiler.DEFAULT_FUNCTIONS);
// add cbrt()
functions.put("cbrt", MethodHandles.publicLookup().findStatic(Math.class, "cbrt",
MethodType.methodType(double.class, double.class)));
// call compile with customized function map
Expression foo = JavascriptCompiler.compile("cbrt(score)+ln(popularity)",
functions);
It is possible to pass any MethodHandle as function that only takes double
parameters and returns a double. The method does not need to be public, it just needs to
be resolved correctly using a private MethodHandles.Lookup instance. Ideally the methods should be
static, but you can use MethodHandle.bindTo(Object) to bind it to a receiver.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intprivate static final Stringprivate static final Stringstatic final Map<String, MethodHandle> The default set of functions available to expressions.private static final org.objectweb.asm.commons.Methodprivate static final org.objectweb.asm.Handleprivate static final org.objectweb.asm.Type[]private static final org.objectweb.asm.commons.Methodprivate static final org.objectweb.asm.commons.Methodprivate static final org.objectweb.asm.Typeprivate static final org.objectweb.asm.Type(package private) final Map<String, MethodHandle> private static final org.objectweb.asm.Typeprivate static final MethodHandles.Lookupprivate static final org.objectweb.asm.Typeprivate static final MethodTypeprivate static final org.objectweb.asm.commons.Method(package private) final boolean(package private) final Stringprivate static final org.objectweb.asm.Type -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateJavascriptCompiler(String sourceText, Map<String, MethodHandle> functions, boolean picky) Constructs a compiler for expressions with specific set of functions -
Method Summary
Modifier and TypeMethodDescriptionprivate static voidcheckFunction(MethodHandle method) Check Method signature for compatibility.static ExpressionCompiles the given expression using default compiler settings.static Expressioncompile(String sourceText, Map<String, MethodHandle> functions) Compiles the given expression with the supplied custom functions using default compiler settings.(package private) static Expressioncompile(String sourceText, Map<String, MethodHandle> functions, boolean picky) Compiles the given expression with the supplied custom functions.private ExpressionCompiles the given expression as hidden class.static Map<String, MethodHandle> convertLegacyFunctions(Map<String, Method> functions) Deprecated.Only use this to convert Lucene 9.x or earlier legacy code.(package private) static MethodHandledynamicConstantBootstrap(MethodHandles.Lookup lookup, String constantName, Class<?> type, String functionName) Bootstrap method for dynamic constants.(package private) static intfindSingleQuoteStringEnd(String text, int start) private voidgenerateClass(org.antlr.v4.runtime.tree.ParseTree parseTree, org.objectweb.asm.ClassWriter classWriter, Map<String, Integer> externalsMap) Sends the bytecode of class file toClassWriter.private org.antlr.v4.runtime.tree.ParseTreeParses the sourceText into an ANTLR 4 parse treeprivate static org.objectweb.asm.commons.MethodgetAsmMethod(Class<?> rtype, String name, Class<?>... ptypes) create an ASM Method object from return type, method name, and parameters.private ExpressioninvokeConstructor(MethodHandles.Lookup lookup, Class<?> expressionClass, Map<String, Integer> externalsMap) private static Map<String, MethodHandle> (package private) static StringnormalizeQuotes(String text) (package private) static ThrowablepatchStackTrace(Throwable t, Expression impl) Method called from try/catch handler in compiled expression.private voidsetupPicky(JavascriptParser parser)
-
Field Details
-
LOOKUP
-
CLASSFILE_VERSION
private static final int CLASSFILE_VERSION- See Also:
-
MT_EXPRESSION_CTOR_LOOKUP
-
COMPILED_EXPRESSION_CLASS
-
COMPILED_EXPRESSION_INTERNAL
-
EXPRESSION_TYPE
private static final org.objectweb.asm.Type EXPRESSION_TYPE -
FUNCTION_VALUES_TYPE
private static final org.objectweb.asm.Type FUNCTION_VALUES_TYPE -
METHOD_HANDLE_TYPE
private static final org.objectweb.asm.Type METHOD_HANDLE_TYPE -
JAVASCRIPT_COMPILER_TYPE
private static final org.objectweb.asm.Type JAVASCRIPT_COMPILER_TYPE -
THROWABLE_TYPE
private static final org.objectweb.asm.Type THROWABLE_TYPE -
EXPRESSION_CTOR
private static final org.objectweb.asm.commons.Method EXPRESSION_CTOR -
EVALUATE_METHOD
private static final org.objectweb.asm.commons.Method EVALUATE_METHOD -
DOUBLE_VAL_METHOD
private static final org.objectweb.asm.commons.Method DOUBLE_VAL_METHOD -
PATCH_STACK_METHOD
private static final org.objectweb.asm.commons.Method PATCH_STACK_METHOD -
EVALUATE_EXCEPTIONS
private static final org.objectweb.asm.Type[] EVALUATE_EXCEPTIONS -
DYNAMIC_CONSTANT_BOOTSTRAP_HANDLE
private static final org.objectweb.asm.Handle DYNAMIC_CONSTANT_BOOTSTRAP_HANDLE -
sourceText
-
functions
-
picky
final boolean picky -
DEFAULT_FUNCTIONS
The default set of functions available to expressions.See the
package documentationfor a list.
-
-
Constructor Details
-
JavascriptCompiler
Constructs a compiler for expressions with specific set of functions- Parameters:
sourceText- The expression to compile
-
-
Method Details
-
getAsmMethod
private static org.objectweb.asm.commons.Method getAsmMethod(Class<?> rtype, String name, Class<?>... ptypes) create an ASM Method object from return type, method name, and parameters. -
compile
Compiles the given expression using default compiler settings.- Parameters:
sourceText- The expression to compile- Returns:
- A new compiled expression
- Throws:
ParseException- on failure to compile
-
compile
public static Expression compile(String sourceText, Map<String, MethodHandle> functions) throws ParseExceptionCompiles the given expression with the supplied custom functions using default compiler settings.Functions must be
public static, returndoubleand can take from zero to 256doubleparameters.- Parameters:
sourceText- The expression to compilefunctions- map of String names toMethodHandles- Returns:
- A new compiled expression
- Throws:
ParseException- on failure to compile
-
convertLegacyFunctions
@Deprecated public static Map<String,MethodHandle> convertLegacyFunctions(Map<String, Method> functions) throws IllegalAccessExceptionDeprecated.Only use this to convert Lucene 9.x or earlier legacy code. For new code useMethodHandle.Converts a legacy map with reflectiveMethodfunctions toMap<String,MethodHandlefor use withcompile(String, Map).- Parameters:
functions- a map with only public and accessible reflective methods- Returns:
- a new (modifiable) map with the same function declarations, but converted to
MethodHandle - Throws:
IllegalAccessException- if any of the methods infunctionsare not accessible by the publicMethodHandles.Lookup.
-
compile
static Expression compile(String sourceText, Map<String, MethodHandle> functions, boolean picky) throws ParseExceptionCompiles the given expression with the supplied custom functions.Functions must be
public static, returndoubleand can take from zero to 256doubleparameters.- Parameters:
sourceText- The expression to compilefunctions- map of String names toMethodHandlespicky- whether to throw exception on ambiguity or other internal parsing issues (this option makes things slower too, it is only for debugging).- Returns:
- A new compiled expression
- Throws:
ParseException- on failure to compile
-
compileExpression
Compiles the given expression as hidden class.- Returns:
- A new compiled expression
- Throws:
ParseException- on failure to compile
-
invokeConstructor
private Expression invokeConstructor(MethodHandles.Lookup lookup, Class<?> expressionClass, Map<String, Integer> externalsMap) throws ReflectiveOperationException- Throws:
ReflectiveOperationException
-
getAntlrParseTree
Parses the sourceText into an ANTLR 4 parse tree- Returns:
- The ANTLR parse tree
- Throws:
ParseException- on failure to parse
-
setupPicky
-
generateClass
private void generateClass(org.antlr.v4.runtime.tree.ParseTree parseTree, org.objectweb.asm.ClassWriter classWriter, Map<String, Integer> externalsMap) throws ParseExceptionSends the bytecode of class file toClassWriter.- Throws:
ParseException
-
normalizeQuotes
-
findSingleQuoteStringEnd
-
loadDefaultFunctions
-
checkFunction
Check Method signature for compatibility. -
dynamicConstantBootstrap
static MethodHandle dynamicConstantBootstrap(MethodHandles.Lookup lookup, String constantName, Class<?> type, String functionName) throws IllegalAccessException Bootstrap method for dynamic constants. This returns aMethodHandlefor thefunctionNamefrom the class data passed viaMethodHandles.Lookup.defineHiddenClassWithClassData(byte[], java.lang.Object, boolean, java.lang.invoke.MethodHandles.Lookup.ClassOption...). TheconstantNameis ignored.- Throws:
IllegalAccessException
-
patchStackTrace
Method called from try/catch handler in compiled expression. This patches the stack trace and adds back a hidden frame (including the source code of script as filename).
-