Skip to content

Commit

Permalink
Handle serialization of JAVA arrays in GraalJS execution
Browse files Browse the repository at this point in the history
  • Loading branch information
shanggeeth committed May 20, 2024
1 parent 478306c commit ba98971
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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() + ")");
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ba98971

Please sign in to comment.