Skip to content

Commit a8bb871

Browse files
bjfisheregon
authored andcommitted
Implement hash interop methods for Truffle::Interop
1 parent 6158c04 commit a8bb871

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed

src/main/java/org/truffleruby/interop/InteropNodes.java

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import com.oracle.truffle.api.frame.VirtualFrame;
2222
import com.oracle.truffle.api.interop.ExceptionType;
2323
import com.oracle.truffle.api.interop.NodeLibrary;
24+
import com.oracle.truffle.api.interop.UnknownKeyException;
25+
import com.oracle.truffle.api.interop.UnsupportedTypeException;
2426
import org.jcodings.specific.UTF8Encoding;
2527
import org.truffleruby.RubyContext;
2628
import org.truffleruby.RubyLanguage;
@@ -1998,6 +2000,198 @@ protected Object getMetaQualifiedName(Object metaObject,
19982000
}
19992001
// endregion
20002002

2003+
// region Hash
2004+
2005+
@CoreMethod(names = "has_hash_entries?", onSingleton = true, required = 1)
2006+
public abstract static class HasHashEntriesNode extends InteropCoreMethodArrayArgumentsNode {
2007+
@Specialization(limit = "getCacheLimit()")
2008+
protected boolean hasHashEntriesNode(Object receiver,
2009+
@CachedLibrary("receiver") InteropLibrary interop) {
2010+
return interop.hasHashEntries(receiver);
2011+
}
2012+
}
2013+
2014+
2015+
@CoreMethod(names = "hash_entries_iterator", onSingleton = true, required = 1)
2016+
public abstract static class HashEntriesIteratorNode extends InteropCoreMethodArrayArgumentsNode {
2017+
@Specialization(limit = "getCacheLimit()")
2018+
protected Object hashEntriesIterator(Object receiver,
2019+
@CachedLibrary("receiver") InteropLibrary interop,
2020+
@Cached TranslateInteropExceptionNode translateInteropException) {
2021+
try {
2022+
return interop.getHashEntriesIterator(receiver);
2023+
} catch (UnsupportedMessageException e) {
2024+
throw translateInteropException.execute(e);
2025+
}
2026+
}
2027+
}
2028+
2029+
@CoreMethod(names = "hash_entry_existing?", onSingleton = true, required = 2)
2030+
public abstract static class HashEntryExistingNode extends InteropCoreMethodArrayArgumentsNode {
2031+
@Specialization(limit = "getCacheLimit()")
2032+
protected boolean hashEntryExisting(Object receiver, Object key,
2033+
@CachedLibrary("receiver") InteropLibrary interop) {
2034+
return interop.isHashEntryExisting(receiver, key);
2035+
}
2036+
}
2037+
2038+
@CoreMethod(names = "hash_entry_insertable?", onSingleton = true, required = 2)
2039+
public abstract static class HashEntryInsertableNode extends InteropCoreMethodArrayArgumentsNode {
2040+
@Specialization(limit = "getCacheLimit()")
2041+
protected boolean hashEntryInsertable(Object receiver, Object key,
2042+
@CachedLibrary("receiver") InteropLibrary interop) {
2043+
return interop.isHashEntryInsertable(receiver, key);
2044+
}
2045+
}
2046+
2047+
@CoreMethod(names = "hash_entry_modifiable?", onSingleton = true, required = 2)
2048+
public abstract static class HashEntryModifiableNode extends InteropCoreMethodArrayArgumentsNode {
2049+
@Specialization(limit = "getCacheLimit()")
2050+
protected boolean hashEntryModifiable(Object receiver, Object key,
2051+
@CachedLibrary("receiver") InteropLibrary interop) {
2052+
return interop.isHashEntryModifiable(receiver, key);
2053+
}
2054+
}
2055+
2056+
@CoreMethod(names = "hash_entry_readable?", onSingleton = true, required = 2)
2057+
public abstract static class HashEntryReadableNode extends InteropCoreMethodArrayArgumentsNode {
2058+
@Specialization(limit = "getCacheLimit()")
2059+
protected boolean hashEntryReadable(Object receiver, Object key,
2060+
@CachedLibrary("receiver") InteropLibrary interop) {
2061+
return interop.isHashEntryReadable(receiver, key);
2062+
}
2063+
}
2064+
2065+
@CoreMethod(names = "hash_entry_removable?", onSingleton = true, required = 2)
2066+
public abstract static class HashEntryRemovableNode extends InteropCoreMethodArrayArgumentsNode {
2067+
@Specialization(limit = "getCacheLimit()")
2068+
protected boolean hashEntryRemovable(Object receiver, Object key,
2069+
@CachedLibrary("receiver") InteropLibrary interop) {
2070+
return interop.isHashEntryRemovable(receiver, key);
2071+
}
2072+
}
2073+
2074+
2075+
@CoreMethod(names = "hash_entry_writable?", onSingleton = true, required = 2)
2076+
public abstract static class HashEntryWritableNode extends InteropCoreMethodArrayArgumentsNode {
2077+
@Specialization(limit = "getCacheLimit()")
2078+
protected boolean hashEntryWritable(Object receiver, Object key,
2079+
@CachedLibrary("receiver") InteropLibrary interop) {
2080+
return interop.isHashEntryWritable(receiver, key);
2081+
}
2082+
}
2083+
2084+
@CoreMethod(names = "hash_keys_iterator", onSingleton = true, required = 1)
2085+
public abstract static class HashKeysIteratorNode extends InteropCoreMethodArrayArgumentsNode {
2086+
@Specialization(limit = "getCacheLimit()")
2087+
protected Object hashKeysIterator(Object receiver,
2088+
@CachedLibrary("receiver") InteropLibrary interop,
2089+
@Cached TranslateInteropExceptionNode translateInteropException) {
2090+
try {
2091+
return interop.getHashKeysIterator(receiver);
2092+
} catch (UnsupportedMessageException e) {
2093+
throw translateInteropException.execute(e);
2094+
}
2095+
}
2096+
}
2097+
2098+
@CoreMethod(names = "hash_size", onSingleton = true, required = 1)
2099+
public abstract static class HashSizeNode extends InteropCoreMethodArrayArgumentsNode {
2100+
@Specialization(limit = "getCacheLimit()")
2101+
protected long hashSize(Object receiver,
2102+
@CachedLibrary("receiver") InteropLibrary interop,
2103+
@Cached TranslateInteropExceptionNode translateInteropException) {
2104+
try {
2105+
return interop.getHashSize(receiver);
2106+
} catch (UnsupportedMessageException e) {
2107+
throw translateInteropException.execute(e);
2108+
}
2109+
}
2110+
}
2111+
2112+
@CoreMethod(names = "hash_values_iterator", onSingleton = true, required = 1)
2113+
public abstract static class HashValuesIteratorNode extends InteropCoreMethodArrayArgumentsNode {
2114+
@Specialization(limit = "getCacheLimit()")
2115+
protected Object hashValuesIterator(Object receiver,
2116+
@CachedLibrary("receiver") InteropLibrary interop,
2117+
@Cached TranslateInteropExceptionNode translateInteropException) {
2118+
try {
2119+
return interop.getHashValuesIterator(receiver);
2120+
} catch (UnsupportedMessageException e) {
2121+
throw translateInteropException.execute(e);
2122+
}
2123+
}
2124+
}
2125+
2126+
2127+
@CoreMethod(names = "read_hash_value", onSingleton = true, required = 2)
2128+
public abstract static class ReadHashValueNode extends InteropCoreMethodArrayArgumentsNode {
2129+
@Specialization(limit = "getCacheLimit()")
2130+
protected Object readHashValue(Object receiver, Object key,
2131+
@CachedLibrary("receiver") InteropLibrary interop,
2132+
@Cached TranslateInteropExceptionNode translateInteropException) {
2133+
try {
2134+
return interop.readHashValue(receiver, key);
2135+
} catch (UnsupportedMessageException e) {
2136+
throw translateInteropException.execute(e);
2137+
} catch (UnknownKeyException e) {
2138+
return nil;
2139+
}
2140+
}
2141+
}
2142+
2143+
@CoreMethod(names = "read_hash_value_or_default", onSingleton = true, required = 3)
2144+
public abstract static class ReadHashValueOrDefaultNode extends InteropCoreMethodArrayArgumentsNode {
2145+
@Specialization(limit = "getCacheLimit()")
2146+
protected Object readHashValueOrDefault(Object receiver, Object key, Object defaultValue,
2147+
@CachedLibrary("receiver") InteropLibrary interop,
2148+
@Cached TranslateInteropExceptionNode translateInteropException) {
2149+
try {
2150+
return interop.readHashValueOrDefault(receiver, key, defaultValue);
2151+
} catch (UnsupportedMessageException e) {
2152+
throw translateInteropException.execute(e);
2153+
}
2154+
}
2155+
}
2156+
2157+
@CoreMethod(names = "remove_hash_entry", onSingleton = true, required = 2)
2158+
public abstract static class RemoveHashEntryNode extends InteropCoreMethodArrayArgumentsNode {
2159+
@Specialization(limit = "getCacheLimit()")
2160+
protected Object removeHashEntry(Object receiver, Object key,
2161+
@CachedLibrary("receiver") InteropLibrary interop,
2162+
@Cached TranslateInteropExceptionNode translateInteropException) {
2163+
try {
2164+
interop.removeHashEntry(receiver, key);
2165+
return nil;
2166+
} catch (UnsupportedMessageException e) {
2167+
throw translateInteropException.execute(e);
2168+
} catch (UnknownKeyException e) {
2169+
return nil;
2170+
}
2171+
}
2172+
}
2173+
2174+
@CoreMethod(names = "write_hash_entry", onSingleton = true, required = 3)
2175+
public abstract static class WriteHashEntryNode extends InteropCoreMethodArrayArgumentsNode {
2176+
@Specialization(limit = "getCacheLimit()")
2177+
protected Object writeHashEntry(Object receiver, Object key, Object value,
2178+
@CachedLibrary("receiver") InteropLibrary interop,
2179+
@Cached TranslateInteropExceptionNode translateInteropException) {
2180+
try {
2181+
interop.writeHashEntry(receiver, key, value);
2182+
return nil;
2183+
} catch (UnsupportedMessageException e) {
2184+
throw translateInteropException.execute(e);
2185+
} catch (UnknownKeyException e) {
2186+
return nil;
2187+
} catch (UnsupportedTypeException e) {
2188+
throw translateInteropException.execute(e);
2189+
}
2190+
}
2191+
}
2192+
2193+
// endregion
2194+
20012195
// region Identity
20022196
@CoreMethod(names = "identical?", onSingleton = true, required = 2)
20032197
public abstract static class IsIdenticalNode extends InteropCoreMethodArrayArgumentsNode {

0 commit comments

Comments
 (0)