From ba98971aad902f2b8d1494e6942f6bd8b5478522 Mon Sep 17 00:00:00 2001 From: shanggeeth Date: Mon, 20 May 2024 05:52:50 +0530 Subject: [PATCH] Handle serialization of JAVA arrays in GraalJS execution --- .../model/graph/graaljs/GraalSerializer.java | 18 +++++++++++++++--- .../graaljs/JsGraalGraphBuilderFactory.java | 5 ++--- .../framework/util/FrameworkUtils.java | 2 ++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/graaljs/GraalSerializer.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/graaljs/GraalSerializer.java index c15bd311f2ca..8f147bb37fd7 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/graaljs/GraalSerializer.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/graaljs/GraalSerializer.java @@ -75,7 +75,9 @@ public static Object toJsSerializableInternal(Object value) { return list; } else if (value instanceof Value) { Value valueObj = (Value) value; - if (valueObj.canExecute()) { + if (valueObj.isHostObject()) { + return valueObj.asHostObject(); + } else if (valueObj.canExecute()) { return GraalSerializableJsFunction.toSerializableForm(valueObj); } else if (valueObj.isProxyObject()) { return valueObj.asProxyObject(); @@ -133,8 +135,9 @@ public Object fromJsSerializable(Object value, Context engine) throws FrameworkE } public static Object fromJsSerializableInternal(Object value, Context context) throws FrameworkException { - - if (value instanceof GraalSerializableJsFunction) { + if (value == null) { + return null; + } else if (value instanceof GraalSerializableJsFunction) { GraalSerializableJsFunction serializableJsFunction = (GraalSerializableJsFunction) value; try { return context.eval("js", "(" + serializableJsFunction.getSource() + ")"); @@ -157,6 +160,15 @@ public static Object fromJsSerializableInternal(Object value, Context context) t deserializedValue.setArrayElement(index, deserializedObject); } return deserializedValue; + } else if (value.getClass().isArray()) { + Value deserializedValue = context.eval(POLYGLOT_LANGUAGE, "[]"); + int arraySize = java.lang.reflect.Array.getLength(value); + for (int index = 0; index < arraySize; index++) { + Object deserializedObject = + fromJsSerializableInternal(java.lang.reflect.Array.get(value, index), context); + deserializedValue.setArrayElement(index, deserializedObject); + } + return deserializedValue; } return value; } diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/graaljs/JsGraalGraphBuilderFactory.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/graaljs/JsGraalGraphBuilderFactory.java index 6f52580394af..922657b1dea1 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/graaljs/JsGraalGraphBuilderFactory.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/graaljs/JsGraalGraphBuilderFactory.java @@ -94,10 +94,9 @@ public static void persistCurrentContext(AuthenticationContext authContext, Cont * Since, we don't have a difference between global and engine scopes, we need to identify what are the * custom functions and the logger object we added bindings to, and not persist them since we will anyways * bind them again. - * The functions will be host objects and can be executed. The logger object will be host object and will - * not have any array elements. + * The functions will be host objects and can be executed. The Logger object needs to be identified by name. */ - if (!(binding.isHostObject() && (binding.canExecute() || !binding.hasArrayElements()))) { + if (!((binding.isHostObject() && binding.canExecute()) || key.equals("Log"))) { persistableMap.put(key, GraalSerializer.getInstance().toJsSerializable(binding)); } }); diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/util/FrameworkUtils.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/util/FrameworkUtils.java index 1d17a6628027..ad5da87a1d1c 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/util/FrameworkUtils.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/util/FrameworkUtils.java @@ -4020,6 +4020,8 @@ public static JsGenericGraphBuilderFactory createJsGenericGraphBuilderFactoryFro return new JsGraalGraphBuilderFactory(); } else if (StringUtils.equalsIgnoreCase(FrameworkConstants.OPENJDK_NASHORN, scriptEngineName)) { return new JsOpenJdkNashornGraphBuilderFactory(); + } else if (StringUtils.equalsIgnoreCase(FrameworkConstants.NASHORN, scriptEngineName)) { + return new JsGraphBuilderFactory(); } } // Config is not set. Hence going with class for name approach.