Class JavascriptCompiler

java.lang.Object
org.apache.lucene.expressions.js.JavascriptCompiler

public final class JavascriptCompiler extends Object
An expression compiler for javascript expressions.

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 Details

    • LOOKUP

      private static final MethodHandles.Lookup LOOKUP
    • CLASSFILE_VERSION

      private static final int CLASSFILE_VERSION
      See Also:
    • MT_EXPRESSION_CTOR_LOOKUP

      private static final MethodType MT_EXPRESSION_CTOR_LOOKUP
    • COMPILED_EXPRESSION_CLASS

      private static final String COMPILED_EXPRESSION_CLASS
    • COMPILED_EXPRESSION_INTERNAL

      private static final String 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

      final String sourceText
    • functions

      final Map<String,MethodHandle> functions
    • picky

      final boolean picky
    • DEFAULT_FUNCTIONS

      public static final Map<String,MethodHandle> DEFAULT_FUNCTIONS
      The default set of functions available to expressions.

      See the package documentation for a list.

  • Constructor Details

    • JavascriptCompiler

      private JavascriptCompiler(String sourceText, Map<String,MethodHandle> functions, boolean picky)
      Constructs a compiler for expressions with specific set of functions
      Parameters:
      sourceText - The expression to compile
  • Method Details