From c2dc8221cb6bab5a9d1a51123112dbaeecf3388c Mon Sep 17 00:00:00 2001 From: lvca Date: Sun, 16 Jun 2024 02:17:32 -0400 Subject: [PATCH] fix: javascript returning objects now are unwrapped in simple Java object --- .../JavascriptFunctionDefinition.java | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/engine/src/main/java/com/arcadedb/function/polyglot/JavascriptFunctionDefinition.java b/engine/src/main/java/com/arcadedb/function/polyglot/JavascriptFunctionDefinition.java index 34fe01c7e..fac01b41b 100644 --- a/engine/src/main/java/com/arcadedb/function/polyglot/JavascriptFunctionDefinition.java +++ b/engine/src/main/java/com/arcadedb/function/polyglot/JavascriptFunctionDefinition.java @@ -18,6 +18,7 @@ import org.graalvm.polyglot.Value; import java.io.*; +import java.util.*; /** * Javascript implementation of a function. To define the function, pass the function name, code and optional parameters in the constructor. @@ -104,9 +105,21 @@ public Object execute(final Object... parameters) { public static Object getValue(final Value result) { if (result == null) return null; - else if (result.isHostObject()) - return result.asHostObject(); - else if (result.isString()) + else if (result.isHostObject()) { + Object v = result.asHostObject(); + if (v instanceof Value) + v = getValue((Value) v); + else if (v instanceof List) { + for (int i = 0; i < ((List) v).size(); ++i) { + Object elem = ((List) v).get(i); + if (elem instanceof Value) + ((List) v).set(i, getValue((Value) elem)); + else if (elem instanceof Map && !(elem instanceof HashMap)) + ((List) v).set(i, new HashMap((Map) elem)); + } + } + return v; + } else if (result.isString()) return result.asString(); else if (result.isBoolean()) return result.asBoolean(); @@ -119,10 +132,20 @@ else if (result.fitsInFloat()) return result.asFloat(); else return result.asDouble(); + } else if (result.hasArrayElements()) { + final long size = result.getArraySize(); + final List array = new ArrayList<>(); + for (int i = 0; i < size; i++) { + final Object elem = getValue(result.getArrayElement(i)); + array.add(elem); + } + return array; } else if (result.isNull()) return null; - - // UNKNOWN OR NOT SUPPORTED - return null; + else if (result.hasMembers()) + return new HashMap<>(result.as(Map.class)); + else + // UNKNOWN OR NOT SUPPORTED + return null; } }