|
| 1 | +package org.enso.interpreter.runtime.builtin; |
| 2 | + |
| 3 | +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
| 4 | +import com.oracle.truffle.api.dsl.Bind; |
| 5 | +import com.oracle.truffle.api.dsl.Cached; |
| 6 | +import com.oracle.truffle.api.dsl.Idempotent; |
| 7 | +import com.oracle.truffle.api.dsl.Specialization; |
| 8 | +import com.oracle.truffle.api.interop.InteropLibrary; |
| 9 | +import com.oracle.truffle.api.interop.UnsupportedMessageException; |
| 10 | +import com.oracle.truffle.api.library.ExportLibrary; |
| 11 | +import com.oracle.truffle.api.library.ExportMessage; |
| 12 | +import com.oracle.truffle.api.nodes.Node; |
| 13 | +import org.enso.interpreter.node.expression.builtin.Builtin; |
| 14 | +import org.enso.interpreter.runtime.EnsoContext; |
| 15 | +import org.enso.interpreter.runtime.data.EnsoObject; |
| 16 | +import org.enso.interpreter.runtime.data.Type; |
| 17 | +import org.enso.interpreter.runtime.library.dispatch.TypesLibrary; |
| 18 | + |
| 19 | +/** |
| 20 | + * Base class for every Enso builtin object. Not type. Note that base class for a builtin type is |
| 21 | + * {@link Builtin}. |
| 22 | + * |
| 23 | + * <p>In other words, this class represents an object of builtin type in a similar way that {@link |
| 24 | + * org.enso.interpreter.runtime.data.atom.Atom} represents an object of a non-builtin type. |
| 25 | + */ |
| 26 | +@ExportLibrary(InteropLibrary.class) |
| 27 | +@ExportLibrary(TypesLibrary.class) |
| 28 | +public abstract class BuiltinObject extends EnsoObject { |
| 29 | + |
| 30 | + @ExportMessage |
| 31 | + public final boolean hasType() { |
| 32 | + return true; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Returns the name of the builtin as saved inside {@link Builtins#builtinsByName}. Not fully |
| 37 | + * qualified. |
| 38 | + * |
| 39 | + * @return |
| 40 | + */ |
| 41 | + protected abstract String builtinName(); |
| 42 | + |
| 43 | + protected final Type getBuiltinType(Node node) { |
| 44 | + return GetType.uncached(this, node); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Must return false, otherwise if a builtin object is passed to a host method that has a single |
| 49 | + * {@code Object} argument, host interop would convert the builtin object to a {@code Map} with |
| 50 | + * all its members. Even if the builtin object is, e.g., a number of a date. |
| 51 | + * |
| 52 | + * <p>Must return false as long as all our stdlib Java methods accept {@code Object} and not |
| 53 | + * {@link org.graalvm.polyglot.Value} as arguments comming from Enso. |
| 54 | + */ |
| 55 | + @ExportMessage |
| 56 | + public final boolean hasMembers() { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + @ExportMessage |
| 61 | + public final Object getMembers(boolean includeInternal) throws UnsupportedMessageException { |
| 62 | + throw UnsupportedMessageException.create(); |
| 63 | + } |
| 64 | + |
| 65 | + @ExportMessage |
| 66 | + public final boolean hasMetaObject() { |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + @ExportMessage(name = "getType", library = TypesLibrary.class) |
| 71 | + @ExportMessage(name = "getMetaObject", library = InteropLibrary.class) |
| 72 | + public static final class GetType { |
| 73 | + |
| 74 | + GetType() {} |
| 75 | + |
| 76 | + /** |
| 77 | + * Caching on class of the receiver - as long as there is the same class, its {@link |
| 78 | + * #builtinName()} method will return the same value. Note that we don't want to cache on the |
| 79 | + * builtin name, as that would create a separate polymorph cache for every instance of the |
| 80 | + * receiver. |
| 81 | + */ |
| 82 | + @Specialization( |
| 83 | + guards = {"cachedReceiverClass == receiver.getClass()", "getCtx(node) == cachedCtx"}, |
| 84 | + limit = "1") |
| 85 | + public static Type doItCached( |
| 86 | + BuiltinObject receiver, |
| 87 | + @Bind("$node") Node node, |
| 88 | + @Cached("receiver.getClass()") Class<? extends BuiltinObject> cachedReceiverClass, |
| 89 | + @Cached(value = "getCtx(node)", allowUncached = true) EnsoContext cachedCtx, |
| 90 | + @Cached(value = "getBuiltinType(receiver, cachedCtx)", allowUncached = true) |
| 91 | + Builtin cachedBuiltinType) { |
| 92 | + return cachedBuiltinType.getType(); |
| 93 | + } |
| 94 | + |
| 95 | + @Specialization(replaces = "doItCached") |
| 96 | + public static Type uncached(BuiltinObject receiver, @Bind("$node") Node node) { |
| 97 | + var ctx = getCtx(node); |
| 98 | + return getBuiltinType(receiver, ctx).getType(); |
| 99 | + } |
| 100 | + |
| 101 | + @TruffleBoundary |
| 102 | + public static Builtin getBuiltinType(BuiltinObject receiver, EnsoContext ctx) { |
| 103 | + return ctx.getBuiltins().getBuiltinType(receiver.builtinName()); |
| 104 | + } |
| 105 | + |
| 106 | + @Idempotent |
| 107 | + public static EnsoContext getCtx(Node node) { |
| 108 | + return EnsoContext.get(node); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments