Croway commented on code in PR #26310:
URL: https://github.com/apache/camel/pull/26310#discussion_r3990431965


##########
components/camel-javascript/src/main/java/org/apache/camel/language/js/JavaScriptLanguage.java:
##########
@@ -16,21 +16,62 @@
  */
 package org.apache.camel.language.js;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
 import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
 
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
+import org.apache.camel.Service;
 import org.apache.camel.spi.ScriptingLanguage;
 import org.apache.camel.spi.annotations.Language;
+import org.apache.camel.support.LRUCacheFactory;
 import org.apache.camel.support.TypedLanguageSupport;
 import org.graalvm.polyglot.Context;
+import org.graalvm.polyglot.Engine;
 import org.graalvm.polyglot.Source;
 import org.graalvm.polyglot.Value;
 
-import static org.graalvm.polyglot.Source.newBuilder;
-
+/**
+ * Camel expression language for JavaScript via <a 
href="https://www.graalvm.org/javascript/";>GraalJS</a>.
+ * <p>
+ * One {@link Engine} is shared by all evaluations of a language instance so 
parsed and compiled scripts are reused; a
+ * fresh {@link Context} is still created per evaluation so scripts stay 
isolated from each other. The engine is created

Review Comment:
   Applied in 50f185334d07: kept the eager build in start() (paying the engine 
cost at route startup is the intent) and reworded the javadoc to say so, with 
the on-demand build noted for expressions used before start.
   
   _Claude Code on behalf of Croway_



##########
components/camel-javascript/src/main/java/org/apache/camel/language/js/JavaScriptLanguage.java:
##########
@@ -45,14 +86,140 @@ public Expression createExpression(String expression) {
     @Override
     public <T> T evaluate(String script, Map<String, Object> bindings, 
Class<T> resultType) {
         script = loadResource(script);
-        try (Context cx = JavaScriptHelper.newContext()) {
-            Value b = cx.getBindings("js");
-            bindings.forEach(b::putMember);
-            Source source = newBuilder("js", script, "Unnamed")
-                    .mimeType("application/javascript+module").buildLiteral();
-            Value o = cx.eval(source);
-            Object answer = o != null ? o.as(resultType) : null;
-            return resultType.cast(answer);
+        try (Context cx = newContext()) {
+            if (bindings != null) {
+                Value b = cx.getBindings("js");
+                bindings.forEach(b::putMember);
+            }
+            Value o = cx.eval(source(script));
+            Object answer = materialize(o);
+            if (answer == null || resultType == Object.class || 
resultType.isInstance(answer)) {
+                return resultType.cast(answer);
+            }
+            if (getCamelContext() != null) {
+                return 
getCamelContext().getTypeConverter().convertTo(resultType, answer);

Review Comment:
   Applied in 50f185334d07: mandatoryConvertTo, wrapped as a 
RuntimeCamelException, so an impossible conversion fails loudly as before.
   
   _Claude Code on behalf of Croway_



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to