Re: Expressions module, support of Strings

Uwe Schindler <[email protected]> Wed, 7 May 2025 22:42:23 +0200
Newsgroups gmane.comp.jakarta.lucene.user
Message-ID <[email protected]>
Hi,

the expressions module is just made for calculating scores, therefore 
theres no need to call any function which takes strings. Also string 
bindings are not consumed, it only supports DoubleValues as function 
variables.

If you want a full scripting language, check Elasticsearch Expressions 
(where I was also involved in development, especially the support for 
lambdas and all kinds of dynnamic types). If you look at that code it is 
a magnitude larger and more complex. This comes from the fact that 
everything which is not a number in Java is a reference and therefor you 
need invokedynamic to hell because you must be able to call methods on 
object instances like String.

So I'd really say: expressions with reference types is a complete 
rewrite, it could be a separate project, but rewriting a fully featured 
Javascript compiler is slow on execution and has security risks. It was 
already implemented by others. Making a more static language with 
perfect support for safety and arbitrary invoke support based on 
invokedyanmic and/or dynamic constants (like the Elasticsearch Painless 
engine) is a very hard task. It took us months to implement it for 
Elasticsearch in 2017. The ongoing port to Java 24 classfile library and 
dynamic constants and hidden classes with classdata (Java 16) is still 
not done (as far as I remember).

So I'd argue to keep expressions as a language to express score 
canculations (plain function queries).

Small tip: If you want to provide a function to the engine that has a 
fixed static "String" parameter or is bound to a fixed instance (because 
it's virtual), you can register it as MethodHandle while binding it to 
the String or another object instance, e.g., like a script function that 
calls System.out.println(double) while returning Double.NaN as fake 
return type:

functions.put("print", MethodHandles.filterReturnValue(lookup.findVirtual(PrintStream.class, "println",
   methodType(void.class, double.class)), MethodHandles.constant(double.class, Double.NaN)).bindTo(System.out));

(I used this during debugging of expressions, mine was a bit of more 
complex as it returned the double parameter itsself).

Uwe

Am 07.05.2025 um 20:55 schrieb David Smiley:
> I've been looking at the Expressions module, a really impressive piece of
> work!
>
> The "JavaScript" sub-package only appears to support an extreme subset of
> JavaScript, one where the only data type is a double.  I don't see that it
> supports Strings/text, to include passing Strings to methods and resolving
> String data from a binding.  Can someone confirm this?  What would it take
> to expand that?
>
> I can see that Javascript.g4 (Antlr grammar) references a "STRING" but
> can't make out if/how it's used in JavaScriptCompiler.
>
> ~ David Smiley
> Apache Lucene/Solr Search Developer
> http://www.linkedin.com/in/davidwsmiley
>
-- 
Uwe Schindler
Achterdiek 19, D-28357 Bremen
https://www.thetaphi.de
eMail:[email protected]