Class DynamicDispatchLibrary
java.lang.Object
com.oracle.truffle.api.nodes.Node
com.oracle.truffle.api.library.Library
com.oracle.truffle.api.library.DynamicDispatchLibrary
- All Implemented Interfaces:
NodeInterface, Cloneable
A library that allows to dynamically dispatch to export library classes. Sometimes the target
exports for a receiver type cannot be statically determined by the receiver class with the
ExportLibrary
annotation. To allow such types to dynamically dispatch to exports the
dynamic dispatch library can be exported instead. By exporting dynamic dispatch the export target
can be chosen dynamically.
The dynamic dispatch library requires to implement the dispatch method. The dispatch method
returns the target exports class that this receiver should dispatch to. If it returns
null
then the type will dispatch to the library default exports. The implementation
of the dispatch must be stable for a single receiver value. For example it is not allowed to
change the dispatch target for a receiver instance. If the dispatch target was changed while the
receiver was used by a library then an AssertionError
will be thrown.
Full usage example
@ExportLibrary(DynamicDispatchLibrary.class) static final class DispatchObject { final Object data; final Class<?> dispatchTarget; DispatchObject(Object data, Class<> dispatchTarget) { this.data = data; this.dispatchTarget = dispatchTarget; } @ExportMessage Class<?> dispatch() { return dispatchTarget; } } @GenerateLibrary public abstract static class ArrayLibrary extends Library { public boolean isArray(Object receiver) { return false; } public abstract int read(Object receiver, int index); } @ExportLibrary(value = ArrayLibrary.class, receiverType = DispatchObject.class) static final class DispatchedBufferArray { @ExportMessage static boolean isArray(DispatchObject o) { return true; } @ExportMessage static int read(DispatchObject o, int index) { return ((int[]) o.data)[index]; } } public static void main(String[] args) { ArrayLibrary arrays = LibraryFactory.resolve(ArrayLibrary.class).getUncached(); assert 42 == arrays.read(new DispatchObject(new int[]{42}, DispatchedBufferArray.class), 0); }
- Since:
- 19.0
-
Nested Class Summary
Nested classes/interfaces inherited from class Node
Node.Child, Node.Children
Modifier and TypeClassDescriptionstatic @interface
Marks fields that represent child nodes of this node.static @interface
Marks array fields that are children of this node. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Constructor for generated subclasses. -
Method Summary
Modifier and TypeMethodDescriptionabstract Object
Cast the object receiver type to the dispatched type.Class
<?> Returns a class thatexports
at least one library with an explicit receiver.static LibraryFactory
<DynamicDispatchLibrary> Returns the library factory forDynamicDispatchLibrary
.Methods inherited from class Node
accept, adoptChildren, atomic, atomic, copy, deepCopy, getChildren, getCost, getDebugProperties, getDescription, getEncapsulatingSourceSection, getLock, getParent, getRootNode, getSourceSection, insert, insert, isAdoptable, isSafelyReplaceableBy, notifyInserted, onReplace, replace, replace, reportPolymorphicSpecialize, reportReplace, toString
Modifier and TypeMethodDescriptionfinal void
accept
(NodeVisitor nodeVisitor) Invokes theNodeVisitor.visit(Node)
method for this node and recursively also for all child nodes.final void
final void
final <T> T
copy()
Creates a shallow copy of this node.deepCopy()
Creates a deep copy of this node.Iterator over the children of this node.getCost()
Deprecated.in 24.1 without replacementReturns properties of this node interesting for debugging and can be overwritten by subclasses to add their own custom properties.Returns a user-readable description of the purpose of the Node, or "" if no description is available.Retrieves the segment of guest language source code that is represented by this Node, if present; otherwise retrieves the segment represented by the nearest AST ancestor that has this information.protected final Lock
getLock()
Returns a lock object that can be used to synchronize modifications to the AST.final Node
The current parent node of this node.final RootNode
Get the root node of the tree a node belongs to.Retrieves the segment of guest language source code that is represented by this Node.final <T extends Node>
Tinsert
(T newChild) final <T extends Node>
T[]insert
(T[] newChildren) boolean
Returnstrue
if this node can be adopted by a parent.final boolean
isSafelyReplaceableBy
(Node newNode) Checks if this node can be replaced by another node: tree structure & type.protected final void
notifyInserted
(Node node) Notifies the framework about the insertion of one or more nodes during execution.protected void
onReplace
(Node newNode, CharSequence reason) Intended to be implemented by subclasses ofNode
to receive a notification when the node is rewritten.final <T extends Node>
Treplace
(T newNode) Replaces this node with another node.final <T extends Node>
Treplace
(T newNode, CharSequence reason) Replaces this node with another node.final void
Notifies the runtime that this node specialized to a polymorphic state.protected final void
reportReplace
(Node oldNode, Node newNode, CharSequence reason) Reports thatoldNode
was replaced withnewNode
, notifying anyreplace observers
and invalidating any compiled call targets.toString()
Converts this node to a textual representation useful for debugging.
-
Constructor Details
-
DynamicDispatchLibrary
protected DynamicDispatchLibrary()Constructor for generated subclasses. Subclasses of this class are generated, do not extend this class directly.- Since:
- 19.0
-
-
Method Details
-
dispatch
-
cast
-
getFactory
Returns the library factory forDynamicDispatchLibrary
.- Since:
- 19.0
-