001/**
002 * Copyright (c) 2025-2026, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.gnu.org/licenses/lgpl-3.0.txt
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package dev.tinyflow.core.util.graalvm;
017
018import org.graalvm.polyglot.Context;
019import org.graalvm.polyglot.Value;
020import org.graalvm.polyglot.proxy.ProxyObject;
021
022import java.util.Map;
023import java.util.Set;
024import java.util.stream.Collectors;
025
026public class ProxyMap implements ProxyObject {
027    private final Map<Object, Object> map;
028    private final Context context;
029
030    public ProxyMap(Map<Object, Object> map, Context context) {
031        this.map = map;
032        this.context = context;
033    }
034
035    @Override
036    public Object getMember(String key) {
037        return JsInteropUtils.wrapJavaValueForJS(context, map.get(key)).as(Object.class);
038    }
039
040    @Override
041    public boolean hasMember(String key) {
042        return map.containsKey(key);
043    }
044
045    @Override
046    public Set<String> getMemberKeys() {
047        return map.keySet().stream()
048            .map(Object::toString)
049            .collect(Collectors.toSet());
050    }
051
052    @Override
053    public void putMember(String key, Value value) {
054        map.put(key, JsInteropUtils.unwrapJsValue(value));
055    }
056}