Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Part 2 of system for builtin objects #3454

Merged
merged 14 commits into from
May 19, 2022
Merged
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@
- [Move Builtin Types and Methods definitions to stdlib][3363]
- [Reduce boilerplate by generating BuiltinMethod nodes from simple method
signatures][3444]
- [Generate boilerplate classes related to error handling and varargs in
builtins from method signatures][3454]

[3227]: https://github.com/enso-org/enso/pull/3227
[3248]: https://github.com/enso-org/enso/pull/3248
Expand All @@ -229,6 +231,7 @@
[3363]: https://github.com/enso-org/enso/pull/3363
[3444]: https://github.com/enso-org/enso/pull/3444
[3453]: https://github.com/enso-org/enso/pull/3453
[3454]: https://github.com/enso-org/enso/pull/3454

# Enso 2.0.0-alpha.18 (2021-10-12)

Expand Down
9 changes: 5 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,11 @@ Global / onChangedBuildSource := ReloadOnSourceChanges
// ============================================================================

ThisBuild / javacOptions ++= Seq(
"-encoding", // Provide explicit encoding (the next line)
"UTF-8", // Specify character encoding used by Java source files.
"-deprecation", // Shows a description of each use or override of a deprecated member or class.
"-g" // Include debugging information
"-encoding", // Provide explicit encoding (the next line)
"UTF-8", // Specify character encoding used by Java source files
"-deprecation", // Shows a description of each use or override of a deprecated member or class
"-g", // Include debugging information
"-Xlint:unchecked", // Enable additional warnings
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like it

)

ThisBuild / scalacOptions ++= Seq(
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import org.enso.interpreter.dsl.AcceptsError;
import org.enso.interpreter.dsl.Builtin;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.error.InvalidArrayIndexError;
import org.enso.interpreter.runtime.Context;
import org.enso.interpreter.runtime.callable.UnresolvedConversion;
import org.enso.interpreter.runtime.callable.UnresolvedSymbol;
Expand All @@ -22,6 +23,7 @@
/** A primitve boxed array type for use in the runtime. */
@ExportLibrary(InteropLibrary.class)
@ExportLibrary(MethodDispatchLibrary.class)
@Builtin(pkg = "mutable", stdlibName = "Standard.Base.Data.Array.Array")
public class Array implements TruffleObject {
private final Object[] items;

Expand All @@ -30,6 +32,7 @@ public class Array implements TruffleObject {
*
* @param items the element values
*/
@Builtin.Method(expandVarargs = 4, description = "Creates an array with given elements.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooof. I think this is pointless. The new_{n} family were just helpers for the java-side tests for when they had no access to stdlib. We should have got rid of them a loooong time ago. And it seems like the whole expandVarargs functionality is basically useless otherwise? At least I can't think of another case.

public Array(Object... items) {
this.items = items;
}
Expand All @@ -39,7 +42,7 @@ public Array(Object... items) {
*
* @param size the size of the created array.
*/
@Builtin(pkg = "mutable", description = "Creates an uninitialized array of a given size.")
@Builtin.Method(description = "Creates an uninitialized array of a given size.")
public Array(long size) {
this.items = new Object[(int) size];
}
Expand Down Expand Up @@ -75,21 +78,19 @@ public Object readArrayElement(long index) throws InvalidArrayIndexException {
}

/** @return the size of this array */
@Builtin(pkg = "mutable", description = "Returns the size of this array.")
@Builtin.Method(description = "Returns the size of this array.")
public long length() {
return this.getItems().length;
}

/** @return an empty array */
@Builtin(pkg = "mutable", description = "Creates an empty Array")
@Builtin.Method(description = "Creates an empty Array")
public static Object empty() {
return new Array();
}

/** @return an identity array */
@Builtin(
pkg = "mutable",
description = "Identity on arrays, implemented for protocol completeness.")
@Builtin.Method(description = "Identity on arrays, implemented for protocol completeness.")
public Object toArray() {
return this;
}
Expand All @@ -104,6 +105,19 @@ long getArraySize() {
return items.length;
}

@Builtin.Method(name = "at", description = "Gets an array element at the given index.")
@Builtin.WrapException(from = IndexOutOfBoundsException.class, to = InvalidArrayIndexError.class)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

holy cow these are such a cool idea! :O

public Object get(long index) {
return getItems()[(int) index];
}

@Builtin.Method(name = "setAt", description = "Gets an array element at the given index.")
@Builtin.WrapException(from = IndexOutOfBoundsException.class, to = InvalidArrayIndexError.class)
public Object set(long index, @AcceptsError Object value) {
getItems()[(int) index] = value;
return this;
}

/**
* Exposes an index validity check through the polyglot API.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import org.enso.interpreter.dsl.Builtin;
import org.enso.interpreter.runtime.Context;
import org.enso.interpreter.runtime.callable.UnresolvedSymbol;
import org.enso.interpreter.runtime.callable.function.Function;
import org.enso.interpreter.runtime.library.dispatch.MethodDispatchLibrary;

/** A mutable reference type. */
@ExportLibrary(MethodDispatchLibrary.class)
@Builtin(pkg = "mutable", stdlibName = "Standard.Base.Data.Ref.Ref")
public class Ref implements TruffleObject {
private volatile Object value;

Expand All @@ -21,11 +23,13 @@ public class Ref implements TruffleObject {
*
* @param value the initial value to store in the reference.
*/
@Builtin.Method(description = "Creates a new Ref")
public Ref(Object value) {
this.value = value;
}

/** @return the current value of the reference. */
@Builtin.Method(name = "get", description = "Gets the value stored in the reference")
public Object getValue() {
return value;
}
Expand All @@ -35,8 +39,11 @@ public Object getValue() {
*
* @param value the value to store.
*/
public void setValue(Object value) {
@Builtin.Method(name = "put", description = "Stores a new value in the reference")
public Object setValue(Object value) {
Object old = this.value;
this.value = value;
return old;
}

@ExportMessage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,51 @@
package org.enso.interpreter.dsl;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;

/** An annotation denoting a method that will auto-generate a BuiltinMethod node. */
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface Builtin {
/** @return the name of the subpackage for the generated method node. */
String pkg() default "";

/** @return a custom name, by default it uses the name of the annotated element. */
String name() default "";
/** @return A fully qualified name of the corresponding Enso type in standard library. */
String stdlibName() default "";

/** @return a short description of this method. */
String description() default "";
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
@interface Method {
/**
* @return a custom name for the generated builting method. By default, it uses the name of the
* annotated method.
*/
String name() default "";

/** @return a short description for the generated builtin method. */
String description() default "";

/**
* @return When applied to a method/constructor with varargs, indicates how many times vararg
* parameter should be expanded and implicitly how many builtin nodes should be generated.
* Must be zero when there are no varaargs in the parameters list.
*/
int expandVarargs() default 0;
}

/**
* Annotation indicating that a potential exception during the execution of the method should be
* wrapped in Enso's error type that will be reported to the user. The annotation can be repeated
* leading to multiple catch clauses.
*/
@Repeatable(WrapExceptions.class)
@interface WrapException {
/** @return Class of the potential exception to be caught during the execution of the method */
Class<? extends Exception> from();
/** @return Class of Enso's builtin (error) type * */
Class<?> to();
}

/** Container for {@link WrapException} annotations */
@interface WrapExceptions {
WrapException[] value() default {};
}
}
Loading