A multi-language framework for executing dynamic languages that achieves high performance when combined with Graal.
—
Advanced Truffle capabilities for sophisticated language implementations, including library-based dispatch systems, static object layouts, runtime utilities, and performance optimization infrastructure.
Framework for creating custom library-based dispatch systems with automatic caching and specialization.
/**
* Base class for creating custom libraries
*/
public abstract class Library extends Node {
/**
* Check if library accepts receiver type
* @param receiver Object to check
* @return true if accepts
*/
public boolean accepts(Object receiver) {
return true;
}
/**
* Get library priority for receiver
* @param receiver Object to check
* @return Priority value (higher = preferred)
*/
public int getPriority() {
return 0;
}
}
/**
* Factory for creating library instances
* @param <T> Library type
*/
public abstract class LibraryFactory<T extends Library> {
/**
* Get factory for library class
* @param libraryClass Library class
* @return LibraryFactory instance
*/
public static <T extends Library> LibraryFactory<T> resolve(Class<T> libraryClass) {
return null; // Implementation details
}
/**
* Get uncached library instance
* @return Uncached library
*/
public abstract T getUncached();
/**
* Get uncached library for specific receiver
* @param receiver Target receiver
* @return Uncached library
*/
public abstract T getUncached(Object receiver);
/**
* Create cached library
* @param receiver Target receiver
* @return Cached library
*/
public abstract T create(Object receiver);
/**
* Create dispatched library
* @param limit Cache limit
* @return Dispatched library
*/
public abstract T createDispatched(int limit);
/**
* Get dispatch class
* @return Library dispatch class
*/
public abstract Class<?> getDispatchClass();
}
/**
* Annotation for generating library classes
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface GenerateLibrary {
/**
* Default export receiver type
* @return Receiver class
*/
Class<?> defaultExportLookupEnabled() default Object.class;
/**
* Whether to generate assert messages
* @return true to generate
*/
boolean assertions() default true;
/**
* Push encapsulating node to library calls
* @return true to push
*/
boolean pushEncapsulatingNode() default true;
}
/**
* Message representation for library operations
*/
public abstract class Message {
/**
* Get message name
* @return Simple message name
*/
public abstract String getSimpleName();
/**
* Get qualified message name
* @return Qualified message name
*/
public abstract String getQualifiedName();
/**
* Get library class
* @return Library class containing message
*/
public abstract Class<? extends Library> getLibraryClass();
}
/**
* Default export provider interface
*/
public interface DefaultExportProvider {
/**
* Get default export class
* @return Export class
*/
Class<?> getExportClass();
/**
* Get export priority
* @return Priority value
*/
int getPriority();
/**
* Create export instance
* @return Export instance
*/
Object createExport();
}
/**
* Eager export provider for immediate initialization
*/
public interface EagerExportProvider extends DefaultExportProvider {
/**
* Get eager export list
* @return List of export classes
*/
List<Class<?>> getEagerExports();
}
/**
* Library that provides reflection capabilities
*/
public abstract class ReflectionLibrary extends Library {
/**
* Get uncached reflection library
* @return Uncached library
*/
public static ReflectionLibrary getUncached() {
return null; // Implementation details
}
/**
* Send message to object
* @param receiver Target object
* @param message Message to send
* @param arguments Message arguments
* @return Message result
* @throws Exception if message fails
*/
public abstract Object send(Object receiver, Message message, Object... arguments) throws Exception;
/**
* Check if object has message
* @param receiver Target object
* @param message Message to check
* @return true if has message
*/
public abstract boolean accepts(Object receiver, Message message);
}
/**
* Library for dynamic dispatch based on receiver type
*/
public abstract class DynamicDispatchLibrary extends Library {
/**
* Get uncached dynamic dispatch library
* @return Uncached library
*/
public static DynamicDispatchLibrary getUncached() {
return null; // Implementation details
}
/**
* Dispatch call based on receiver type
* @param receiver Call receiver
* @param name Method name
* @param arguments Call arguments
* @return Call result
* @throws Exception if dispatch fails
*/
public abstract Object dispatch(Object receiver, String name, Object... arguments) throws Exception;
/**
* Get dispatch target class
* @param receiver Dispatch receiver
* @return Target class for dispatch
*/
public Class<?> dispatch(Object receiver) {
return receiver.getClass();
}
}Usage Example:
@GenerateLibrary
public abstract class MathLibrary extends Library {
/**
* Add two numbers
* @param receiver Left operand
* @param other Right operand
* @return Addition result
*/
public Object add(Object receiver, Object other) {
// Default implementation
throw new UnsupportedOperationException("add not supported");
}
/**
* Multiply two numbers
* @param receiver Left operand
* @param other Right operand
* @return Multiplication result
*/
public Object multiply(Object receiver, Object other) {
// Default implementation
throw new UnsupportedOperationException("multiply not supported");
}
}
@ExportLibrary(MathLibrary.class)
public class MyNumber implements TruffleObject {
private final double value;
public MyNumber(double value) {
this.value = value;
}
@ExportMessage
Object add(Object other) {
if (other instanceof MyNumber) {
return new MyNumber(this.value + ((MyNumber) other).value);
}
throw new UnsupportedOperationException();
}
@ExportMessage
Object multiply(Object other) {
if (other instanceof MyNumber) {
return new MyNumber(this.value * ((MyNumber) other).value);
}
throw new UnsupportedOperationException();
}
}High-performance object layouts with compile-time shape optimization.
/**
* Static object shape defining compile-time layout
* @param <T> Object type
*/
public abstract class StaticShape<T> {
/**
* Get property by name
* @param propertyName Property name
* @return StaticProperty instance or null
*/
public abstract StaticProperty getProperty(Object propertyName);
/**
* Create new instance with this shape
* @return New object instance
*/
public abstract T newInstance();
/**
* Get language associated with shape
* @return TruffleLanguage instance
*/
public abstract TruffleLanguage<?> getLanguage();
/**
* Create shape builder
* @param language Target language
* @return Builder instance
*/
public static <T> Builder<T> newBuilder(TruffleLanguage<?> language) {
return null; // Implementation details
}
/**
* Builder for creating static shapes
* @param <T> Object type
*/
public static final class Builder<T> {
/**
* Add property to shape
* @param property Property to add
* @param type Property type
* @param isFinal Whether property is final
* @return Builder instance
*/
public Builder<T> property(StaticProperty property, Class<?> type, boolean isFinal) {
return this;
}
/**
* Add property with name and type
* @param name Property name
* @param type Property type
* @return Builder instance
*/
public Builder<T> property(String name, Class<?> type) {
return property(StaticProperty.create(name), type, false);
}
/**
* Add final property
* @param name Property name
* @param type Property type
* @return Builder instance
*/
public Builder<T> finalProperty(String name, Class<?> type) {
return property(StaticProperty.create(name), type, true);
}
/**
* Build static shape
* @return StaticShape instance
*/
public StaticShape<T> build() {
return null; // Implementation details
}
/**
* Build with factory
* @param factory Object factory
* @return StaticShape instance
*/
public StaticShape<T> build(Class<T> factory) {
return null; // Implementation details
}
}
}
/**
* Property descriptor for static objects
*/
public interface StaticProperty {
/**
* Create property with name
* @param name Property name
* @return StaticProperty instance
*/
static StaticProperty create(String name) {
return null; // Implementation details
}
/**
* Get property name
* @return Property name
*/
String getName();
/**
* Get property ID for access
* @return Property identifier
*/
String getId();
}
/**
* Shape generator for array-based storage
*/
public abstract class ArrayBasedShapeGenerator<T> extends ShapeGenerator<T> {
/**
* Create array-based generator
* @param language Target language
* @return Generator instance
*/
public static <T> ArrayBasedShapeGenerator<T> create(TruffleLanguage<?> language) {
return null; // Implementation details
}
}
/**
* Shape generator for field-based storage
*/
public abstract class FieldBasedShapeGenerator<T> extends ShapeGenerator<T> {
/**
* Create field-based generator
* @param language Target language
* @return Generator instance
*/
public static <T> FieldBasedShapeGenerator<T> create(TruffleLanguage<?> language) {
return null; // Implementation details
}
}
/**
* Base shape generator class
* @param <T> Object type
*/
public abstract class ShapeGenerator<T> {
/**
* Generate static shape
* @param properties Property definitions
* @return StaticShape instance
*/
public abstract StaticShape<T> generateShape(StaticProperty... properties);
/**
* Generate shape with factory
* @param factory Object factory class
* @param properties Property definitions
* @return StaticShape instance
*/
public abstract StaticShape<T> generateShape(Class<T> factory, StaticProperty... properties);
}Essential runtime utilities for assumptions, context management, and optimization support.
/**
* Assumed value with invalidation support
* @param <T> Value type
*/
public final class AssumedValue<T> {
/**
* Create assumed value
* @param name Assumption name
* @param initialValue Initial value
*/
public AssumedValue(String name, T initialValue) {
// Implementation details
}
/**
* Create assumed value with name
* @param name Assumption name
* @return AssumedValue instance
*/
public static <T> AssumedValue<T> create(String name) {
return new AssumedValue<>(name, null);
}
/**
* Get current value (optimized)
* @return Current value
*/
public T get() {
return null; // Implementation details
}
/**
* Get current value with assumption check
* @param assumption Assumption to validate
* @return Current value
*/
public T get(Assumption assumption) {
return null; // Implementation details
}
/**
* Set new value (invalidates assumption)
* @param newValue New value
*/
public void set(T newValue) {
// Implementation details
}
/**
* Get underlying assumption
* @return Assumption instance
*/
public Assumption getAssumption() {
return null; // Implementation details
}
}
/**
* Cyclic assumption management
*/
public final class CyclicAssumption {
/**
* Create cyclic assumption
* @param name Assumption name
*/
public CyclicAssumption(String name) {
// Implementation details
}
/**
* Get current assumption
* @return Current Assumption
*/
public Assumption getAssumption() {
return null; // Implementation details
}
/**
* Invalidate current assumption and create new one
*/
public void invalidate() {
// Implementation details
}
/**
* Invalidate with message
* @param message Invalidation message
*/
public void invalidate(String message) {
// Implementation details
}
}
/**
* Context-local storage
* @param <T> Stored value type
*/
public final class ContextLocal<T> {
/**
* Get value for current context
* @return Context-local value
*/
public T get() {
return null; // Implementation details
}
/**
* Get value for specific context
* @param context Target context
* @return Context-local value
*/
public T get(TruffleContext context) {
return null; // Implementation details
}
/**
* Set value for current context
* @param value New value
*/
public void set(T value) {
// Implementation details
}
/**
* Set value for specific context
* @param context Target context
* @param value New value
*/
public void set(TruffleContext context, T value) {
// Implementation details
}
}
/**
* Context-thread-local storage
* @param <T> Stored value type
*/
public final class ContextThreadLocal<T> {
/**
* Get value for current context and thread
* @return Context-thread-local value
*/
public T get() {
return null; // Implementation details
}
/**
* Get value for specific context and current thread
* @param context Target context
* @return Context-thread-local value
*/
public T get(TruffleContext context) {
return null; // Implementation details
}
/**
* Get value for specific context and thread
* @param context Target context
* @param thread Target thread
* @return Context-thread-local value
*/
public T get(TruffleContext context, Thread thread) {
return null; // Implementation details
}
/**
* Set value for current context and thread
* @param value New value
*/
public void set(T value) {
// Implementation details
}
/**
* Set value for specific context and current thread
* @param context Target context
* @param value New value
*/
public void set(TruffleContext context, T value) {
// Implementation details
}
}
/**
* Action executed on specific threads
*/
public abstract class ThreadLocalAction {
/**
* Perform action on thread
* @param access Thread access interface
*/
protected abstract void perform(Access access);
/**
* Thread access interface for actions
*/
public interface Access {
/**
* Get current thread
* @return Thread instance
*/
Thread getThread();
/**
* Check if thread is current
* @return true if current thread
*/
boolean isCurrentThread();
/**
* Submit action to thread
* @param action Action to submit
*/
void submit(ThreadLocalAction action);
}
}
/**
* Truffle file system abstraction
*/
public final class TruffleFile {
/**
* Check if file exists
* @return true if exists
*/
public boolean exists() {
return false; // Implementation details
}
/**
* Check if is directory
* @return true if directory
*/
public boolean isDirectory() {
return false; // Implementation details
}
/**
* Check if is regular file
* @return true if regular file
*/
public boolean isRegularFile() {
return false; // Implementation details
}
/**
* Get file size
* @return File size in bytes
*/
public long size() throws IOException {
return 0; // Implementation details
}
/**
* Get file name
* @return File name
*/
public String getName() {
return null; // Implementation details
}
/**
* Get file path
* @return File path string
*/
public String getPath() {
return null; // Implementation details
}
/**
* Read all bytes
* @return File content as bytes
*/
public byte[] readAllBytes() throws IOException {
return null; // Implementation details
}
/**
* Read all text
* @param charset Character set
* @return File content as string
*/
public String readString(Charset charset) throws IOException {
return null; // Implementation details
}
/**
* Write bytes to file
* @param bytes Bytes to write
*/
public void write(byte[] bytes) throws IOException {
// Implementation details
}
/**
* Write string to file
* @param content String content
* @param charset Character set
*/
public void writeString(String content, Charset charset) throws IOException {
// Implementation details
}
/**
* Create input stream
* @return InputStream for file
*/
public InputStream newInputStream() throws IOException {
return null; // Implementation details
}
/**
* Create output stream
* @return OutputStream for file
*/
public OutputStream newOutputStream() throws IOException {
return null; // Implementation details
}
/**
* List directory contents
* @return Array of TruffleFile instances
*/
public TruffleFile[] list() throws IOException {
return null; // Implementation details
}
/**
* Resolve child path
* @param child Child path
* @return Child TruffleFile
*/
public TruffleFile resolve(String child) {
return null; // Implementation details
}
/**
* Get parent directory
* @return Parent TruffleFile or null
*/
public TruffleFile getParent() {
return null; // Implementation details
}
}
/**
* Truffe logger with language-specific configuration
*/
public final class TruffleLogger {
/**
* Log info message
* @param message Log message
*/
public void info(String message) {
// Implementation details
}
/**
* Log warning message
* @param message Log message
*/
public void warning(String message) {
// Implementation details
}
/**
* Log error message
* @param message Log message
*/
public void severe(String message) {
// Implementation details
}
/**
* Log debug message
* @param message Log message
*/
public void fine(String message) {
// Implementation details
}
/**
* Check if level is loggable
* @param level Log level
* @return true if loggable
*/
public boolean isLoggable(Level level) {
return false; // Implementation details
}
/**
* Log with level
* @param level Log level
* @param message Log message
*/
public void log(Level level, String message) {
// Implementation details
}
/**
* Log with level and throwable
* @param level Log level
* @param message Log message
* @param thrown Exception to log
*/
public void log(Level level, String message, Throwable thrown) {
// Implementation details
}
}
/**
* Safepoint management for thread coordination
*/
public final class TruffleSafepoint {
/**
* Poll safepoint on current thread
*/
public static void poll() {
// Implementation details
}
/**
* Poll safepoint with node context
* @param location Node location for debugging
*/
public static void poll(Node location) {
// Implementation details
}
/**
* Set pending safepoint
* @param action Action to execute at safepoint
*/
public static void setPending(ThreadLocalAction action) {
// Implementation details
}
/**
* Check if safepoint is pending
* @return true if pending
*/
public static boolean isPending() {
return false; // Implementation details
}
}Advanced compiler directives and optimization support.
/**
* Compiler directives for optimization hints
*/
public final class CompilerDirectives {
/**
* Mark code path as unlikely
* @param condition Condition to check
* @return Original condition value
*/
public static boolean injectBranchProbability(double probability, boolean condition) {
return condition;
}
/**
* Indicate slow path execution
*/
public static void transferToInterpreter() {
// Implementation details
}
/**
* Indicate slow path with message
* @param message Transfer reason
*/
public static void transferToInterpreterAndInvalidate() {
// Implementation details
}
/**
* Check if compiled
* @return true if compiled code
*/
public static boolean inCompiledCode() {
return false; // Implementation details
}
/**
* Check if interpreting
* @return true if interpreter
*/
public static boolean inInterpreter() {
return !inCompiledCode();
}
/**
* Blackhole value to prevent optimization
* @param value Value to blackhole
*/
public static void blackhole(Object value) {
// Implementation details
}
/**
* Mark value as compilation final
* @param value Value to mark
* @return Original value
*/
public static <T> T compilationFinal(T value) {
return value;
}
/**
* Ensure virtualized
* @param object Object to virtualize
* @return Original object
*/
public static <T> T ensureVirtualized(T object) {
return object;
}
/**
* Cast exact type
* @param value Value to cast
* @param type Target type
* @return Cast value
*/
@SuppressWarnings("unchecked")
public static <T> T castExact(Object value, Class<T> type) {
return (T) value;
}
/**
* Mark boundary between compiled and interpreter
*/
public static void interpreterOnly(Runnable runnable) {
runnable.run();
}
}
/**
* Compilation-time assertions
*/
public final class CompilerAsserts {
/**
* Assert never reached in compiled code
*/
public static void neverPartOfCompilation() {
// Implementation details
}
/**
* Assert never reached with message
* @param message Assertion message
*/
public static void neverPartOfCompilation(String message) {
// Implementation details
}
/**
* Assert always compiled
*/
public static void partialEvaluationConstant(Object value) {
// Implementation details
}
/**
* Assert compilation final
* @param value Value to check
*/
public static void compilationConstant(Object value) {
// Implementation details
}
}/**
* Option descriptor for language/instrument options
*/
public final class OptionDescriptor {
/**
* Create option descriptor
* @param key Option key
* @param help Help text
* @return OptionDescriptor instance
*/
public static OptionDescriptor newBuilder(OptionKey<?> key, String help) {
return null; // Implementation details
}
/**
* Get option key
* @return OptionKey instance
*/
public OptionKey<?> getKey() {
return null; // Implementation details
}
/**
* Get help text
* @return Help text
*/
public String getHelp() {
return null; // Implementation details
}
/**
* Get option name
* @return Option name
*/
public String getName() {
return null; // Implementation details
}
}
/**
* Descriptor for option categories
*/
public enum OptionCategory {
USER, EXPERT, DEBUG
}
/**
* Stability level for APIs
*/
public enum OptionStability {
STABLE, EXPERIMENTAL
}
/**
* Memory management utilities
*/
public final class MemoryUtil {
/**
* Get object size estimate
* @param object Object to measure
* @return Size estimate in bytes
*/
public static long sizeOf(Object object) {
return 0; // Implementation details
}
/**
* Trigger garbage collection hint
*/
public static void gc() {
System.gc();
}
}Install with Tessl CLI
npx tessl i tessl/maven-org-graalvm-truffle--truffle-api