From eba4f35265e4c74d007f62e9fd8d31038ffcbda7 Mon Sep 17 00:00:00 2001 From: 0007 <0007@qq.com> Date: Wed, 27 Aug 2025 19:58:59 +0800 Subject: [PATCH] Add File --- .../core/util/graalvm/JsInteropUtils.java | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 agents-flex-core/src/main/java/com/agentsflex/core/util/graalvm/JsInteropUtils.java diff --git a/agents-flex-core/src/main/java/com/agentsflex/core/util/graalvm/JsInteropUtils.java b/agents-flex-core/src/main/java/com/agentsflex/core/util/graalvm/JsInteropUtils.java new file mode 100644 index 0000000..7347f23 --- /dev/null +++ b/agents-flex-core/src/main/java/com/agentsflex/core/util/graalvm/JsInteropUtils.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2023-2025, Agents-Flex (fuhai999@gmail.com). + *
+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.apache.org/licenses/LICENSE-2.0 + *
+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.agentsflex.core.util.graalvm; + +import org.graalvm.polyglot.Context; +import org.graalvm.polyglot.Value; + +import java.time.*; +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class JsInteropUtils { + + public static Value wrapJavaValueForJS(Context context, Object value) { + if (value == null) { + return context.asValue(null); + } + + // 处理 LocalDateTime / LocalDate / ZonedDateTime -> JS Date + if (value instanceof LocalDateTime) { + return context.eval("js", "new Date('" + ((LocalDateTime) value).atZone(ZoneId.systemDefault()) + "')"); + } + if (value instanceof LocalDate) { + return context.eval("js", "new Date('" + ((LocalDate) value).atStartOfDay(ZoneId.systemDefault()) + "')"); + } + if (value instanceof ZonedDateTime) { + return context.eval("js", "new Date('" + (value) + "')"); + } + if (value instanceof Date) { + return context.eval("js", "new Date(" + ((Date) value).getTime() + ")"); + } + + // 处理 Map -> ProxyObject + if (value instanceof Map) { + return context.asValue(new ProxyMap((Map) value, context)); + } + + // 处理 List -> ProxyArray + if (value instanceof List) { + return context.asValue(new ProxyList((List) value, context)); + } + + // 处理 Set -> ProxyArray + if (value instanceof Set) { + return context.asValue(new ProxyList(new ArrayList<>((Set) value), context)); + } + + // 处理数组 -> ProxyArray + if (value.getClass().isArray()) { + int length = java.lang.reflect.Array.getLength(value); + List