Skip to content

Commit

Permalink
All builtin types extend BuiltinObject
Browse files Browse the repository at this point in the history
  • Loading branch information
Akirathan committed Dec 13, 2024
1 parent e9b0ba9 commit 007d74c
Show file tree
Hide file tree
Showing 17 changed files with 186 additions and 405 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.enso.interpreter.runtime.builtin;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.node.expression.builtin.Builtin;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.data.EnsoObject;
import org.enso.interpreter.runtime.data.Type;
import org.enso.interpreter.runtime.library.dispatch.TypesLibrary;

/**
* Base class for every Enso builtin object. Not type. Note that base class for a builtin type is
* {@link Builtin}.
*
* <p>The {@link InteropLibrary interop} protocol roughly corresponds to the implementation of the
* protocol inside {@link org.enso.interpreter.runtime.data.atom.Atom}.
*
* <p>Note that extension methods are not resolved, because they are not defined in builtins module
* scope. In other words, extension methods are not reported as members via interop.
*/
@ExportLibrary(InteropLibrary.class)
@ExportLibrary(TypesLibrary.class)
public abstract class BuiltinObject extends EnsoObject {

private final String builtinName;

@CompilationFinal private Builtin cachedBuiltinType;

/**
* @param builtinName Simple name of the builtin that should be contained in {@link
* org.enso.interpreter.runtime.builtin.Builtins#builtinsByName}.
*/
protected BuiltinObject(String builtinName) {
this.builtinName = builtinName;
}

@ExportMessage
public final boolean hasType() {
return true;
}

@ExportMessage
public final Type getType(@Bind("$node") Node node) {
if (cachedBuiltinType == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
var ctx = EnsoContext.get(node);
cachedBuiltinType = ctx.getBuiltins().getBuiltinType(builtinName);
}
return cachedBuiltinType.getType();
}

/**
* Must return false, otherwise if a builtin object is passed to a host method that has a single
* {@code Object} argument, host interop would convert the builtin object to a {@code Map} with
* all its members. Even if the builtin object is, e.g., a number of a date.
*
* <p>Must return false as long as all our stdlib Java methods accept {@code Object} and not
* {@link org.graalvm.polyglot.Value} as arguments comming from Enso.
*/
@ExportMessage
public final boolean hasMembers() {
return false;
}

@ExportMessage
public final Object getMembers(boolean includeInternal) throws UnsupportedMessageException {
throw UnsupportedMessageException.create();
}

@ExportMessage
public final boolean hasMetaObject() {
return true;
}

@ExportMessage
public final Type getMetaObject(@Bind("$node") Node node) {
return getType(node);
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package org.enso.interpreter.runtime.data;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import com.oracle.truffle.api.nodes.Node;
import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.LocalTime;
import org.enso.interpreter.dsl.Builtin;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.library.dispatch.TypesLibrary;
import org.enso.interpreter.runtime.builtin.BuiltinObject;
import org.enso.polyglot.common_utils.Core_Date_Utils;

@ExportLibrary(InteropLibrary.class)
@ExportLibrary(TypesLibrary.class)
@Builtin(pkg = "date", name = "Date", stdlibName = "Standard.Base.Data.Time.Date.Date")
public final class EnsoDate extends EnsoObject {
@Builtin(
pkg = "date",
name = EnsoDate.builtinName,
stdlibName = "Standard.Base.Data.Time.Date.Date")
public final class EnsoDate extends BuiltinObject {
static final String builtinName = "Date";
private final LocalDate date;

public EnsoDate(LocalDate date) {
super(builtinName);
this.date = date;
}

Expand Down Expand Up @@ -77,26 +78,6 @@ LocalTime asTime() throws UnsupportedMessageException {
throw UnsupportedMessageException.create();
}

@ExportMessage
Type getMetaObject(@Bind("$node") Node node) {
return EnsoContext.get(node).getBuiltins().date();
}

@ExportMessage
boolean hasMetaObject() {
return true;
}

@ExportMessage
boolean hasType() {
return true;
}

@ExportMessage
Type getType(@Bind("$node") Node node) {
return EnsoContext.get(node).getBuiltins().date();
}

@CompilerDirectives.TruffleBoundary
@ExportMessage
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
package org.enso.interpreter.runtime.data;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import com.oracle.truffle.api.nodes.Node;
import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.enso.interpreter.dsl.Builtin;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.BuiltinObject;
import org.enso.interpreter.runtime.data.text.Text;
import org.enso.interpreter.runtime.library.dispatch.TypesLibrary;
import org.enso.polyglot.common_utils.Core_Date_Utils;

@ExportLibrary(InteropLibrary.class)
@ExportLibrary(TypesLibrary.class)
@Builtin(
pkg = "date",
name = "DateTime",
name = EnsoDateTime.builtinName,
stdlibName = "Standard.Base.Data.Time.Date_Time.Date_Time")
public final class EnsoDateTime extends EnsoObject {
public final class EnsoDateTime extends BuiltinObject {
static final String builtinName = "Date_Time";
private final ZonedDateTime dateTime;

public EnsoDateTime(ZonedDateTime dateTime) {
super(builtinName);
this.dateTime = dateTime;
}

Expand Down Expand Up @@ -205,26 +202,6 @@ ZoneId asTimeZone() {
return dateTime.getZone();
}

@ExportMessage
Type getMetaObject(@CachedLibrary("this") InteropLibrary thisLib) {
return EnsoContext.get(thisLib).getBuiltins().dateTime();
}

@ExportMessage
boolean hasMetaObject() {
return true;
}

@ExportMessage
boolean hasType() {
return true;
}

@ExportMessage
Type getType(@Bind("$node") Node node) {
return EnsoContext.get(node).getBuiltins().dateTime();
}

@ExportMessage
@CompilerDirectives.TruffleBoundary
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package org.enso.interpreter.runtime.data;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import com.oracle.truffle.api.nodes.Node;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand All @@ -16,39 +14,23 @@
import java.time.temporal.Temporal;
import org.enso.interpreter.dsl.Builtin;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.BuiltinObject;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.library.dispatch.TypesLibrary;

@ExportLibrary(InteropLibrary.class)
@ExportLibrary(TypesLibrary.class)
@Builtin(pkg = "date", name = "Duration", stdlibName = "Standard.Base.Data.Time.Duration.Duration")
public final class EnsoDuration extends EnsoObject {
@Builtin(
pkg = "date",
name = EnsoDuration.builtinName,
stdlibName = "Standard.Base.Data.Time.Duration.Duration")
public final class EnsoDuration extends BuiltinObject {
private final Duration duration;
static final String builtinName = "Duration";

public EnsoDuration(Duration duration) {
super(builtinName);
this.duration = duration;
}

@ExportMessage
boolean hasType() {
return true;
}

@ExportMessage
Type getType(@Bind("$node") Node node) {
return EnsoContext.get(node).getBuiltins().duration();
}

@ExportMessage
Type getMetaObject(@Bind("$node") Node node) {
return EnsoContext.get(node).getBuiltins().duration();
}

@ExportMessage
boolean hasMetaObject() {
return true;
}

@Builtin.Method(
name = "new_builtin",
description =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.TruffleFile;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.interop.ArityException;
import com.oracle.truffle.api.interop.InteropLibrary;
Expand Down Expand Up @@ -38,25 +37,26 @@
import java.util.function.Function;
import org.enso.interpreter.dsl.Builtin;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.BuiltinObject;
import org.enso.interpreter.runtime.data.text.Text;
import org.enso.interpreter.runtime.data.vector.ArrayLikeAtNode;
import org.enso.interpreter.runtime.data.vector.ArrayLikeHelpers;
import org.enso.interpreter.runtime.data.vector.ArrayLikeLengthNode;
import org.enso.interpreter.runtime.error.DataflowError;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.library.dispatch.TypesLibrary;

/**
* A wrapper for {@link TruffleFile} objects exposed to the language. For methods documentation
* please refer to {@link TruffleFile}.
*/
@ExportLibrary(InteropLibrary.class)
@ExportLibrary(TypesLibrary.class)
@Builtin(pkg = "io", name = "File", stdlibName = "Standard.Base.System.File.File")
public final class EnsoFile extends EnsoObject {
@Builtin(pkg = "io", name = EnsoFile.builtinName, stdlibName = "Standard.Base.System.File.File")
public final class EnsoFile extends BuiltinObject {
static final String builtinName = "File";
private final TruffleFile truffleFile;

public EnsoFile(TruffleFile truffleFile) {
super(builtinName);
if (truffleFile == null) {
throw CompilerDirectives.shouldNotReachHere();
}
Expand Down Expand Up @@ -797,26 +797,6 @@ public String toString() {
return toDisplayString(false);
}

@ExportMessage
Type getMetaObject(@CachedLibrary("this") InteropLibrary thisLib) {
return EnsoContext.get(thisLib).getBuiltins().file();
}

@ExportMessage
boolean hasMetaObject() {
return true;
}

@ExportMessage
boolean hasType() {
return true;
}

@ExportMessage
Type getType(@Bind("$node") Node node) {
return EnsoContext.get(node).getBuiltins().file();
}

static RuntimeException raiseIOException(Node where, IOException ex) {
var ctx = EnsoContext.get(where);
var guestEx = ctx.asGuestValue(ex);
Expand Down
Loading

0 comments on commit 007d74c

Please sign in to comment.