All Downloads are FREE. Search and download functionalities are using the official Maven repository.

swim.vm.VmHostObject Maven / Gradle / Ivy

// Copyright 2015-2019 SWIM.AI inc.
//
// 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 swim.vm;

import java.util.Collection;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyObject;
import swim.dynamic.HostField;
import swim.dynamic.HostMember;
import swim.dynamic.HostMethod;
import swim.dynamic.HostObjectType;

public class VmHostObject extends VmHostProxy implements ProxyObject {
  final VmBridge bridge;
  final HostObjectType type;
  final T self;

  public VmHostObject(VmBridge bridge, HostObjectType type, T self) {
    this.bridge = bridge;
    this.type = type;
    this.self = self;
  }

  @Override
  public final T unwrap() {
    return this.self;
  }

  @Override
  public boolean hasMember(String key) {
    final HostMember member = this.type.getMember(this.bridge, this.self, key);
    return member != null;
  }

  @Override
  public Object getMember(String key) {
    final HostMember member = this.type.getMember(this.bridge, this.self, key);
    if (member instanceof HostField) {
      final Object hostValue = ((HostField) member).get(this.bridge, this.self);
      return this.bridge.hostToGuest(hostValue);
    } else if (member instanceof HostMethod) {
      return this.bridge.hostMethodToGuestMethod((HostMethod) member, this.self);
    } else {
      return null;
    }
  }

  @Override
  public void putMember(String key, Value guestValue) {
    final HostMember member = this.type.getMember(this.bridge, this.self, key);
    if (member instanceof HostField) {
      final Object hostValue = this.bridge.guestToHost(guestValue);
      ((HostField) member).set(this.bridge, this.self, hostValue);
    } else {
      throw new UnsupportedOperationException();
    }
  }

  @Override
  public boolean removeMember(String key) {
    final HostMember member = this.type.getMember(this.bridge, this.self, key);
    if (member instanceof HostField) {
      return ((HostField) member).remove(this.bridge, this.self);
    } else {
      throw new UnsupportedOperationException();
    }
  }

  @Override
  public Object getMemberKeys() {
    final Collection> members = this.type.members(this.bridge, this.self);
    final String[] memberKeys = new String[members.size()];
    int i = 0;
    for (HostMember member : members) {
      memberKeys[i] = member.key();
      i += 1;
    }
    return new VmProxyArray(memberKeys);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy