-
Notifications
You must be signed in to change notification settings - Fork 323
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
Changes from all commits
a050436
22334d8
8f8f71d
e6ee3b3
9d8c857
0ac9a2c
2e6ab25
8a84a79
56f5e5e
b143559
cbef1ec
d6a3614
c9fd054
09e7503
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
||
|
@@ -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.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ooof. I think this is pointless. The |
||
public Array(Object... items) { | ||
this.items = items; | ||
} | ||
|
@@ -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]; | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
* | ||
|
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 {}; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like it