package com.oracle.truffle.polyglot;
import java.util.Collection;
import java.util.Objects;
import org.graalvm.collections.EconomicSet;
import org.graalvm.collections.Pair;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Cached.Shared;
import com.oracle.truffle.api.interop.ArityException;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnknownIdentifierException;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.interop.UnsupportedTypeException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
@ExportLibrary(InteropLibrary.class)
final class HostAdapterSuperMembers implements TruffleObject {
final HostObject adapter;
HostAdapterSuperMembers(HostObject adapter) {
this.adapter = Objects.requireNonNull(adapter);
}
public Object getAdapter() {
return adapter;
}
static final class NameCache {
@CompilationFinal private Pair<String, String> cachedNameToSuper;
private static final NameCache UNCACHED = new NameCache(true);
NameCache() {
}
NameCache(boolean uncached) {
if (uncached) {
this.cachedNameToSuper = Pair.empty();
}
}
String getSuperMethodName(String name) {
if (cachedNameToSuper == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
cachedNameToSuper = Pair.create(name, HostAdapterFactory.getSuperMethodName(name));
}
String cachedName = cachedNameToSuper.getLeft();
if (cachedName != null) {
if (cachedName.equals(name)) {
return cachedNameToSuper.getRight();
} else {
CompilerDirectives.transferToInterpreterAndInvalidate();
cachedNameToSuper = Pair.empty();
return HostAdapterFactory.getSuperMethodName(name);
}
} else {
return HostAdapterFactory.getSuperMethodName(name);
}
}
static NameCache create() {
return new NameCache();
}
static NameCache getUncached() {
return UNCACHED;
}
}
@SuppressWarnings("static-method")
@ExportMessage
boolean hasMembers() {
return true;
}
@ExportMessage
Object readMember(String name,
@Shared("cache") @Cached NameCache cache,
@CachedLibrary("this.adapter") InteropLibrary interop) throws UnsupportedMessageException, UnknownIdentifierException {
String superMethodName = cache.getSuperMethodName(name);
return interop.readMember(getAdapter(), superMethodName);
}
@ExportMessage
Object invokeMember(String name, Object[] args,
@Shared("cache") @Cached NameCache cache,
@CachedLibrary("this.adapter") InteropLibrary interop) throws UnsupportedMessageException, ArityException, UnknownIdentifierException, UnsupportedTypeException {
String superMethodName = cache.getSuperMethodName(name);
return interop.invokeMember(getAdapter(), superMethodName, args);
}
@ExportMessage
boolean isMemberReadable(String name,
@Shared("cache") @Cached NameCache cache,
@CachedLibrary("this.adapter") InteropLibrary interop) {
String superMethodName = cache.getSuperMethodName(name);
return interop.isMemberReadable(getAdapter(), superMethodName);
}
@ExportMessage
boolean isMemberInvocable(String name,
@Shared("cache") @Cached NameCache cache,
@CachedLibrary("this.adapter") InteropLibrary interop) {
String superMethodName = cache.getSuperMethodName(name);
return interop.isMemberInvocable(getAdapter(), superMethodName);
}
@ExportMessage
@TruffleBoundary
Object getMembers(boolean includeInternal) {
return new HostObject.KeysArray(includeInternal ? collectSuperMembers() : new String[0]);
}
@TruffleBoundary
private String[] collectSuperMembers() {
HostClassDesc classDesc = HostClassDesc.forClass(this.adapter.getEngine(), this.adapter.getLookupClass());
EconomicSet<String> names = EconomicSet.create();
Collection<String> methodNames = classDesc.getMethodNames(false, true);
for (String name : methodNames) {
if (name.startsWith(HostAdapterBytecodeGenerator.SUPER_PREFIX)) {
names.add(name.substring(HostAdapterBytecodeGenerator.SUPER_PREFIX.length()));
}
}
return names.toArray(new String[names.size()]);
}
}